Jdom解析xml文件
package parsexml;import java.io.IOException;import java.io.InputStream;import org.jdom.Attribute;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;public class JdomParseXml{private static SAXBuilder saxBuilder;private static Element root, node;private static Document document;public static void bulid(InputStream xmlFile){saxBuilder = new SAXBuilder();try{document = saxBuilder.build(xmlFile); // 加载xml文件root = document.getRootElement();// 获得根元素}catch (JDOMException e){e.printStackTrace();}catch (IOException e){e.printStackTrace();}}//获得某节点的值public static String getNodeValue(String nodeName){Element nodeValue = node.getChild(nodeName);return nodeValue.getText();}//获得某节点public static void setChildName(String childName){node = root.getChild(childName);}public static void main(String[] args){JdomParseXml.bulid(JdomParseXml.class.getClassLoader().getResourceAsStream("test.xml"));setChildName("person");String attValue = getNodeAttr("perid");System.out.println(attValue);String ContextValue = getNodeValue("age");System.out.println(ContextValue);}// 获得某节点的属性的值public static String getNodeAttr(String attr){Attribute attrValue = node.getAttribute(attr);return attrValue.getValue();}}