Example usage for javax.xml.parsers DocumentBuilderFactory setNamespaceAware

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

Introduction

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

Prototype


public void setNamespaceAware(boolean awareness) 

Source Link

Document

Specifies that the parser produced by this code will provide support for XML namespaces.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File("data.xsd"));

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setSchema(schema);/*from   w w  w  .j  a  v  a  2  s .c o m*/
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new File("data.xml"));

    Element result = document.getElementById("abc");
    System.out.println(result);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setXIncludeAware(true);/*from   ww w  . j  a  va 2 s.c  o  m*/
    DocumentBuilder parser = factory.newDocumentBuilder();
    System.out.println("aware: " + parser.isXIncludeAware());
    Document document = parser.parse(args[0]);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    Source source = new DOMSource(document);
    Result output = new StreamResult(System.out);
    transformer.transform(source, output);
}

From source file:Main.java

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);

    DocumentBuilder loader = factory.newDocumentBuilder();
    Document document = loader.newDocument();

    String docNS = "http://www.my-company.com";

    Element order = document.createElementNS(docNS, "order");
    document.appendChild(order);//from w  ww. j  a  v a 2  s .c  om
    order.setAttribute("xmlns", docNS);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.newDocument();

    Element rootElement = document.createElement("root");
    document.appendChild(rootElement);//from   w w  w.j  a v  a 2s . co  m
    rootElement.setAttributeNS(XMLConstants.XML_NS_URI, "space", "preserve");

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.transform(new DOMSource(document), new StreamResult(System.out));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<xml xmlns:log='http://sample.com'><test log:writer='someWriter'/></xml>";
    StringReader xmlReader = new StringReader(xml);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(xmlReader));

    Element currentNode = (Element) doc.getElementsByTagName("test").item(0);
    String attributeValue = currentNode.getAttributes().getNamedItemNS("http://sample.com", "writer")
            .getNodeValue();/*w w w .  j  a va 2s .c  o  m*/
    System.out.println("Attribute value is " + attributeValue);
    xmlReader.close();
}

From source file:Main.java

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(true);//w  w  w.  j  a  v  a 2  s .  c o m

    DocumentBuilder loader = factory.newDocumentBuilder();
    Document document = loader.parse("sample.xml");

    String docNS = "http://www.my-company.com";

    Element order = document.getDocumentElement();
    Attr date = order.getAttributeNodeNS(docNS, "date");
    NodeList children = order.getElementsByTagNameNS(docNS, "item");

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);
    DocumentBuilder builder;// w ww. j a v a  2 s  .c  om
    builder = docFactory.newDocumentBuilder();
    Document doc = builder.parse(CFG_FILE);
    XPathExpression expr = XPathFactory.newInstance().newXPath().compile(XPATH_FOR_PRM_MaxThread);
    Object result = expr.evaluate(doc, XPathConstants.NUMBER);
    if (result instanceof Double) {
        System.out.println(((Double) result).intValue());
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);
    DocumentBuilder builder = docFactory.newDocumentBuilder();
    Document doc = builder.parse("data.xml");
    XPathExpression expr = XPathFactory.newInstance().newXPath().compile("/script/data");
    Object hits = expr.evaluate(doc, XPathConstants.NODESET);
    if (hits instanceof NodeList) {
        NodeList list = (NodeList) hits;
        for (int i = 0; i < list.getLength(); i++) {
            System.out.println(list.item(i).getTextContent());
        }//  w w w. jav  a  2 s  .  c  om
    }
}

From source file:Main.java

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

    DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Create the parser
    Document xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    System.out.println(xmlDoc.getDocumentURI());
}

From source file:Main.java

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

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    XPathExpression expr = xpath.compile("//element[@key='property1']/text()");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);

    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue());
    }//from   w ww. j  a v  a2s  .  c o  m
}