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 doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); visit(doc, 0);//from w ww .j a va2 s . c o m }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*from w w w . jav a 2 s . c o m*/ factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); CDATASection cdataNode = doc.createCDATASection(""); Comment commentNode = doc.createComment(""); Text textNode = doc.createTextNode(""); // All three types of nodes implement the CharacterData interface CharacterData cdata = cdataNode; cdata = commentNode; cdata = textNode; // data int offset = 5; cdata.insertData(offset, "a "); cdata.appendData(" b"); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*from ww w . java 2s .c o m*/ factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getElementById("key1"); CDATASection cdataNode = doc.createCDATASection(""); Comment commentNode = doc.createComment(""); Text textNode = doc.createTextNode(""); // All three types of nodes implement the CharacterData interface CharacterData cdata = cdataNode; cdata = commentNode; cdata = textNode; // Replace text String replacement = "c"; int offset = 10; int len = 6; cdata.replaceData(offset, len, replacement); }
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 v a 2 s. c o m*/ Document doc = factory.newDocumentBuilder().parse(new File("infilename.xml")); String fragment = "<fragment>aaa</fragment>"; factory = DocumentBuilderFactory.newInstance(); Document d = factory.newDocumentBuilder().parse(new InputSource(new StringReader(fragment))); Node node = doc.importNode(d.getDocumentElement(), true); DocumentFragment docfrag = doc.createDocumentFragment(); while (node.hasChildNodes()) { docfrag.appendChild(node.removeChild(node.getFirstChild())); } Element element = doc.getDocumentElement(); element.appendChild(docfrag); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true);/*from w w w .j a v a 2 s. c o m*/ DocumentBuilder builder = docFactory.newDocumentBuilder(); Document doc = builder.parse("data.xml"); XPathExpression expr = XPathFactory.newInstance().newXPath().compile("/script/data"); Object hits = expr.evaluate(doc, XPathConstants.NODESET); if (hits instanceof NodeList) { NodeList list = (NodeList) hits; for (int i = 0; i < list.getLength(); i++) { System.out.println(list.item(i).getTextContent()); } } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); Element root = doc.getDocumentElement(); EntityReference eref = (EntityReference) root.getFirstChild(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);// w w w .jav a 2s . 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(); Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); NodeList list = doc.getElementsByTagName("*"); for (int i = 0; i < list.getLength(); i++) { Element element = (Element) list.item(i); System.out.println(element); }// ww w .j a v a 2s . c o m }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(new File("data.xml")); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); Element element = (Element) xpath.evaluate("//node3/name", document, XPathConstants.NODE); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(new File("filename.xml")); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); Element element = (Element) xpath.evaluate("//*[@id='one']", document, XPathConstants.NODE); }