1

我有一个这样的 XML:

<thoughts>
    <thought>
        <id>1</id>
        <category>Leadership</category>
        <what>sometext</what>            
        <who>sometext</who>
    </thought>
    <thought>
        <id>2</id>
        <category>Leadership</category>
        <what>sometext</what>            
        <who>sometext</who>
    </thought>
    ... 100's of with category Leadership
    <thought>
        <id>1</id>
        <category>Love</category>
        <what>sometext</what>            
        <who>sometext</who>
    </thought>
    <thought>
        <id>2</id>
        <category>Love</category>
        <what>sometext</what>            
        <who>sometext</who>
    </thought>
    ... 100's of with category Love

    ... and so on up to about ten categories
</thoughts>

我在 java 中解析这个 xml 以获取 android 中的特定类别和 id。这是我的代码:

String id= "100",category="Love";// for example
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(getResources().openRawResource(R.raw.thoughts));
doc.getDocumentElement().normalize();

// Log.d(LOG,"root: " + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("thought");

for (int temp = 0; temp < nList.getLength(); temp++) {
    org.w3c.dom.Node nNode = nList.item(temp);
    Element eElement = (Element) nNode;
    if (nNode.getNodeType() == Node.ELEMENT_NODE && getTagValue("id", eElement).contains(id) && getTagValue("category", eElement).contains(category)) {
        Log.d("THOUGHTSERVICE", "getTagValue(\"what\", eElement):"+getTagValue("what", eElement));
        what = getTagValue("what", eElement);
        who = getTagValue("who", eElement);
    }
}

问题是:这是一种蛮力。你能建议一些其他的方法吗?可以举个例子。

提前致谢

4

4 回答 4

1

为什么不使用 JAXB。您可能想查看Best XML parser for Java以了解更多详细信息

于 2012-09-15T10:41:54.643 回答
1

基于XPath的查询似乎更适合这种情况,因为您知道自己感兴趣的节点。

于 2012-09-15T10:43:02.487 回答
0

更好用xmlPullParser,它是按顺序读取的,你可以在找到并阅读你感兴趣的标签后立即停止阅读。它会减少 RAM 的使用,而且如果所需的标签出现在文件的开头,你就不需要阅读文件的其余部分。

你也可以做一些跳过,比如如果<id>不匹配,跳到下一个<id>标签。

于 2012-09-15T10:41:25.610 回答
0

// 使用 DOM Parser Procedure 解析 xml 响应字符串

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document doc = db.parse(inStream);
NodeList nodeList = doc.getElementsByTagName("thoughts");
for (int index = 0; index < nodeList.getLength(); index++)
{
    Node node = nodeList.item(index);
    if (node.getNodeType() == Node.ELEMENT_NODE)
    {
        Element element = (Element) node;
        NodeList PAYMENT_DATENode = element.getElementsByTagName("id");
        for (int iIndex = 0; iIndex < PAYMENT_DATENode.getLength(); iIndex++) 
        {
            if (PAYMENT_DATENode.item(iIndex).getNodeType() == Node.ELEMENT_NODE) 
            {
                Element nameElement = (Element) PAYMENT_DATENode.item(iIndex);
                String s = (nameElement.getFirstChild().getNodeValue();
            }
        }
    }
}
于 2012-09-15T11:21:28.180 回答