List of usage examples for javax.xml.parsers DocumentBuilderFactory newInstance
public static DocumentBuilderFactory newInstance()
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); DocumentTraversal traversal = (DocumentTraversal) document; NodeIterator iterator = traversal.createNodeIterator(document.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);//from w w w.j a va2 s .co m for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) { System.out.println("Element: " + ((Element) n).getTagName()); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document doc = impl.createDocument(null, null, null); Element e1 = doc.createElement("api"); doc.appendChild(e1);/*w w w. jav a 2 s .c o m*/ Element e2 = doc.createElement("java"); e1.appendChild(e2); e2.setAttribute("url", "http://www.java2s.com"); //transform the DOM for showing the result in console DOMSource domSource = new DOMSource(doc); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); System.out.println(sw.toString()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document doc = impl.createDocument(null, null, null); Element e1 = doc.createElement("api"); doc.appendChild(e1);//from w w w .jav a 2s .com Element e2 = doc.createElement("java"); e1.appendChild(e2); e2.setAttribute("url", "http://www.java2s.com"); // transform the DOM for showing the result in console DOMSource domSource = new DOMSource(doc); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); System.out.println(sw.toString()); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setValidating(false);/*from w ww .ja va 2 s. com*/ 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
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); DocumentTraversal traversal = (DocumentTraversal) document; TreeWalker walker = traversal.createTreeWalker(document.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);/*from w w w . ja va 2 s . com*/ traverseLevel(walker, ""); }
From source file:Main.java
public static void main(String args[]) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document xmldoc = impl.createDocument(null, "XMLDoc", null); Element root = xmldoc.getDocumentElement(); Element e0 = xmldoc.createElement("Doc"); Element e1 = xmldoc.createElement("TITLE"); Node n1 = xmldoc.createTextNode("Java"); e1.appendChild(n1);/* w w w. j av a 2 s.c o m*/ Element e2 = xmldoc.createElement("data"); Node n2 = xmldoc.createTextNode("text node"); e2.appendChild(n2); e0.appendChild(e1); e0.appendChild(e2); root.appendChild(e0); StreamResult out = new StreamResult("howto.xml"); DOMSource domSource = new DOMSource(xmldoc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, out); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = db.parse(new InputSource( new StringReader("<emp><empname><firstName></firstName><lastName></lastName></empname></emp>"))); NodeList customerNodes = doc.getElementsByTagName("empname"); for (int i = 0; i < customerNodes.getLength(); i++) { NodeList children = customerNodes.item(i).getChildNodes(); for (int j = 0; j < children.getLength(); j++) { String childNode = children.item(j).getNodeName(); if (childNode.equalsIgnoreCase("firstName")) { children.item(j).setTextContent(String.valueOf("John")); } else if (childNode.equalsIgnoreCase("lastName")) { children.item(j).setTextContent(String.valueOf("Doe")); }// ww w. j av a 2 s . c o m } } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); System.out.println(writer.getBuffer().toString()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); DocumentTraversal traversal = (DocumentTraversal) document; NodeIterator iterator = traversal.createNodeIterator(document.getDocumentElement(), NodeFilter.SHOW_ALL, new ItemFilter(), true); for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) { System.out.println("Element: " + ((Element) n).getTagName()); }/* www . j ava2 s . c om*/ }
From source file:Main.java
public static void main(String args[]) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); Document xmldoc = impl.createDocument(null, "TODOs", null); Element root = xmldoc.getDocumentElement(); Element e0 = xmldoc.createElement("TOPIC"); Element e1 = xmldoc.createElement("TITLE"); Node n1 = xmldoc.createTextNode("Java"); e1.appendChild(n1);/*from w w w. ja va 2 s . co m*/ Element e2 = xmldoc.createElement("URL"); Node n2 = xmldoc.createTextNode("http://www.server.com"); e2.appendChild(n2); e0.appendChild(e1); e0.appendChild(e2); root.appendChild(e0); Node pi = xmldoc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"TODOs.xsl\""); xmldoc.insertBefore(pi, root); StreamResult out = new StreamResult("howto.xml"); DOMSource domSource = new DOMSource(xmldoc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, out); }
From source file:Main.java
static public void main(String[] arg) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("foo.xml"); TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); NodeList list = doc.getFirstChild().getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node element = list.item(i).cloneNode(true); if (element.hasChildNodes()) { Source src = new DOMSource(element); FileOutputStream fs = new FileOutputStream("k" + i + ".xml"); Result dest = new StreamResult(fs); aTransformer.transform(src, dest); fs.close();/*from w w w . j a va2 s . c o m*/ } } }