使用属性解析XML

时间:2013-04-27 09:12:15

标签: java xml-parsing

我想用属性和值解析这个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>

我想将fieldname解析为键,将值解析为dav(values)。

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());
                } 

             }
        }

2 个答案:

答案 0 :(得分:0)

你的问题不是很清楚。有关W3C DOM的Javadoc,请参阅http://www.w3.org/2003/01/dom2-javadoc/org/w3c/dom/Element.html,它将提供属性及其值。我认为您需要

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.

答案 1 :(得分: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());
            }

        }
    }