/* * (C) Copyright IBM Corp. 2004. All rights reserved. * * US Government Users Restricted Rights Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import org.apache.xerces.dom.DOMOutputImpl; import org.w3c.dom.Document; import org.w3c.dom.bootstrap.DOMImplementationRegistry; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSOutput; import org.w3c.dom.ls.LSParser; import org.w3c.dom.ls.LSSerializer; /** * This code uses the DOM Level 3 LS classes to serialize * an XML file to standard output. */ public class DomFour { public void parseAndPrint(String uri) { Document doc = null; try { System.setProperty(DOMImplementationRegistry.PROPERTY, "org.apache.xerces.dom.DOMXSImplementationSourceImpl"); DOMImplementationRegistry direg = DOMImplementationRegistry.newInstance(); DOMImplementationLS dils = (DOMImplementationLS) direg.getDOMImplementation("LS"); LSParser lsp = dils.createLSParser (DOMImplementationLS.MODE_SYNCHRONOUS, null); doc = lsp.parseURI(uri); LSSerializer domWriter = dils.createLSSerializer(); LSOutput lso = new DOMOutputImpl(); lso.setByteStream(System.out); domWriter.write(doc, lso); } catch (Exception e) { System.err.println("Sorry, an error occurred: " + e); } } /** Main program entry point. */ public static void main(String argv[]) { if (argv.length == 0 || (argv.length == 1 && argv[0].equals("-help"))) { System.out.println("\nUsage: java DomFour uri"); System.out.println(" where uri is the URI of the XML " + "document you want to print."); System.out.println(" Sample: java DomFour sonnet.xml"); System.out.println("\nParses an XML document, then writes " + "the DOM tree to the console."); System.exit(1); } DomFour d4 = new DomFour(); d4.parseAndPrint(argv[0]); } }