<UserInfo xmlns="http://schemas.datacontract.org/2004/07/LBB.WCFWebRole.Data" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><DisplayName>admin</DisplayName><FollowerCount>4</FollowerCount><FollowingCount>2</FollowingCount><Id>4</Id><LoginName>admin</LoginName><PostCount>1</PostCount><TagCount>0</TagCount></UserInfo>用dom4j读取这段XML想要获取其中的 比如 Id节点 怎么做?

解决方案 »

  1.   


    private String filePath = getClass().getResource("/").getPath()+"tt.xml"; // 文件所在的实际物理路径
    private static Document document;
    private  void createDocument() {
    SAXReader reader = new SAXReader();
    try {
    this.document = reader.read(new File(filePath));
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    reader = null;
    }
    if (document != null) {
     Element root = document.getRootElement();
     List<Element> childelements = root.elements("Id");
     for(Element e:childelements){
     System.out.println(e.getStringValue());
     }
     }
    }
      

  2.   

    我去。。 原来是Element不是Node = =