JAVA JDOM解析XML 带CDATA数据

import java.io.StringReader;
import java.util.*;


import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;







public class PraseXmlTest {


public static void main(String[] args) {
String xml = "12345678";
System.out.println(parseXmlToList2(xml)[0]);
}

@SuppressWarnings("unchecked")
private static Object[] parseXmlToList2(String xml){
List argMapList = new ArrayList();
Map retMap = new HashMap();
try {
StringReader read = new StringReader(xml);
// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
InputSource source = new InputSource(read);
// 创建一个新的SAXBuilder
SAXBuilder sb = new SAXBuilder();
// 通过输入源构造一个Document
Document doc = (Document) sb.build(source);
Element root = doc.getRootElement();// 指向根节点
List es = root.getChildren();
if (es != null && es.size() != 0) {
for (Element element : es) {
retMap.put(element.getName(), element.getText());
}
}
argMapList.add(retMap);
} catch (Exception e) {
e.printStackTrace();
}
return new Object[] { argMapList};
}
}

你可能感兴趣的:(JAVA)