Xml parsing with tag A, which contains several attributes [Java, DOM]

I use a DOM parser for data processing. The problem is that I cannot get the "url =" "length" and "type" tag, which is inside the "enclosure" tag

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

Here is the code I'm using: can someone point me in 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();
}
+3
source share
1 answer

See the method Element.getAttributes()to get all attributes as an array. Element.getAttributeNode(String name)will provide you with a specific attribute.

+6
source

Source: https://habr.com/ru/post/1764317/


All Articles