0

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

             }
        }
4

2 回答 2

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

        }
    }
于 2013-04-27T10:09:28.763 回答
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.
于 2013-04-27T09:31:13.263 回答