2

I am using an XML DOM Parser in my Activity and it has to parse XML with CDATA values. The DOM Parser works absolutely fine with plain XML, but when the data contains CDATA values it doesn't work.

The XML file that I need to parse is like:

<?xml version="1.0" encoding="utf-8"?> <organizations> <organization> <name><![CDATA[Center for Maximum]]></name> <image><![CDATA[https://www.xyz.com/company_placeholder.png]]></image> <city><![CDATA[Austin]]></city> <state><![CDATA[Texas]]></state> </organization> </organizations> 

I have added setCoalescing(true) to my DocumentBuilderFactory object but even then it is not parsing correctly. It is giving the error java.net.MalformedURLException: Protocol not found:. Thanks in advance.

2 Answers 2

4

Well the only solution that I found for this problem is to use XPATH for parsing the various item values. Such as, if we need to parse the "name" item in the above XML code then we need to do the following:

 XMLParser parser = new XMLParser(); String xml = parser.getXmlFromUrl(url); // getting XML from URL Document doc = parser.getDomElement(xml); // getting DOM element XPath xPath = XPathFactory.newInstance().newXPath(); String name = null; try { name=xPath.evaluate("//organization/name/text()", doc).trim(); } catch (XPathExpressionException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } 

Now the String name has the desired value of the item "name". This is the work around I used in my code for parsing CDATA. Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

What we need to specify in the first argument of xPath.evaluate.
The first argument tells the name of the element whose value we need to get from the XML. For example, in the above code, we get the value of "name".
Thanks for this solution, but i get value for only 1st entry, i have more than 100 entries in my xml with CDATA, how can i do that? please help me out on this.
1

I am using an XML DOM Parser in my Activity and it has to parse XML with CDATA values. For parsing CDATA content you can try getTextContent on element and you will be able to get data inside CDATA tag.

 NodeList nodeListGetName = doc.getElementsByTagName("name"); Element eleName = (Element) nodeListGetName.item(0); String strName = eleName.getTextContent(); 

In above code I have parsed data for <name> node. You can use the same pattern for other CDATA content parsing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.