2

I am using DOM to parse an XML string as in the following example. This works great except in one instance. The document which I am trying to parse looks like this:

<response requestID=\"1234\">
    <expectedValue>Alarm</expectedValue>
    <recommendations>For steps on how to resolve visit <a href=\"some website\">Website</a> and use the search features for \"Alarm\"<recommendations>
    <setting>Active</setting>
<response>

The code I used to parse the XML is as follows:

try {   
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xmlResult));

        Document doc = db.parse(is);
        NodeList nlResponse = doc.getElementsByTagName("response");

        String[] String = new String[3];  //result entries

        for (int i = 0; i < nlResponse.getLength(); i++) {
            Element e = (Element) nlResponse.item(i);
            int c1 = 0; //count for string array

            NodeList ev = e.getElementsByTagName("expectedValue");
            Element line = (Element) ev.item(0);
            String[c1] = (getCharacterDataFromElement(line));
            c1++;

            NodeList rec = e.getElementsByTagName("recommendations");
            line = (Element) rec.item(0);
            String[c1] = (getCharacterDataFromElement(line));
            c1++;

            NodeList set = e.getElementsByTagName("settings");
            line = (Element) set.item(0);
            String[c1] = (getCharacterDataFromElement(line));
            c1++;  

I am able to parse the code and put the result into a string array (as opposed to the System.out.println()). With the current code, my string array looks as follows:

String[0] = "Alarm"
String[1] = "For steps on how to resolve visit"
String[2] = "Active"

I would like some way of being able to read the rest of the information within "Recommendations" in order to ultimately display the hyperlink (along with other output) in a TextView. How can I do this?

5
  • I don't think anyone can answer that without seeing your code (short of writing the while thing for you) Commented Aug 17, 2011 at 18:06
  • Sorry... I meant to post this link. My code (for the parsing) looks basically the same. java2s.com/Code/Java/XML/… Commented Aug 17, 2011 at 18:09
  • Edit your question to include the relevant parts of your actual code. Commented Aug 17, 2011 at 18:10
  • As a sidenote, the source looks odd to me.Usually < and > would be encoded as &lt; and &gt;. This could explain why your parser is doing dodgy stuff. Commented Aug 17, 2011 at 18:15
  • @Philippe: This is valid XML. Mixed content is allowed. Commented Aug 17, 2011 at 22:58

1 Answer 1

1

I apologize for my previous answer in assuming your xml was ill-formed.

I think what is happening is that your call to the getCharacterDataFromElement is only looking at the first child node for text, when it will need to look at all the child nodes and getting the href attribute as well as the text for the 2nd child node when looking at the recommendations node.

e.g. after getting the Element for recommendation

            String srec = "";
            NodeList nl = line.getChildNodes();
            srec += nl.item(0).getTextContent();

            Node n = nl.item(1);
            NamedNodeMap nm = n.getAttributes();
            srec += "<a href=\"" + nm.getNamedItem("href").getTextContent() +  "\">" + n.getTextContent() + "</a>";

            srec += nl.item(2).getTextContent();

            String[c1] = srec; 
Sign up to request clarification or add additional context in comments.

3 Comments

The current snippet is valid xml.
My mistake, I was under the impression that text couldn't appear within parent nodes alongside its child nodes.
Thank you! Worked perfectly. FWIW, when using Android, API 8 or higher must be used for getTextContent().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.