简体   繁体   中英

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

<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. The Element.getAttributeNode(String name) will give you a specific attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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