0

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并将其放入字符串数组中。

4

2 回答 2

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但如果你想从 Internet 访问它,你可以这样做。

日志

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;

我希望这可以帮助你。

于 2013-03-14T03:40:20.590 回答
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());它对我有用。

于 2013-03-14T06:00:47.537 回答