java - Processing CDATA from XML via DOM parser - Stack Overflow
I'm suspecting that your problem is in the following line of code from the
The following example has 3 children: text node "detail ", CDATA node "with cdata" and text node " here":
The following example has 1 child: a CDATA node "detail with cdata here":
But the same example as above written this way:
You either have to loop through all children (no matter how many) and concatenate the value of each to get the full result, or if it's not important for you to differentiate between plain text and text inside CDATA, then set the
Coalescing specifies that the parser produced by this code will convert CDATA nodes to Text nodes and append it to the adjacent (if any) text node. By default the value of this is set to false.
Read full article from java - Processing CDATA from XML via DOM parser - Stack Overflow
I'm suspecting that your problem is in the following line of code from the
getTagValue
method:Node nValue = (Node) nlList.item(0);
You are always getting the first child! But you might have more than one.The following example has 3 children: text node "detail ", CDATA node "with cdata" and text node " here":
<Details>detail <![CDATA[with cdata]]> here</Details>
If you run your code, you get only "detail ", you loose the rest.The following example has 1 child: a CDATA node "detail with cdata here":
<Details><![CDATA[detail with cdata here]]></Details>
If you run your code, you get everything.But the same example as above written this way:
<Details> <![CDATA[detail with cdata here]]> </Details>
now has 3 children because the spaces and line feeds are picked up as text nodes. If you run your code you get the first empty text node with a line feed, you loose the rest.You either have to loop through all children (no matter how many) and concatenate the value of each to get the full result, or if it's not important for you to differentiate between plain text and text inside CDATA, then set the
coalescing
property on the document builder factory first:DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setCoalescing(true);
Coalescing specifies that the parser produced by this code will convert CDATA nodes to Text nodes and append it to the adjacent (if any) text node. By default the value of this is set to false.
Read full article from java - Processing CDATA from XML via DOM parser - Stack Overflow
No comments:
Post a Comment