[java]代码库
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//得到解析工厂
DocumentBuilder newDocumentBuilder = factory.newDocumentBuilder();//文档解析器
Document parse = newDocumentBuilder.parse("stus.xml");
NodeList elementsByTagName = parse.getElementsByTagName("stus");
Element e= (Element)elementsByTagName.item(0);
System.out.println(e.getElementsByTagName("age").item(0).getFirstChild().getTextContent());
DOMReader reader=new DOMReader();
org.dom4j.Document read = reader.read(parse);
org.dom4j.Element rootElement = read.getRootElement();
List elements = rootElement.elements();
Iterator<org.dom4j.Element> childs = elements.iterator();
while(childs.hasNext()){
org.dom4j.Element child = childs.next();
System.out.println(child.getText());
if(child.getName().equals("age")){
System.out.println(child.getText());
System.out.println("id:"+child.attributeValue("id"));
List<org.dom4j.Element> elements2 = child.elements("s");
System.out.println(elements2.get(0).getText());
}
}
}
[源代码打包下载]