2013-05-02 136 views
1
<persons> 
    <people id="1"> 
     <name>Kumar</name> 

     <address> 
     <street doorNumber="1a">First Cross Street</street> 

     <city>Chennai</city> 

     <state>TamilNadu</state> 

     <country>India</country> 

     <postcode>600001</postcode> 

     </address> 

     <Company id="C101">TCS</Company> 

    <sex>Male</sex> 
    </people> 
</persons>  

我想用這種方式得到一個輸出使用dom。 人ID:1 名稱:庫馬爾 地址: 街道: 城市: 狀態: 國家: 郵編: 公司ID 公司: 性別:在java中使用dom解析xml

我無法訪問街道,城市即所有的子節點及其屬性。 這是我的代碼。

NodeList nList = doc.getElementsByTagName("people"); 


     for (int temp = 0; temp < nList.getLength(); temp++) { 

      Node nNode = nList.item(temp); 

      System.out.println("\nCurrent Element :" + nNode.getNodeName()); 

      if (nNode.getNodeType() == Node.ELEMENT_NODE) {     
       Element eElement = (Element) nNode; 

       System.out.println("People ID:" +eElement.getAttribute("id")); 
     System.out.println("Name:" +eElement.getElementsByTagName("name").item(0).getTextContent()); 

     System.out.println("Address:" +eElement.getElementsByTagName("address").item(0).getTextContent()); 

     System.out.print("Sex:" +eElement.getElementsByTagName("sex").item(0).getTextContent()); 

} 

回答

0

一般來說,您需要從解析xml,屬性和內容中獲得兩件事情。 所以添加兩種方法:

getAttrs(文檔DOC,字符串標記名,字符串的Attr)

的getContent(文檔DOC,字符串標記名)

下面是主要功能:

public static void main(String args[]){ 

     try{ 
      File xmlFile = new File("C:/Users/syang/Desktop/Workspace/Test Station/ParseXMl/rec/testing1.xml"); 
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
      Document xmlDom = dBuilder.parse(xmlFile); 

      //normalization 
      xmlDom.getDocumentElement().normalize(); 

      System.out.println("Root element: " + xmlDom.getDocumentElement().getNodeName()); 

      String id = getAttrs(xmlDom,"people","id"); 
      String name = getContent(xmlDom,"name"); 
      String doorNumber = getAttrs(xmlDom,"street","doorNumber"); 
      String streetName = getContent(xmlDom,"Street"); 
      String city = getContent(xmlDom,"city"); 
      String state = getContent(xmlDom,"state"); 
      String country = getContent(xmlDom,"country"); 
      String postcode = getContent(xmlDom,"postcode"); 
      String companyId = getAttrs(xmlDom,"Company","id"); 
      String companyName = getContent(xmlDom,"Company"); 
      String sex = getContent(xmlDom,"sex"); 

      System.out.println("People ID: " + id + "; Name: " + name + "; Address: " + doorNumber + 
        " " + streetName + ", " + city + ", " + state + ", " + country + "; Postcode: " + postcode + 
        "; Company ID: " + companyId + "; Company Name: " + companyName + "; People Sex: " + sex); 


     }catch (Exception e){ 
      e.printStackTrace(); 
     } 

    } 

    private static String getAttrs(Document doc, String tagName, String Attr){ 
     String result = ""; 

     NodeList nList = doc.getElementsByTagName(tagName); 
     if(nList.getLength()>0){ 
      Element eElement = (Element)nList.item(0); 
      String AttrStr = eElement.getAttribute(Attr); 
      if(!"".equals(AttrStr)){ 
       result = String.valueOf(AttrStr); 
      } 

     } 
     return result; 
    } 

    private static String getContent(Document doc, String tagName){ 
     String result = ""; 

     NodeList nList = doc.getElementsByTagName(tagName); 
     if(nList.getLength()>0){ 
      Element eElement = (Element)nList.item(0); 
      String contentStr = eElement.getTextContent(); 
      if(!"".equals(contentStr)){ 
       result = String.valueOf(contentStr); 
      } 

     } 
     return result; 
    } 

輸出結果爲:

根元素:人物人物ID:1;名字:庫馬爾;地址:1a, 印度泰米爾納德邦,欽奈;郵政編碼:600001;公司ID:C101;公司 名稱:TCS;人性別:男性

祝你好運!