List of usage examples for javax.xml.parsers DocumentBuilderFactory setNamespaceAware
public void setNamespaceAware(boolean awareness)
From source file:MainClass.java
static public void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("y.xml"); NodeList configs = doc.getElementsByTagName("C"); for (int i = 0; i < configs.getLength(); i++) { Element config = (Element) configs.item(i); String runMode = config.getAttribute("r").trim(); if (runMode.equals("test")) { NodeList connectionURLs = config.getElementsByTagName("URL"); System.out.println(connectionURLs.item(0).getNodeName() + "=" + connectionURLs.item(0).getFirstChild().getNodeValue()); return; }/*from w w w.j a v a 2 s. co m*/ } }
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))); DOMConfiguration config = xmlDoc.getDomConfig(); }
From source file:Main.java
public static void main(String[] args) throws Exception { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder(); Document document = parser.parse(new File("NewFile.xml")); Schema schema = schemaFactory.newSchema(new File("NewFile.xsd")); Validator validator = schema.newValidator(); validator.validate(new DOMSource(document)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { InputSource in = new InputSource(new FileInputStream("y.xml")); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true); Document doc = dfactory.newDocumentBuilder().parse(in); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); CachedXPathAPI path = new CachedXPathAPI(); NodeIterator nl = path.selectNodeIterator(doc, "\\abc\\"); Node n;/*from ww w . j a v a 2 s . c o m*/ while ((n = nl.nextNode()) != null) transformer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(System.out))); }
From source file:Main.java
public static void main(String[] args) throws Throwable { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElement("root"); root.setAttribute("xmlns:m", "http://www.java2s.com/blog"); root.setAttribute("xmlns:rt", "http://www.java2s.com/forum"); doc.appendChild(root);//from w w w . ja va 2 s. c om Element elt = doc.createElement("simple"); elt.setAttribute("m:Path", "false"); elt.setAttribute("m:Content", "false"); elt.setAttribute("rt:file", "false"); root.appendChild(doc.createTextNode("\n\t")); root.appendChild(elt); root.appendChild(doc.createTextNode("\n")); TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(System.out)); }
From source file:Main.java
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse("books.xml"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//numDocs"); 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 w w .ja va2 s . co m }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(false); // never forget this! DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("/data.xml"); XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xpath = xPathFactory.newXPath(); XPathExpression xPathExpression = xpath.compile("//city/text()"); Object result = xPathExpression.evaluate(doc, XPathConstants.NODESET); System.out.println(result.toString()); NodeList nodes = (NodeList) result; System.out.println(nodes.getLength()); for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); }/*from w w w . j a va2 s . c o m*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); // Create original document Document document = db.newDocument(); Element root = document.createElementNS("urn:FOO", "ns0:Root"); document.appendChild(root);/* ww w . j a va 2 s. c o m*/ Element request = document.createElementNS("urn:FOO", "ns0:Request"); root.appendChild(request); // Create new Request element. Element newRequest = document.createElementNS("urn:BAR", "ns1:Request"); // Replace Request element root.replaceChild(newRequest, request); // Output the new document TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(System.out); t.transform(source, result); }
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(); edit(document);//from w ww . ja v a2s . c om System.out.println(documentToString(document)); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document dDoc = builder.parse("c:\\file.xml"); XPath xPath = XPathFactory.newInstance().newXPath(); xPath.setNamespaceContext(new UniversalNamespaceResolver(dDoc)); String query = "//rss/channel/yweather:location/@city"; XPathExpression expr = xPath.compile(query); Object result = expr.evaluate(dDoc, XPathConstants.STRING); System.out.println(result);/* w w w. j a v a 2 s.c o m*/ }