简体   繁体   English

使用包含多个属性的标记解析Xml [Java,DOM]

[英]Parsing Xml with A tag that contains multiple attributes [Java,DOM]

I use DOM parser to mine the data.Problem is that i cannot manage to get the "url=" "length" and "type" tag that is inside the "enclosure" tag 我使用DOM解析器来挖掘数据。问题是我无法设法获得“enclosure”标签内的“url =”“length”和“type”标签

<item>
      <title>blah blah</title>
      <description>blah blah</description>
      <enclosure url="THEURL" length="LENGTH" type="TYPE" />
</item>

Here's the code that i use : Can anyone point me to the right direction? 这是我使用的代码:任何人都能指出我正确的方向吗?

for (int t = 0; t < nList.getLength(); t++) {
                Node nNode = nList.item(t);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;

                    System.out.println("title : "
                            + getTagValue("title", eElement));
                    System.out.println("description : "
                            + getTagValue("description", eElement));                        

                    for (int t2 = 0; t2 < nList2.getLength(); t2++) {                           
                    Node nNode2 = nList2.item(t2);                  
                    Element eElement2 = (Element) nNode2;
                    System.out.println("url: "
                            + getTagValue("url", eElement2));
                    }
                }
        }

private static String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
            .getChildNodes();
    Node nValue = (Node) nlList.item(0);

    return nValue.getNodeValue();
}

Look up the Element.getAttributes() method to retrieve all the attributes as an array. 查找Element.getAttributes()方法以将所有属性检索为数组。 The Element.getAttributeNode(String name) will give you a specific attribute. Element.getAttributeNode(String name)将为您提供特定属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM