XML Transformation : DOM to Text

// JAXP classes

import javax.xml.transform.*;

import javax.xml.transform.dom.*;

import javax.xml.transform.stream.*;

import javax.xml.parsers.*;

 

// DOM classes

import org.w3c.dom.*;

 

// JDK classes

import java.io.*;

 

 

/** Input an RSS document, remove the first "item" element, and

output the resulting RSS document to System.out */

 

class DOMtoText

{

     public static void main(String args[])

     {

          try

          {

              // Input an RSS document into a DOM Document object

              DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

              DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();

              Document document = parser.parse(new File(args[0]));

 

              // Use the DOM API to remove the first item element

              // (this code assumes that there is at least one item...)

              NodeList items = document.getElementsByTagName("item");

              items.item(0).getParentNode().removeChild(items.item(0));

 

              // Use JAXP methods to output the modified Document object

              TransformerFactory tFactory = TransformerFactory.newInstance();

              Transformer transformer = tFactory.newTransformer();

              transformer.transform(new DOMSource(document), new StreamResult(System.out));

          }

          catch (Exception e)

          {

              e.printStackTrace();

          }

          return;

     }

}

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu