public static void main(String[] args) {
String str="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
str+="";
str+="";
str+=“18”;
str+=“张三”;
str+="";
str+="";
str+=“19”;
str+=“里斯”;
str+="";
str+="";
//dom
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(str.getBytes(“UTF-8”)));
NodeList node = document.getElementsByTagName(“RESULT”);
for (int i = 0; i < node.getLength(); i++) {
System.out.println(“age:”+document.getElementsByTagName(“AGE”).item(i).getFirstChild().getNodeValue());
System.out.println(“name:”+document.getElementsByTagName(“NAME”).item(i).getFirstChild().getNodeValue());
}
}catch(Exception e){
e.printStackTrace();
}
//dom4j
try {
SAXReader reader = new SAXReader();
org.dom4j.Document document = reader.read(new ByteArrayInputStream(str.getBytes(“UTF-8”)));
Element root = document.getRootElement();
Iterator rt= root.elementIterator(“RESULT”);
Element element;
while(rt.hasNext()){
element = (Element) rt.next();
System.out.print(“age:” + element.elementText(“AGE”));
System.out.println(“name:” + element.elementText(“NAME”));
}
} catch (Exception e) {
e.printStackTrace();
}
//jdom
try {
SAXBuilder builder = new SAXBuilder();
org.jdom.Document document = builder.build(new ByteArrayInputStream(str.getBytes(“UTF-8”)));
org.jdom.Element element = document.getRootElement();
List node = element.getChildren();
for(int i=0;i<node.size();i++) {
System.out.print(“age:” +((org.jdom.Element)node.get(i)).getChild(“AGE”).getText());
System.out.println(“name:” + ((org.jdom.Element)node.get(i)).getChild(“NAME”).getText());
}
} catch (Exception e) {
e.printStackTrace();
}
}
05-05