List of usage examples for javax.xml.parsers DocumentBuilderFactory setNamespaceAware
public void setNamespaceAware(boolean awareness)
From source file:DOMGenerate.java
public static void main(String[] argv) { try {//w w w. java 2 s.c o m DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElementNS(null, "person"); // Create Root Element Element item = doc.createElementNS(null, "name"); // Create element item.appendChild(doc.createTextNode("Jeff")); root.appendChild(item); // Attach element to Root element item = doc.createElementNS(null, "age"); // Create another Element item.appendChild(doc.createTextNode("28")); root.appendChild(item); // Attach Element to previous element down tree item = doc.createElementNS(null, "height"); item.appendChild(doc.createTextNode("1.80")); root.appendChild(item); // Attach another Element - grandaugther doc.appendChild(root); // Add Root to Document DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer // for the DOM LSOutput out = domImplLS.createLSOutput(); StringWriter stringOut = new StringWriter(); // Writer will be a String out.setCharacterStream(stringOut); ser.write(doc, out); // Serialize the DOM System.out.println("STRXML = " + stringOut.toString()); // Spit out the // DOM as a String } catch (Exception ex) { ex.printStackTrace(); } }
From source file:DOMCheck.java
static public void main(String[] arg) { boolean validate = true; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate);//from w w w . jav a 2s .com dbf.setNamespaceAware(true); try { DocumentBuilder builder = dbf.newDocumentBuilder(); builder.setErrorHandler(new MyErrorHandler()); InputSource is = new InputSource("person.xml"); 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:MainClass.java
public static void main(String args[]) throws Exception { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); builderFactory.setValidating(true);//from ww w .j a v a 2s . c o m 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:Main.java
static public void main(String[] arg) { boolean validate = true; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate);/*from w w w . ja va 2 s. com*/ 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: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(), ""); }/*from w w w .j ava 2 s.c o m*/
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setValidating(false);/*from www.j a va2s .co m*/ domFactory.setNamespaceAware(true); domFactory.setIgnoringComments(true); domFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document dDoc = builder.parse("C:/data.xsd"); Node rootNode = dDoc.getElementsByTagName("xs:schema").item(0); System.out.println(rootNode.getNodeName()); XPath xPath1 = XPathFactory.newInstance().newXPath(); NamespaceContext nsContext = new NamespaceContext() { @Override public String getNamespaceURI(String prefix) { return "http://www.w3.org/2001/XMLSchema"; } @Override public String getPrefix(String namespaceURI) { return "xs"; } @Override public Iterator getPrefixes(String namespaceURI) { Set s = new HashSet(); s.add("xs"); return s.iterator(); } }; xPath1.setNamespaceContext((NamespaceContext) nsContext); NodeList nList1 = (NodeList) xPath1.evaluate("//xs:schema", dDoc, XPathConstants.NODESET); System.out.println(nList1.item(0).getNodeName()); NodeList nList2 = (NodeList) xPath1.evaluate("//xs:element", rootNode, XPathConstants.NODESET); System.out.println(nList2.item(0).getNodeName()); }
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 va2 s.co 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: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 {/*from ww w . j av a 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
static public void main(String[] arg) { boolean validate = true; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate);/* ww w . j a v a 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(); InputSource is = new InputSource(new StringReader(getXMLData())); 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); }
From source file:MainClass.java
static public void main(String[] arg) { boolean validate = false; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate);//from w ww . j a v a 2 s .c om dbf.setNamespaceAware(true); dbf.setIgnoringElementContentWhitespace(true); Document doc = null; try { DocumentBuilder builder = dbf.newDocumentBuilder(); doc = builder.parse(new InputSource(new StringReader(xmlString))); } 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); } TreeDumper td = new TreeDumper(); td.dump(doc); }