Dharmendra
Dharmendra

Reputation: 33996

Getting error in DOM Parser

private String getNodeValue(Element e) {
    String str = "";
    Node node = e.getFirstChild();

    if (node instanceof CharacterData) {

        CharacterData cd = (CharacterData) node;
        str += cd.getData();
    }
    System.out.println("String ="+ str);
    return str;
}

i am using this code for parse xml using DOM

EDIT!

<?xml version="1.0" encoding="ISO-8859-1"?>
<root status="1">
<reminder type= "timer" id="861">
<user fromid="48" toid="48" fromemail="[email protected]">Dharmendra Patel</user>
<title>10:00 AM Coffy?</title> 
<desc>Let&#039;s go for coffy</desc>
<date>13/03/2011 09:22:00</date>
<repeat>MO</repeat> 
<todo><category name="">
<item></item> 
</category>
</todo>
</reminder>
</root>

This is my XMl response and i am using this code

NodeList nldesc = elUser.getElementsByTagName("desc");

Element eldesc = (Element) nldesc.item(0);

String taskdesc = getNodeValue(eldesc);

if node value contains String like "Let's go for coeffy" so this gives me string like "Lets go for coeffy" so what is the problem ? please help me..

Upvotes: 0

Views: 418

Answers (1)

Olegas
Olegas

Reputation: 10517

You can use Element.getTextContent() for getting an element text. Note, what this method can be accesed only in API level 8 and higher.

In APi level less than 8 you can use getNodeValue method. It will succeed only if your node is of type TEXT_NODE

EDIT

    try {
        Document doc =
                DocumentBuilderFactory
                        .newInstance()
                        .newDocumentBuilder().parse(
                            new StringBufferInputStream("<desc>let's go for Coffey </desc>"));
        NodeList list = doc.getElementsByTagName("desc");
        Log.d("MainActivity", list.item(0).getFirstChild().getNodeValue());
    } catch (ParserConfigurationException e) {
        // ignore
    } catch (IOException e) {
        // ignore
    } catch (SAXException e) {
        // ignore
    }

The following code works just fine. After it is executed i've got at my logcat:

04-12 21:20:42.766: DEBUG/MainActivity(26843): let's go for Coffey

EDIT 2:

To deal with #&... entities use Html.fromHtml(String) method on encoded string and then toString() on result.

Log.d("MainActivity", 
    Html.fromHTML(
        list.item(0).getFirstChild().getNodeValue()
    ).toString());

EDIT 3

Here is the complete solution for your case.

    String data = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> <root status=\"1\"> <reminder type= \"timer\" id=\"861\"> <user fromid=\"48\" toid=\"48\" fromemail=\"[email protected]\">Dharmendra Patel</user> <title>10:00 AM Coffy?</title> <desc>Let&#039;s go for coffy</desc> <date>13/03/2011 09:22:00</date> <repeat>MO</repeat> <todo><category name=\"\"> <item></item> </category> </todo> </reminder> </root>";
    try {
        Document doc =
                DocumentBuilderFactory
                        .newInstance()
                        .newDocumentBuilder().parse(
                        new StringBufferInputStream(data));
        NodeList list = doc.getElementsByTagName("desc");
        Node node = list.item(0);
        NodeList charNodes = node.getChildNodes();
        StringBuilder builder = new StringBuilder();
        for(int i = 0, l = charNodes.getLength(); i < l; i++) {
            builder.append(Html.fromHtml(charNodes.item(i).getNodeValue()).toString());
        }
        Log.d("MainActivity", builder.toString());
    } catch (ParserConfigurationException e) {
        // ignore
    } catch (IOException e) {
        // ignore
    } catch (SAXException e) {
        // ignore
    }

Upvotes: 1

Related Questions