Code example for DocumentBuilder

Methods: parse, setErrorHandler

0
    throws ParserConfigurationException, SAXException, IOException 
  { 
    // Java XML factories are not declared to be thread safe 
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = factory.newDocumentBuilder();
    db.setErrorHandler(new ParserErrorHandler());
    return db.parse(is);
  } 
 
  /** Converts the given DOM back to a string. */ 
  public static String getXML(Document doc)
    throws TransformerConfigurationException, TransformerException 
  { 
    Source source = new DOMSource(doc);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    // Java XML factories are not declared to be thread safe 
    TransformerFactory factory = TransformerFactory.newInstance();
    factory.setErrorListener(new XMLListener());
    Transformer transformer = factory.newTransformer();
    transformer.transform(source, result);