Example usage for javax.xml.parsers DocumentBuilderFactory newInstance

List of usage examples for javax.xml.parsers DocumentBuilderFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory newInstance.

Prototype

public static DocumentBuilderFactory newInstance() 

Source Link

Document

Obtain a new instance of a DocumentBuilderFactory .

Usage

From source file:DOMEdit.java

static public void main(String[] arg) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);/*from w ww.j av a  2s .  c om*/
        dbf.setNamespaceAware(true);
        dbf.setIgnoringElementContentWhitespace(true);

        Document doc = null;
        try {
            DocumentBuilder builder = dbf.newDocumentBuilder();
            builder.setErrorHandler(new MyErrorHandler());
            InputSource is = new InputSource("personWithDTD.xml");
            doc = builder.parse(is);

            addFragment(doc);

            write(doc);
        } catch (Exception e) {
            System.err.println(e);
        }
    }

From source file:DOMEdit.java

static public void main(String[] arg) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);/*from w  ww  .ja  v a2  s. c  o m*/
        dbf.setNamespaceAware(true);
        dbf.setIgnoringElementContentWhitespace(true);

        Document doc = null;
        try {
            DocumentBuilder builder = dbf.newDocumentBuilder();
            builder.setErrorHandler(new MyErrorHandler());
            InputSource is = new InputSource("personWithDTD.xml");
            doc = builder.parse(is);

            deleteFirstElement(doc);

            write(doc);
        } catch (Exception e) {
            System.err.println(e);
        }
    }

From source file:DOMEdit.java

static public void main(String[] arg) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);//from www. ja va  2 s .co m
        dbf.setNamespaceAware(true);
        dbf.setIgnoringElementContentWhitespace(true);

        Document doc = null;
        try {
            DocumentBuilder builder = dbf.newDocumentBuilder();
            builder.setErrorHandler(new MyErrorHandler());
            InputSource is = new InputSource("personWithDTD.xml");
            doc = builder.parse(is);

            newEmail(doc, "B D", "newEmail");

            write(doc);
        } catch (Exception e) {
            System.err.println(e);
        }
    }

From source file:DOMEdit.java

static public void main(String[] arg) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);/*from www. ja v a2  s.co m*/
        dbf.setNamespaceAware(true);
        dbf.setIgnoringElementContentWhitespace(true);

        Document doc = null;
        try {
            DocumentBuilder builder = dbf.newDocumentBuilder();
            builder.setErrorHandler(new MyErrorHandler());
            InputSource is = new InputSource("personWithDTD.xml");
            doc = builder.parse(is);

            makeNamelist(doc);

            write(doc);
        } catch (Exception e) {
            System.err.println(e);
        }
    }

From source file:DOMEdit.java

static public void main(String[] arg) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);// ww  w . j  a  v  a 2  s  .c o  m
        dbf.setNamespaceAware(true);
        dbf.setIgnoringElementContentWhitespace(true);

        Document doc = null;
        try {
            DocumentBuilder builder = dbf.newDocumentBuilder();
            builder.setErrorHandler(new MyErrorHandler());
            InputSource is = new InputSource("personWithDTD.xml");
            doc = builder.parse(is);

            append(doc, "newName", "1111111111", "newEmail");

            write(doc);
        } catch (Exception e) {
            System.err.println(e);
        }
    }

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<root>" + "<ctc:BasePrice>" + "<cgc:PriceAmount currencyID='EUR'>18.75</cgc:PriceAmount>"
            + "<cgc:BaseQuantity quantityUnitCode='EA'>1</cgc:BaseQuantity>" + "</ctc:BasePrice>"
            + "<ctc:BasePrice>" + "<cgc:PriceAmount currencyID='EUR'>18.25</cgc:PriceAmount>"
            + "<cgc:BaseQuantity quantityUnitCode='EA'>1</cgc:BaseQuantity>"
            + "<cgc:MinimumQuantity quantityUnitCode='EA'>3</cgc:MinimumQuantity>" + "</ctc:BasePrice>"
            + "</root>";

    InputStream xmlStream = new ByteArrayInputStream(xml.getBytes());
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    Document xmlDocument = builder.parse(xmlStream);
    XPath xPath = XPathFactory.newInstance().newXPath();
    String expression = "//*[local-name() = 'BasePrice' and not(descendant::*[local-name() = 'MinimumQuantity'])]/*[local-name()='PriceAmount']";
    NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
    for (int i = 0; i < nodeList.getLength(); i++) {
        System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
    }//from w  w  w. j a  va 2 s. c  o  m
}

From source file:DOMCopy.java

static public void main(String[] arg) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(true);//from  w ww .  java2 s  .c  om
        dbf.setNamespaceAware(true);
        dbf.setIgnoringElementContentWhitespace(true);

        Document doc = null;
        try {
            DocumentBuilder builder = dbf.newDocumentBuilder();
            builder.setErrorHandler(new MyErrorHandler());
            InputSource is = new InputSource("personWithDTD.xml");
            doc = builder.parse(is);

            write(doc);
        } catch (Exception e) {
            System.err.println(e);
        }
    }

From source file:Main.java

public static void main(String[] args) throws Throwable {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    String entryType = null;/*from   w w w.j  a va2  s.co  m*/
    XPathExpression[] expressions = new XPathExpression[] {
            xpath.compile("local-name(*[local-name() = 'package'])"),
            xpath.compile("local-name(*[local-name() = 'libapp'])"),
            xpath.compile("local-name(*[local-name() = 'module'])") };

    DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = fac.newDocumentBuilder();
    Document doc = parser.parse(args[0]);

    for (int i = 0; i < expressions.length; i++) {
        String found = (String) expressions[i].evaluate(doc.getDocumentElement(), XPathConstants.STRING);
        entryType = mappings.get(found);
        if (entryType != null && !entryType.trim().isEmpty()) {
            break;
        }
    }
    System.out.println(entryType);
}

From source file:Main.java

public static void main(String args[]) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true); // Set namespace aware
    builderFactory.setValidating(true); // and validating parser feaures
    builderFactory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = null;
    try {//w w  w. j a va  2 s .  co m
        builder = builderFactory.newDocumentBuilder(); // Create the parser
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    Document xmlDoc = null;

    try {
        xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    } catch (SAXException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();
    }
    DocumentType doctype = xmlDoc.getDoctype();
    if (doctype == null) {
        System.out.println("DOCTYPE is null");
    } else {
        System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
    }

    System.out.println("\nDocument body contents are:");
    listNodes(xmlDoc.getDocumentElement(), ""); // Root element & children
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

        findByID(doc, "uniqueID");

        System.out.println(documentToString(doc));

    }//  w w w.  j a va2 s .  c  om