Processing XML Documents Partially : Transform « XML « Java

Processing XML Documents Partially



import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.InputSource;


public class MainClass {
  public static void main(String[] args) throws Exception {
    InputSource in = new InputSource(new FileInputStream("y.xml"));
    DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
    Document doc = dfactory.newDocumentBuilder().parse(in);

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    CachedXPathAPI path = new CachedXPathAPI();
    NodeIterator nl = path.selectNodeIterator(doc, "\\abc\\");

    Node n;
    while ((n = nl.nextNode()) != null)
      transformer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(System.out)));


Related examples in the same category

1.XML transformation
2.A Program That Performs XML Transformations
4.Use the transform.TransformerFactory plugability in the JAXP API
5.Set the TransformerFactory system property to generate and use translets
6.Transforming DOM Node to HTML with JAXP
7.Transformer with parameters
8.Create an XML file and attach an XSL
9.Applying XSLT Stylesheets
10.Catch TransformerException
11.Formatting an XML file using Transformer
12.Transforming an XML File with XSL into a DOM Document
13.XML input, output and transform utilities
14.XSL transformations: It applies a transformation to a set of employee recordsXSL transformations: It applies a transformation to a set of employee records
15.How to write an XML file. It saves a file describing a modern drawing in SVG format.
16.Applies a stylesheet to a given xml document.
17.Applies a stylesheet (that receives parameters) to a given xml document.
18.Apply some indentiation to some XML.