List of usage examples for org.w3c.dom NodeList getLength
public int getLength();
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuild = dbf.newDocumentBuilder(); Document doc = docBuild.parse(new File("test2.xml")); Element x = doc.getDocumentElement(); NodeList m = x.getChildNodes(); for (int i = 0; i < m.getLength(); i++) { Node it = m.item(i);/*from w w w .java 2 s . co m*/ if (it.getNodeType() == 3) { System.out.println(it.getNodeValue()); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<a><o><u>ok</u></o></a>"; Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new InputSource(new StringReader(xml))); NodeList nl = doc.getElementsByTagName("*"); for (int i = 0; i < nl.getLength(); i++) { System.out.println("name is : " + nl.item(i).getNodeName()); }/*from w w w. j a v a 2 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. j a va 2 s .com*/ factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); NodeList list = doc.getElementsByTagName("*"); for (int i = 0; i < list.getLength(); i++) { Element element = (Element) list.item(i); } }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true);/* w w w . j a va 2 s .co 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()))); NodeList list = doc.getElementsByTagName("*"); for (int i = 0; i < list.getLength(); i++) { Element element = (Element) list.item(i); System.out.println(element); }//from ww w. j a va2s. co m }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from www .j a va2s . c o m DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("test.xml"); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); XPathExpression expr = xpath.compile("//element[@key='property1']/text()"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//from ww w . j a va 2 s .com factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element root = null; NodeList list = doc.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { if (list.item(i) instanceof Element) { root = (Element) list.item(i); break; } } root = doc.getDocumentElement(); }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<metadata><codes class = 'class1'>" + "<code code='ABC'>" + "<detail x='blah blah'/>" + "</code>" + "</codes>" + "<codes class = 'class2'>" + "<code code = '123'>" + "<detail x='blah blah'/></code></codes></metadata>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new ByteArrayInputStream(xml.getBytes())); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xPath.compile("//codes/code[@code ='ABC']"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; System.out.println(nodes.getLength() > 0 ? "Yes" : "No"); for (int i = 0; i < nodes.getLength(); i++) { System.out.println("nodes: " + nodes.item(i).getNodeValue()); }/* w ww .j a v a 2 s .c om*/ }
From source file:Main.java
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);// w ww . j a v a 2s .c o m DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse("books.xml"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//numDocs"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); } }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(new File("input.xml")); NodeList nodeList = document.getElementsByTagName("Item"); for (int x = 0, size = nodeList.getLength(); x < size; x++) { System.out.println(nodeList.item(x).getAttributes().getNamedItem("name").getNodeValue()); }/*from w w w . ja va2 s.co m*/ }