如何遍历节点并获取其属性名称?我正在使用DOMParser

时间:2013-03-14 03:17:49

标签: 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" Order="1">
        <child ID="1" Name="SUB_CHILD" Order="1">
        </child>
    </child>
</parent>

代码(新):

 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("-------------------");
        }
    }
}

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

2 个答案:

答案 0 :(得分:1)

代码就是这样。

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文件放在Assets folder中,但如果您想从互联网访问它,则可以这样做。

  

日志

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
  

进口:

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;

我希望这可以帮到你。

答案 1 :(得分:0)

我找到了一个通用解决方案@ http://www.java2s.com/Code/JavaAPI/org.w3c.dom/NodegetAttributes.htm

我也试过你的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 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("-------------------");
        }
    }
}

调用此方法listNodes(document.getDocumentElement().getChildNodes());它适用于我。