List of usage examples for org.w3c.dom NodeList item
public Node item(int index);
index
th item in the collection. From source file:Main.java
public static void main(String[] args) throws Exception { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource("data.xml")); XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList) xpath.evaluate("//employee/name[text()='old']", doc, XPathConstants.NODESET); for (int idx = 0; idx < nodes.getLength(); idx++) { nodes.item(idx).setTextContent("new value"); }/*from w w w .java2 s. c o m*/ Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(new DOMSource(doc), new StreamResult(new File("data_new.xml"))); }
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("in.xml")); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); NodeList nl = doc.getDocumentElement().getChildNodes(); DOMSource source = null;/*from w w w . j a va2s. co m*/ for (int x = 0; x < nl.getLength(); x++) { Node e = nl.item(x); if (e instanceof Element) { source = new DOMSource(e); break; } } transformer.transform(source, result); System.out.println(sw.toString()); }
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 ava 2s . c o m*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(new InputSource(new StringReader(cfgXml))); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("config"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); System.out.println("\nCurrent Element :" + nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; Config c = new Config(); c.name = eElement.getAttribute("name"); c.type = eElement.getAttribute("type"); c.format = eElement.getAttribute("format"); c.size = Integer.valueOf(eElement.getAttribute("size")); c.scale = Integer.valueOf(eElement.getAttribute("scale")); String attribute = eElement.getAttribute("required"); c.required = Boolean.valueOf("Yes".equalsIgnoreCase(attribute) ? true : false); System.out.println("Imported config : " + c); }/*from www . j ava2s .com*/ } }
From source file:Main.java
public static void main(String[] args) throws Exception { XPath xpath = XPathFactory.newInstance().newXPath(); String xpathExpression = "/howto/topic/@name"; InputSource inputSource = new InputSource("howto.xml"); NodeList nodes = (NodeList) xpath.evaluate(xpathExpression, inputSource, XPathConstants.NODESET); int j = nodes.getLength(); for (int i = 0; i < j; i++) { System.out.println(nodes.item(i).getTextContent()); }//from ww w .j a v a 2s . c om }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//from www . j a 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")) { element.setAttribute("showPageNumbers", "no"); } } } System.out.println(doc.getDocumentElement()); }
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("myxml.xml"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//" + "item1" + "/*"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; System.out.println(nodes.getLength()); for (int i = 0; i < nodes.getLength(); i++) { Element el = (Element) nodes.item(i); System.out.println("tag: " + el.getNodeName()); if (el.getFirstChild().getNodeType() == Node.TEXT_NODE) System.out.println("inner value:" + el.getFirstChild().getNodeValue()); NodeList children = el.getChildNodes(); for (int k = 0; k < children.getLength(); k++) { Node child = children.item(k); if (child.getNodeType() != Node.TEXT_NODE) { System.out.println("child tag: " + child.getNodeName()); if (child.getFirstChild().getNodeType() == Node.TEXT_NODE) System.out.println("inner child value:" + child.getFirstChild().getNodeValue()); }//from w ww. j a va 2s .c o m } } }
From source file:Main.java
public static void main(String[] args) throws Exception { XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList) xpath.evaluate("/bookshelf/shelf/book/*", new InputSource(Main.class.getResourceAsStream("/books.xml")), XPathConstants.NODESET); System.out.println("First node: " + nodes.item(0)); System.out.println("Second node: " + nodes.item(1)); }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<root>" + "<ctc:BasePrice>" + "<cgc:PriceAmount currencyID='EUR'>18.75</cgc:PriceAmount>" + "<cgc:BaseQuantity quantityUnitCode='EA'>1</cgc:BaseQuantity>" + "</ctc:BasePrice>" + "<ctc:BasePrice>" + "<cgc:PriceAmount currencyID='EUR'>18.25</cgc:PriceAmount>" + "<cgc:BaseQuantity quantityUnitCode='EA'>1</cgc:BaseQuantity>" + "<cgc:MinimumQuantity quantityUnitCode='EA'>3</cgc:MinimumQuantity>" + "</ctc:BasePrice>" + "</root>"; InputStream xmlStream = new ByteArrayInputStream(xml.getBytes()); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document xmlDocument = builder.parse(xmlStream); XPath xPath = XPathFactory.newInstance().newXPath(); String expression = "//*[local-name() = 'BasePrice' and not(descendant::*[local-name() = 'MinimumQuantity'])]/*[local-name()='PriceAmount']"; NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); i++) { System.out.println(nodeList.item(i).getFirstChild().getNodeValue()); }//from w ww . j av a 2 s . c o m }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*w ww . j a v a 2 s. c om*/ 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 Attributes NamedNodeMap namedNodeMap2 = rootElement2.getAttributes(); for (int x = 0; x < namedNodeMap2.getLength(); x++) { Attr importedNode = (Attr) doc1.importNode(namedNodeMap2.item(x), true); rootElement1.setAttributeNodeNS(importedNode); } // Copy Child Nodes NodeList childNodes2 = rootElement2.getChildNodes(); for (int x = 0; x < childNodes2.getLength(); x++) { Node importedNode = doc1.importNode(childNodes2.item(x), true); 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); }