2013-04-27 190 views
0

我想用屬性和值解析這個XML。現在我能夠解析值,但我無法解析屬性。解析帶屬性的XML

我的XML:

<Book>   
    <vendor> 
     <vendorPartyReference href="VENDOR"/> 
     <vendorDefinedValue fieldName="Bookuser">dav</vendorDefinedValue> 
     <vendorDefinedValue fieldName="ComUser">dav</vendorDefinedValue> 
     <vendorDefinedValue fieldName="Rate">1.02505</vendorDefinedValue> 
     <vendorDefinedValue fieldName="Points">0.00000</vendorDefinedValue> 
     <vendorDefinedValue fieldName="Design">DSF</vendorDefinedValue> 
     <vendorDefinedValue fieldName="Role">APPROVER</vendorDefinedValue> 
     <vendorDefinedValue fieldName="cover">DXBook12</vendorDefinedValue> 
     <vendorDefinedValue fieldName="Organization">Mannar</vendorDefinedValue> 
    </vendor> 
    </Book> 

我想分析像字段名作爲鍵和值DAV(值)。

NodeList nList = document.getElementsByTagName("Book");   
     for(int s=0; s<nList.getLength() ; s++) 
     { 
      NodeList tsfpmlVendor = document.getElementsByTagName("vendor");    
      for(int i=0; i<tsfpmlVendor.getLength(); i++) 
      { 
       Node tsfpmlVendorNode = tsfpmlVendor.item(i); 
       Element tsfpmlVendorElement = (Element)tsfpmlVendorNode;      
       for(int j=0; j<tsfpmlVendorElement.getElementsByTagName("vendorDefinedValue").getLength(); j++) 
       { 
        listVendorField.add(tsfpmlVendorElement.getElementsByTagName("vendorDefinedValue").item(j).getTextContent()); 
        System.out.println("kkk: "+ tsfpmlVendorElement.getElementsByTagName("vendorDefinedValue").item(j).getAttributes().getLength()); 
        System.out.println("kkk: "+ tsfpmlVendorElement.getElementsByTagName("vendorDefinedValue").item(j).getNodeType()); 
       } 

      } 
     } 

回答

0

你的問題不是很清楚。見http://www.w3.org/2003/01/dom2-javadoc/org/w3c/dom/Element.html的Javadoc獲得W3C DOM,這將給屬性和值。我認爲你需要

java.lang.String getAttribute(java.lang.String name) 
      // Retrieves an attribute value by name. 

Attr getAttributeNode(java.lang.String name) 
      //Retrieves an attribute node by name. 
0

這是你的代碼修改,以便它打印出像「filedName屬性「,它的屬性值如」Bookuser「:

NodeList nList = document.getElementsByTagName("Book"); 
    for (int s = 0; s < nList.getLength(); s++) { 
     NodeList tsfpmlVendor = document.getElementsByTagName("vendor"); 
     for (int i = 0; i < tsfpmlVendor.getLength(); i++) { 
      Node tsfpmlVendorNode = tsfpmlVendor.item(i); 
      Element tsfpmlVendorElement = (Element) tsfpmlVendorNode; 
      for (int j = 0; j < tsfpmlVendorElement.getElementsByTagName("vendorDefinedValue").getLength(); j++) { 
       Node n = tsfpmlVendorElement.getElementsByTagName("vendorDefinedValue").item(j); 
       listVendorField.add(n.getTextContent()); 
       NamedNodeMap nnm = n.getAttributes(); 
       for(int k=0; k<nnm.getLength(); k++) { 
        Node nattr = nnm.item(k); 
        System.out.println("Attribute "+nattr.getNodeName() + " => " + nattr.getNodeValue()); 
       } 
       System.out.println("Value "+n.getTextContent()); 
       System.out.println("kkk: " + tsfpmlVendorElement.getElementsByTagName("vendorDefinedValue").item(j).getAttributes().getLength()); 
       System.out.println("kkk: " + tsfpmlVendorElement.getElementsByTagName("vendorDefinedValue").item(j).getNodeType()); 
      } 

     } 
    }