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.

有帮助吗?

解决方案 2

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.

其他提示

The code would be like this.

private void usingDOMParser() {
        try {
            DocumentBuilderFactory mDocumentBuilderFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder mDocumentBuilder = mDocumentBuilderFactory
                    .newDocumentBuilder();
            Document mDocument = mDocumentBuilder.parse(new InputSource(
                    getAssets().open("example.xml")));
            mDocument.getDocumentElement().normalize();
            NodeList mNodeList = mDocument.getElementsByTagName("child");
            for (int i = 0; i < mNodeList.getLength(); i++) {
                Node mNode = mNodeList.item(i);
                Element mElement = (Element) mNode;
                NodeList nameList = mElement.getElementsByTagName("child");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
                Log.i("TAG", "ID: " + nameElement.getAttribute("ID"));
                Log.i("TAG", "Name: " + nameElement.getAttribute("Name"));
                Log.i("TAG", "Order: " + nameElement.getAttribute("Order"));
            }
        }
        catch (Exception e) {
            Log.e("TAG", "Exception: " + e.toString());
        }
    }

Here I put your XML file in Assets folder but if you wanted to access it from Internet, you can do that.

Log

03-14 17:58:15.845: I/AllTestActivity(624): ID: 1
03-14 17:58:15.845: I/AllTestActivity(624): Name: CHILD
03-14 17:58:15.845: I/AllTestActivity(624): Order: 1
03-14 17:58:15.845: I/AllTestActivity(624): ID: 1
03-14 17:58:15.845: I/AllTestActivity(624): Name: SUB_CHILD
03-14 17:58:15.845: I/AllTestActivity(624): Order: 1
03-14 17:58:15.845: I/AllTestActivity(624): ID: 1
03-14 17:58:15.845: I/AllTestActivity(624): Name: SUB_CHILD_NODE1
03-14 17:58:15.845: I/AllTestActivity(624): Order: 01
03-14 17:58:15.845: I/AllTestActivity(624): ID: 2
03-14 17:58:15.845: I/AllTestActivity(624): Name: SUB_CHILD_NODE2
03-14 17:58:15.845: I/AllTestActivity(624): Order: 02
03-14 17:58:15.854: I/AllTestActivity(624): ID: 2
03-14 17:58:15.854: I/AllTestActivity(624): Name: CHILD2
03-14 17:58:15.854: I/AllTestActivity(624): Order: 1
03-14 17:58:15.854: I/AllTestActivity(624): ID: 1
03-14 17:58:15.854: I/AllTestActivity(624): Name: SUB_CHILD
03-14 17:58:15.854: I/AllTestActivity(624): Order: 1
03-14 17:58:15.854: I/AllTestActivity(624): ID: 1
03-14 17:58:15.854: I/AllTestActivity(624): Name: SUB_CHILD_NODE1
03-14 17:58:15.854: I/AllTestActivity(624): Order: 01
03-14 17:58:15.854: I/AllTestActivity(624): ID: 2
03-14 17:58:15.854: I/AllTestActivity(624): Name: SUB_CHILD_NODE2
03-14 17:58:15.854: I/AllTestActivity(624): Order: 02

Imports:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

I hope this can help you out.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top