Android: Parsing XML DOM parser. Converting childnodes to string

Antek Drzewiecki picture Antek Drzewiecki · Jan 12, 2010 · Viewed 9.7k times · Source

Again a question. This time I'm parsing XML messages I receive from a server. Someone thought to be smart and decided to place HTML pages in a XML message. Now I'm kind of facing problems because I want to extract that HTML page as a string from this XML message.

Ok this is the XML message I'm parsing:

<AmigoRequest> <From></From> <To></To> <MessageType>showMessage</MessageType> <Param0>general message</Param0> <Param1><html><head>test</head><body>Testhtml</body></html></Param1> </AmigoRequest>

You see that in Param1 a HTML page is specified. I've tried to extract the message the following way:

public String getParam1(Document d) {
        if (d.getDocumentElement().getTagName().equals("AmigoRequest")) {
            NodeList results = d.getElementsByTagName("Param1");
            // Messagetype depends on what message we are reading.           
            if (results.getLength() > 0 && results != null) {                
                return results.item(0).getFirstChild().getNodeValue();
            }
        }
        return "";
    }

Where d is the XML message in document form. It always returns me a null value, because getNodeValue() returns null. When i try results.item(0).getFirstChild().hasChildNodes() it will return true because he sees there is a tag in the message.

How can i extract the html message <html><head>test</head><body>Testhtml</body></html> from Param0 in a string?

I'm using Android sdk 1.5 (well almost java) and a DOM Parser.

Thanks for your time and replies.

Antek

Answer

Alex Ntousias picture Alex Ntousias · Jan 12, 2010

You could take the content of param1, like this:

public String getParam1(Document d) {
        if (d.getDocumentElement().getTagName().equals("AmigoRequest")) {
            NodeList results = d.getElementsByTagName("Param1");
            // Messagetype depends on what message we are reading.           
            if (results.getLength() > 0 && results != null) {                

                // String extractHTMLTags(String s) is a function that you have 
                // to implement in a way that will extract all the HTML tags inside a string.
                return extractHTMLTags(results.item(0).getTextContent());
            }
        }
        return "";
    }

All you have to do is to implement a function:

String extractHTMLTags(String s)

that will remove all HTML tag occurrences from a string. For that you can take a look at this post: Remove HTML tags from a String