Tuesday, 2 August 2011

Generating SAX Parsing Events by Traversing a DOM Document

     

import java.io.File;
import java.net.URI;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;

import org.w3c.dom.Document;
import org.xml.sax.helpers.DefaultHandler;

public class Main {
  public static void main(String[] argvthrows Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Source source = new DOMSource(doc);

    URI uri = new File("infilename.xml").toURI();
    source.setSystemId(uri.toString());

    DefaultHandler handler = new MyHandler();
    SAXResult result = new SAXResult(handler);
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, result);
  }
}

class MyHandler extends DefaultHandler {
}

   
    
    
    
    
  

No comments:

Post a Comment