1

我有一个XmlResourceParser名为xml. 当我尝试调用getText()一个节点时,如我的代码所示,它返回 null。这很奇怪,因为我可以getName()在返回正确值的同一节点上调用,因此实例设置正确。这是我的代码:

    XmlResourceParser xml = context.getResources().getXml(R.xml.thesaurus);

    try {
        //if (xml.getName().equals("word")) {
            xml.next(); //to the first node within <word></word>
            boolean notFound = true;
            while (notFound) {
                xml.next();
                if (xml.getName() != null && xml.getName().equalsIgnoreCase("synonyms")) {
                    String synonym = xml.getText();
                    Log.v(TAG, String.valueOf(synonym));
                    notFound = false; //found
                }
            }
        }
    } catch (XmlPullParserException xppe) {
        xppe.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

这是我的 XML,即使它没有任何问题:

<?xml version="1.0"?>
<thesaurus>
    <word name="let">
        <synonyms>allow</synonyms>
    </word>
</thesaurus>

任何帮助,将不胜感激!谢谢!

4

3 回答 3

3

我在这里找到了自己的答案我使用了@Libin 在此答案中发布的代码。

    int eventType = xmlResourceParser.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_DOCUMENT) {
            System.out.println("Start document");
        } else if (eventType == XmlPullParser.START_TAG) {
            System.out.println("Start tag " + xmlResourceParser.getName());
        } else if (eventType == XmlPullParser.END_TAG) {
            System.out.println("End tag " + xmlResourceParser.getName());
        } else if (eventType == XmlPullParser.TEXT) {
            System.out.println("Text " + xmlResourceParser.getText());
        }
        eventType = xmlResourceParser.next();
    }

感谢大家的帮助

于 2015-09-29T00:27:11.157 回答
0

当您调用 xml.getText() 时,xml 解析器当前指向 START_TAG,而不是内容。调用 xml.next() 让 getText() 返回内容:

if (xml.getName() != null && xml.getName().equalsIgnoreCase("synonyms")) {
   xml.next();
   String synonym = xml.getText();
   Log.v(TAG, String.valueOf(synonym));
   notFound = false; //found
}

您可以验证迭代器的位置,例如:

if (xml.getEventType() == XmlPullParser.TEXT) {
   // iterator is at content
}
于 2015-09-26T22:01:11.660 回答
-1

尝试这个

final String xml ="<?xml version=\"1.0\"?><thesaurus><word name=\"let\"><synonyms>allow</synonyms></word></thesaurus>";

    final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {
        builder = builderFactory.newDocumentBuilder();
        final Document xmlDocument = builder.parse(new ByteArrayInputStream(xml.getBytes()));
        final XPath xPath = XPathFactory.newInstance().newXPath();
        final Object result = xPath.compile("/thesaurus/word/synonyms").evaluate(xmlDocument, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;

        for (int h = 0; h < nodes.getLength(); h++) {
            final Node node = nodes.item(h);
            final NodeList venueChildNodes = node.getChildNodes();
            System.out.println(node.getChildNodes().item(0).getTextContent());
        }

    } catch (ParserConfigurationException | SAXException | IOException e) {
        e.printStackTrace();
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
于 2015-09-21T01:23:19.077 回答