Example usage for javax.xml.parsers DocumentBuilder parse

List of usage examples for javax.xml.parsers DocumentBuilder parse

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilder parse.

Prototype


public abstract Document parse(InputSource is) throws SAXException, IOException;

Source Link

Document

Parse the content of the given input source as an XML document and return a new DOM Document object.

Usage

From source file:TryDOM.java

public static void main(String args[]) throws Exception {

        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        builderFactory.setValidating(true);
        builderFactory.setIgnoringElementContentWhitespace(true);

        DocumentBuilder builder = null;
        builder = builderFactory.newDocumentBuilder();
        builder.setErrorHandler(new TryDOM());
        Document xmlDoc = builder.parse(new File("y.xml"));
        DocumentType doctype = xmlDoc.getDoctype();
        System.out.println("DOCTYPE node:\n" + getDoctypeString(doctype));
        listNodes(xmlDoc.getDocumentElement(), "");
    }// ww  w  .  ja  v a  2  s.co  m

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    builderFactory.setValidating(true);/*from   w  ww .  j  av a  2s.  com*/

    DocumentBuilder builder = null;
    builder = builderFactory.newDocumentBuilder();
    builder.setErrorHandler(new MainClass());
    Document xmlDoc = null;
    xmlDoc = builder.parse(new File("y.xml"));
    listNodes(xmlDoc);
}

From source file:com.cladonia.security.signature.SignatureVerifier.java

public static void main(String[] args) throws Exception {
    org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
            .getLog(SignatureGenerator.class.getName());

    log.info("**** Testing Signature Verification *****");

    javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from w  w  w .  java 2s  .co  m*/
    dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);

    File f = new File("c:\\temp\\sigout.xml");
    javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
    org.w3c.dom.Document doc = db.parse(new java.io.FileInputStream(f));

    SignatureVerifier verifier = new SignatureVerifier(doc, XngrURLUtilities.getURLFromFile(f).toString());
    if (verifier.verify())
        System.out.println("Signature 1 - verification passed");
    else
        System.out.println("Signature 1 - verification failed");

    File f2 = new File("c:\\temp\\sigout2.xml");
    org.w3c.dom.Document doc2 = db.parse(new java.io.FileInputStream(f2));

    // Note the apache api requires that you pass in a base uri, the location where the 
    // signature file is fine, if you do not know this (i.e in the case where the signature
    // file has not been saved), then just pass in "file:", as it works!!
    SignatureVerifier verifier2 = new SignatureVerifier(doc2, "file:");
    if (verifier2.verify())
        System.out.println("Signature 2 - verification passed");
    else
        System.out.println("Signature 2 - verification failed");

    File f3 = new File("c:\\temp\\vordelsig.xml");
    org.w3c.dom.Document doc3 = db.parse(new java.io.FileInputStream(f3));
    SignatureVerifier verifier3 = new SignatureVerifier(doc3, XngrURLUtilities.getURLFromFile(f3).toString());
    if (verifier3.verify())
        System.out.println("Vordel Signature - verification passed");
    else
        System.out.println("Vordel Signature - verification failed");

    File f4 = new File("c:\\temp\\license.xml");
    org.w3c.dom.Document doc4 = db.parse(new java.io.FileInputStream(f4));
    SignatureVerifier verifier4 = new SignatureVerifier(doc4, XngrURLUtilities.getURLFromFile(f4).toString());
    if (verifier4.verify())
        System.out.println("License Signature - verification passed");
    else
        System.out.println("License Signature - verification failed");

}

From source file:Main.java

static public void main(String[] arg) {
    boolean validate = true;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);//from   www  . j a v  a2s .  c  om
    dbf.setNamespaceAware(true);

    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        builder.setErrorHandler(new MyErrorHandler());
        InputSource is = new InputSource(new StringReader(getXMLData()));
        Document doc = builder.parse(is);
    } catch (SAXException e) {
        System.out.println(e);
    } catch (ParserConfigurationException e) {
        System.err.println(e);
    } catch (IOException e) {
        System.err.println(e);
    }
}

From source file:Main.java

static public void main(String[] arg) throws Exception {
    String filename = "input.xml";
    boolean validate = true;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);//from  w  w w.  j  a  v a  2  s  .c  o  m
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = dbf.newDocumentBuilder();
    builder.setErrorHandler(new MyErrorHandler());
    InputSource is = new InputSource(filename);
    Document doc = builder.parse(is);
    TreeDumper td = new TreeDumper();
    td.dump(doc);
}

From source file:DOM2DOM.java

public static void main(String[] args) throws TransformerException, TransformerConfigurationException,
        FileNotFoundException, ParserConfigurationException, SAXException, IOException {
    TransformerFactory tFactory = TransformerFactory.newInstance();

    if (tFactory.getFeature(DOMSource.FEATURE) && tFactory.getFeature(DOMResult.FEATURE)) {
        //Instantiate a DocumentBuilderFactory.
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();

        // And setNamespaceAware, which is required when parsing xsl files
        dFactory.setNamespaceAware(true);

        //Use the DocumentBuilderFactory to create a DocumentBuilder.
        DocumentBuilder dBuilder = dFactory.newDocumentBuilder();

        //Use the DocumentBuilder to parse the XSL stylesheet.
        Document xslDoc = dBuilder.parse("birds.xsl");

        // Use the DOM Document to define a DOMSource object.
        DOMSource xslDomSource = new DOMSource(xslDoc);

        // Set the systemId: note this is actually a URL, not a local filename
        xslDomSource.setSystemId("birds.xsl");

        // Process the stylesheet DOMSource and generate a Transformer.
        Transformer transformer = tFactory.newTransformer(xslDomSource);

        //Use the DocumentBuilder to parse the XML input.
        Document xmlDoc = dBuilder.parse("birds.xml");

        // Use the DOM Document to define a DOMSource object.
        DOMSource xmlDomSource = new DOMSource(xmlDoc);

        // Set the base URI for the DOMSource so any relative URIs it contains can
        // be resolved.
        xmlDomSource.setSystemId("birds.xml");

        // Create an empty DOMResult for the Result.
        DOMResult domResult = new DOMResult();

        // Perform the transformation, placing the output in the DOMResult.
        transformer.transform(xmlDomSource, domResult);

        //Instantiate an Xalan XML serializer and use it to serialize the output DOM to System.out
        // using the default output format, except for indent="yes"
        java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
        xmlProps.setProperty("indent", "yes");
        xmlProps.setProperty("standalone", "no");
        Serializer serializer = SerializerFactory.getSerializer(xmlProps);
        serializer.setOutputStream(System.out);
        serializer.asDOMSerializer().serialize(domResult.getNode());
    } else {/*from  w w  w . j  a  v  a 2s .  co m*/
        throw new org.xml.sax.SAXNotSupportedException("DOM node processing not supported!");
    }
}

From source file:com.cisco.dvbu.ps.common.adapters.config.AdapterConfig.java

public static void main(String[] args) throws Exception {
    org.apache.log4j.BasicConfigurator.configure();
    org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO);
    ;/*from  w  w w.  j a va2s .c  o m*/
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse("D:\\src\\composite\\wsadapter\\cis_adapter_config.xml");
    AdapterConfig ac = new AdapterConfig(null, doc);
    ConnectorConfig cfg = ac.getConnectorConfig("soaphttp");
    ac.validate();
    if (cfg instanceof SoapHttpConfig)
        System.out.println("Configuration type of CIS: SOAPHTTP");
    else
        System.out.println("Unknown config type");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File("testrss.xml"));

    Element root = doc.getDocumentElement();
    if (!root.getTagName().equalsIgnoreCase("rss")) {
        throw new IOException("Invalid RSS document");
    }//  ww w  .  j  a va 2s.  c  om
    List<RSSChannel> channels = readChannels(root.getChildNodes());
    for (RSSChannel channel : channels) {
        System.out.println("Channel: ");
        System.out.println("    title: " + channel.getTitle());
        System.out.println("    link: " + channel.getLink());
        System.out.println("    description: " + channel.getDescription());
        for (RSSItem item : channel.getItems()) {
            System.out.println("    Item: ");
            System.out.println("        title: " + item.getTitle());
            System.out.println("        link: " + item.getLink());
            System.out.println("        description: " + item.getDescription());
            System.out.println("        pubDate: " + item.getPubDate());
            System.out.println("        guid: " + item.getGuid());
        }
    }
}

From source file:DOMCheck.java

static public void main(String[] arg) {
    String filename = null;//from   w  w w  .j a  v  a  2s . co m
    boolean validate = false;

    if (arg.length == 1) {
        filename = arg[0];
    } else if (arg.length == 2) {
        if (!arg[0].equals("-v"))
            usage();
        validate = true;
        filename = arg[1];
    } else {
        usage();
    }

    // Create a new factory to create parsers that will
    // be aware of namespaces and will validate or
    // not according to the flag setting.
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);
    dbf.setNamespaceAware(true);

    // Use the factory to create a parser (builder) and use
    // it to parse the document.
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        builder.setErrorHandler(new MyErrorHandler());
        InputSource is = new InputSource(filename);
        Document doc = builder.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (ParserConfigurationException e) {
        System.err.println(e);
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
}

From source file:DOMDump.java

static public void main(String[] arg) {
    boolean validate = true;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);/*  w  ww.  j  a  va  2 s . c  o  m*/
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    // Parse the input to produce a parse tree with its root
    // in the form of a Document object
    Document doc = null;
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        builder.setErrorHandler(new MyErrorHandler());
        InputSource is = new InputSource("personWithDTD.xml");
        doc = builder.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (ParserConfigurationException e) {
        System.err.println(e);
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
    dump(doc);
}