Working With XML In Java Using DOM
By now we have created Documents, edited them and navigated the Document. Now we have to actually grab some data from the Elements. For this I’ll demonstrate two methods. One is to retrieve a text from an Element and the other is to retrieve CDATA from an Element.
In order to retrieve a text from a node we need to scan all its children and look for Text or CDATA nodes
/**
* Gets the String value of the node.
If the node does not contain text then an empty String is returned
* @param node the node of interest
* @return the value of that node
*/
public static String getTextForNode(Node node)
{
NodeList children;
Node childNode;
String returnString = “”;
children = node.getChildNodes();
if (children != null)
{
for (int i = 0; i < children.getLength(); i++)
{
childNode = children.item(i);
if ((childNode.getNodeType() == Node.TEXT_NODE) || (childNode.getNodeType() == Node.CDATA_SECTION_NODE))
{
returnString = childNode.getNodeValue();
break;
}
}
}
return returnString;
}
We’ll use similar technic to grab all the data from a CDATA Node
/**
* Returns the text from all CDATA sections of the specified element.
* @param element the element
* @return the value of the CDATA sections or null
*/
public static String getCDATA(Element element)
{
StringBuffer buffer = new StringBuffer();
NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++)
{
Node child = children.item(i);
if (child.getNodeType() == Node.CDATA_SECTION_NODE)
{
CDATASection section = (CDATASection)child;
buffer.append(section.getNodeValue());
}
}
String returnValue = null;
if (buffer.length() > 0)
{
returnValue = buffer.toString();
}
return returnValue;
}
Tweet
|

RSS Feeds 

July 1st, 2005 at 8:02 am
Excellent work!!Really helpful.
July 19th, 2005 at 12:23 am
Thanks! Really useful .
November 16th, 2005 at 6:48 am
Excellent work!!!!!
write more article, u have a excelent way of persenting.
i m work on xml in java.
if u have some more info or detail . it will be helpfull.
send it to rajeez@gmail.com
March 10th, 2006 at 4:08 am
Thanks. This is really useful.
August 14th, 2006 at 11:45 am
Awesome! Code is pretty straightforward and thus easy to understand! Thanks Aviran!
September 25th, 2006 at 2:21 am
Hi
Would you have any idea how to save b;ank strings appearing in CDATA sections. I have observed that DOm removes the empty spaces.
For eg.
is rendered as
is it possible to get the same format as after saving through DOM?
TIA
GPS
September 25th, 2006 at 2:23 am
Hi
Would you have any idea how to save blank strings appearing in CDATA sections. I have observed that DOM removes the empty spaces.
For eg.
“”
is rendered as
“”
is it possible to get the same format as “” after saving through DOM?
TIA
GPS
September 25th, 2006 at 10:50 am
thanks a ton. Was looking around . Finally found some consolidated info. Though other sites did help too
May 22nd, 2007 at 9:28 am
Yes, I used some of your methods! Very useful … for creating a new document I was using
Document ontoFragment = builder.getDOMImplementation().createDocument(
null, null, null);
but your way looks like the correct one
thanks for the effort!
July 8th, 2009 at 6:31 am
HI Guys,
Need Help:code for parsing XML String in JSP.
example :
data=”
101
Sachin
”
Thank You