List of usage examples for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder
public abstract DocumentBuilder newDocumentBuilder() throws ParserConfigurationException;
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from ww w .ja v a 2s. c o m 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[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);// ww w . ja v a2 s . com factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); NamedNodeMap attrs = element.getAttributes(); int numAttrs = attrs.getLength(); for (int i = 0; i < numAttrs; i++) { Attr attr = (Attr) attrs.item(i); String attrName = attr.getNodeName(); String attrValue = attr.getNodeValue(); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//from ww w. ja v a 2 s.c o m DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("yourFile.xml"); Element rootElement = doc.getDocumentElement(); NodeList children = rootElement.getChildNodes(); Node current = null; int count = children.getLength(); for (int i = 0; i < count; i++) { current = children.item(i); if (current.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) current; if (element.getTagName().equalsIgnoreCase("tableOfContents")) { // Get the list of <tocEntry> items NodeList tocitems = element.getElementsByTagName("tocEntry"); // Obtain a reference to the second one Node secondChild = tocitems.item(1); // Create a new <tocEntry> element Element newTOCItem = doc.createElement("tocEntry"); // Create a new "Help" text node Text newText = doc.createTextNode("Help"); // Make it a child of the new <tocEntry> element // <tocEntry>Help</tocEntry> newTOCItem.appendChild(newText); // Add the new <tocEntry> element to <tableOfContents> element.insertBefore(newTOCItem, secondChild); } } } System.out.println(doc.getDocumentElement()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//from w w w .j a va 2s .c o m factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); // Add a PI at the beginning of the document Element element = doc.getDocumentElement(); ProcessingInstruction pi = doc.createProcessingInstruction("target", "instruction"); element.getParentNode().insertBefore(pi, element); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);// www .j ava 2 s. com DocumentBuilder db = dbf.newDocumentBuilder(); File file1 = new File("input1.xml"); Document doc1 = db.parse(file1); Element rootElement1 = doc1.getDocumentElement(); File file2 = new File("input2.xml"); Document doc2 = db.parse(file2); Element rootElement2 = doc2.getDocumentElement(); // Copy Child Nodes NodeList childNodes2 = rootElement2.getChildNodes(); for (int x = 0; x < childNodes2.getLength(); x++) { Node importedNode = doc1.importNode(childNodes2.item(x), true); if (importedNode.getNodeType() == Node.ELEMENT_NODE) { Element importedElement = (Element) importedNode; // Copy Attributes NamedNodeMap namedNodeMap2 = rootElement2.getAttributes(); for (int y = 0; y < namedNodeMap2.getLength(); y++) { Attr importedAttr = (Attr) doc1.importNode(namedNodeMap2.item(y), true); importedElement.setAttributeNodeNS(importedAttr); } } rootElement1.appendChild(importedNode); } // Output Document TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); DOMSource source = new DOMSource(doc1); 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.setValidating(true);// w w w. j a v a 2s. c o m factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getDocumentElement(); Text text = doc.createTextNode("data\n"); element.appendChild(text); text = doc.createTextNode("<>&\"'"); element.appendChild(text); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from ww w. j a v a2s .com factory.setValidating(true); 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[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);// ww w .j av a 2s .c om factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); NamedNodeMap attrs = element.getAttributes(); String[] names = new String[attrs.getLength()]; for (int i = 0; i < names.length; i++) { names[i] = attrs.item(i).getNodeName(); } for (int i = 0; i < names.length; i++) { attrs.removeNamedItem(names[i]); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/* w ww . j a va 2s. c om*/ factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); boolean has = element.hasAttribute("value"); String attrValue = element.getAttribute("value"); element.setAttribute("value", "newValue1"); element = doc.getElementById("key2"); has = element.hasAttribute("value"); attrValue = element.getAttribute("value"); element.setAttribute("value", "a<\"'&>z"); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*from w ww . j a v a 2 s. co m*/ factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getDocumentElement(); Text text1 = (Text) element.getFirstChild(); String string = text1.getData(); String word = "some"; Text text2 = text1.splitText(string.indexOf(word)); }