1

I have an XmlResourceParser instance called xml. When I try to call getText() on a node, as seen in my code, it returns null. This is strange because I can call getName() on the same node it returns the proper value, so the instance is set up properly. Here is my code:

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

Here is my XML, even though there isn't anything wrong with it:

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

Any help would be appreciated! Thanks!

Bobby
  • 1,416
  • 1
  • 17
  • 30

3 Answers3

3

I found my own answer here. I used the code posted in this answer by @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();
    }

Thanks for everyone's help

Community
  • 1
  • 1
Bobby
  • 1,416
  • 1
  • 17
  • 30
0

When you call xml.getText() the xml parser currently points at the START_TAG, and not at the content. Calling xml.next() lets getText() return the content:

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

You can verify the iterator position for example with:

if (xml.getEventType() == XmlPullParser.TEXT) {
   // iterator is at content
}
ya man
  • 443
  • 1
  • 10
  • 15
-1

try this

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();
    }
  • I don't understand why the XML starts off in a string. I need to read it from a document. I'd be willing to try a NodeList if I could start off reading the XML from a file. How can I do this? Thanks! – Bobby Sep 21 '15 at 01:30
  • 2
    Can you explain what you did here that addresses the question? – nweg Sep 21 '15 at 01:31