3/20/2005

Working With XML In Java Using DOM

Filed under: Aviran Mordo @ 1:12 am

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;
}

 

Pages: 1 2 3 4 5

10 Responses to “Working With XML In Java Using DOM”

  1. Georgios Sam Says:

    Excellent work!!Really helpful.

  2. Anonymous Says:

    Thanks! Really useful .

  3. rajeez Says:

    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

  4. JChuah Says:

    Thanks. This is really useful.

  5. Eduardo Says:

    Awesome! Code is pretty straightforward and thus easy to understand! Thanks Aviran!

  6. gps Says:

    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

  7. gps Says:

    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

  8. Urvashi Says:

    thanks a ton. Was looking around . Finally found some consolidated info. Though other sites did help too

  9. Borislav Kirilov Says:

    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!

  10. Sachin Says:

    HI Guys,

    Need Help:code for parsing XML String in JSP.

    example :

    data=”
    101
    Sachin

    Thank You

Leave a Reply

You must have Javascript enabled in order to submit comments.

All fields are optional (except comment).
Some comments may be held for moderation (depends on spam filter) and not show up immediately.
Links will automatically get rel="nofollow" attribute to deter spammers.

Powered by WordPress