import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import java.io.File;
import java.util.Map;
import static me.javaw.me.utils.ConverXMLToMap.*;
/**
* Created by sxyqj on 2017/11/23.
*
* xml_parse api test
*
* jar包提供以下api,已全部列出
*
*/
public class TestMain {
public static void main(String[] args) throws Exception {
String path = TestMain.class.getClassLoader().getResource("sample.xml").getPath();
String textFromFile = FileUtils.readFileToString(new File(path),"GB2312");
/**
* xml转map 不带属性
*/
Map<String, Object> map = xml2map(textFromFile, true);
System.out.println(JSONObject.toJSON(map));
/**
* xml转map 带属性
*/
Map map1 = xml2mapWithAttr(textFromFile, false);
Map map2 = xml2mapWithAttr(textFromFile, true);
System.out.println(JSONObject.toJSON(map1));
/**
* map转xml map中没有根节点的键
*/
Document xml = map2xml(map1,"root");
System.out.println(formatXml(xml));
/**
* map转xml map中含有根节点的键
*/
Document xml1 = map2xml(map2,"root");
System.out.println(formatXml(xml1));
}
}