List of usage examples for javax.xml.parsers DocumentBuilderFactory newInstance
public static DocumentBuilderFactory newInstance()
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.newDocument(); Element book = document.createElement("book"); book.setAttribute("id", "javanut4"); document.appendChild(book);// w w w. j av a 2 s. c o m for (int i = 1; i <= 3; i++) { Element chapter = document.createElement("chapter"); Element title = document.createElement("title"); title.appendChild(document.createTextNode("Chapter " + i)); chapter.appendChild(title); chapter.appendChild(document.createElement("para")); book.appendChild(chapter); } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(System.out); transformer.transform(source, result); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*from w w w . j a v a2 s.c o m*/ DocumentBuilder db = dbf.newDocumentBuilder(); // Create original document Document document = db.newDocument(); Element root = document.createElementNS("urn:FOO", "ns0:Root"); document.appendChild(root); 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[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false);/* www.ja v a2 s . com*/ DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new FileInputStream(new File("input.xml"))); File OutputDOM = new File("out.txt"); FileOutputStream fostream = new FileOutputStream(OutputDOM); OutputStreamWriter oswriter = new OutputStreamWriter(fostream); BufferedWriter bwriter = new BufferedWriter(oswriter); if (!OutputDOM.exists()) { OutputDOM.createNewFile(); } visitRecursively(doc, bwriter); bwriter.close(); oswriter.close(); fostream.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("path/to/file.xml")); XPathFactory xFactory = XPathFactory.newInstance(); XPath xPath = xFactory.newXPath(); XPathExpression expression = xPath.compile("PersonList/Person/Age/text() | PersonList/Person/Name/text()"); NodeList nodes = (NodeList) expression.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getParentNode().getNodeName().equals("Name")) { node.setNodeValue("new name"); } else {//from ww w .j a va 2 s . com node.setNodeValue("42"); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);//from w w w.j a v a2s. c o m DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.newDocument(); Element rootElement = document.createElement("root"); document.appendChild(rootElement); 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);/*from ww w . j a va2s. c o m*/ 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(); System.out.println("Attribute value is " + attributeValue); xmlReader.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/* w w w . j a v a 2 s . c om*/ factory.setXIncludeAware(true); 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 arg[]) throws Exception { String xmlRecords = "<root><x>1</x><x>2</x><x>3</x><x>4</x></root>"; DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xmlRecords)); Document doc = db.parse(is);//from www .j ava 2 s. c o m NodeList nodes = doc.getElementsByTagName("x"); System.out.println(nodes.getLength()); List<String> valueList = new ArrayList<String>(); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); String name = element.getTextContent(); // Element line = (Element) name.item(0); System.out.println("Name: " + name); valueList.add(name); } }
From source file:Main.java
public static void main(String args[]) throws Exception { FileInputStream fileInputStream = new FileInputStream(new File("src/file.xml")); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document doc1 = builder.parse(fileInputStream); doc1.getDocumentElement().normalize(); NodeList kList1 = doc1.getElementsByTagName("item"); StringBuilder stringBuilder = new StringBuilder(); for (int temp = 0; temp < kList1.getLength(); temp++) { Node kNode1 = kList1.item(temp); System.out.println("\nCurrent Element :" + kNode1.getNodeName()); if (kNode1.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) kNode1; System.out.println("node name" + eElement.getNodeName()); Node in = eElement.getFirstChild(); if ((in.getTextContent() != null) && !(in.getTextContent()).isEmpty() && !(in.getTextContent().length() == 0)) stringBuilder.append(in.getTextContent()); }/* w w w .j a va2s. c om*/ } System.out.println(stringBuilder); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);//from w w w . ja v a 2 s . c o m 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()); // DOM as a String }