Android 如何遍历节点并获取其属性名称?I';m使用DOMParser

Android 如何遍历节点并获取其属性名称?I';m使用DOMParser,android,domparser,Android,Domparser,xml文件如下所示: <?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

xml文件如下所示:

<?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>

代码(新):

void列表节点(节点列表){
if(list.getLength()>0){
对于(int i=0;i

我已经编辑了代码。现在我知道
attribute
具有这些属性。如何提取属性
Name
并将其放入字符串数组中。

代码如下所示

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());
        }
    }
进口:


我希望这能帮助您。

我找到了一个通用解决方案@

我也试过你的xml,它很好用;只需填充POJO而不是Sysout,您就可以开始了

你需要的方法是

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";
}
静态void列表节点(节点节点,字符串缩进){
字符串nodeName=node.getNodeName();
System.out.println(缩进+“节点:”+nodeName);
short type=node.getNodeType();
//System.out.println(缩进+“节点类型:”+nodeType(类型));
如果(类型==文本\节点){
System.out.println(缩进+“内容为:”
+((文本)节点).getWholeText());
}else if(node.hasAttributes()){
System.out.println(indent+“元素属性为:”);
NamedNodeMap attrs=node.getAttributes();
对于(int i=0;i0){
系统输出
.println(“+nodeName+”的缩进+子节点为:”;
对于(int i=0;i
已编辑

根据您的请求,我稍微修改了代码,因为您只需要直接子节点,而不是子节点。给你:

 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("-------------------");
        }
    }
}
void列表节点(节点列表){
if(list.getLength()>0){
对于(int i=0;i

调用此方法
listNodes(document.getDocumentElement().getChildNodes())它适合我

标记返回异常:java.lang.NullPointerException。我做错什么了吗?这个问题的预期输出是什么?mNodeList.getLength()只返回1在看到您的答案之前,我已经解决了部分问题。我提供了上面的代码。谢谢你的回答。如果你的代码可以解决当前的问题,我很乐意使用它。使用上面的代码,你可以得到所有节点和所有与之相关的元素,你可以修改代码,在你想要的地方中断。对不起,我不知道怎么做,先生。我几乎不知所措了。静态void列表节点(节点节点,字符串缩进)中的值来自哪里?什么是文本节点?文本节点来自
import static org.w3c.dom.NODE.TEXT\u节点
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;
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";
}
 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("-------------------");
        }
    }
}