Search code examples
androiddomparser

How to traverse the nodes and get their attribute names? I'm using DOMParser


The xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<parent>
    <child ID="1" Name="CHILD" Order="1">
        <child ID="1" Name="SUB_CHILD" Order="1">
        </child>
    </child>
    <child ID="2" Name="CHILD2" Order="1">
        <child ID="1" Name="SUB_CHILD" Order="1">
        </child>
    </child>
</parent>

CODE (NEW):

 void listNodes(NodeList list) {
    if (list.getLength() > 0) {
        for (int i = 0; i < list.getLength(); i++) {
            System.out.println("-------------------");
            if (list.item(i).hasAttributes()) {
                NamedNodeMap attrs = list.item(i).getAttributes();
                for (int index = 0; index < attrs.getLength(); index++) {
                    Attr attribute = (Attr) attrs.item(index);
                    if(attribute.getName().equals("Name")){
                     names[index] = ????                            
                        }
                }
            }else{
                System.out.println(list.item(i).getNodeName()+ " has no attributes");
            }
            System.out.println("-------------------");
        }
    }
}

I have edited the code. Now I know attribute has the attributes. How do I extract the attribute Name and put it inside a string array.


Solution

  • There is one generic solution which i found @ http://www.java2s.com/Code/JavaAPI/org.w3c.dom/NodegetAttributes.htm

    I tried your xml as well, it works fine; just populate your POJO instead of Sysout and you are good to go.

    The methods you need are

    static void listNodes(Node node, String indent) {
        String nodeName = node.getNodeName();
        System.out.println(indent + " Node: " + nodeName);
        short type = node.getNodeType();
        //System.out.println(indent + " Node Type: " + nodeType(type));
        if (type == TEXT_NODE) {
            System.out.println(indent + " Content is: "
                    + ((Text) node).getWholeText());
        } else if (node.hasAttributes()) {
            System.out.println(indent + " Element Attributes are:");
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr attribute = (Attr) attrs.item(i);
                System.out.println(indent + " " + attribute.getName() + " = "
                        + attribute.getValue());
            }
        }
    
        NodeList list = node.getChildNodes();
        if (list.getLength() > 0) {
            System.out
                    .println(indent + " Child Nodes of " + nodeName + " are:");
            for (int i = 0; i < list.getLength(); i++) {
                listNodes(list.item(i), indent + "  ");
            }
        }
    }
    
    static String nodeType(short type) {
        switch (type) {
        case ELEMENT_NODE:
            return "Element";
        case DOCUMENT_TYPE_NODE:
            return "Document type";
        case ENTITY_NODE:
            return "Entity";
        case ENTITY_REFERENCE_NODE:
            return "Entity reference";
        case NOTATION_NODE:
            return "Notation";
        case TEXT_NODE:
            return "Text";
        case COMMENT_NODE:
            return "Comment";
        case CDATA_SECTION_NODE:
            return "CDATA Section";
        case ATTRIBUTE_NODE:
            return "Attribute";
        case PROCESSING_INSTRUCTION_NODE:
            return "Attribute";
        }
        return "Unidentified";
    }
    

    Edited

    As per your request I modified the code a bit as you just want the immediate child nodes and not the sub child. here you go:

     void listNodes(NodeList list) {
        if (list.getLength() > 0) {
            for (int i = 0; i < list.getLength(); i++) {
                System.out.println("-------------------");
                if (list.item(i).hasAttributes()) {
                    NamedNodeMap attrs = list.item(i).getAttributes();
                    for (int index = 0; index < attrs.getLength(); index++) {
                        Attr attribute = (Attr) attrs.item(index);
                        System.out.println(" " + attribute.getName() + " = "+ attribute.getValue());
                    }
                }else{
                    System.out.println(list.item(i).getNodeName()+ " has no attributes");
                }
                System.out.println("-------------------");
            }
        }
    }
    

    Calling this method listNodes(document.getDocumentElement().getChildNodes()); It works for me.