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 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 ww w .j a v a2 s . co m*/ }
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("yourFile.xml")); NodeList nodeList = document.getElementsByTagName("message"); for (int x = 0, size = nodeList.getLength(); x < size; x++) { System.out.println(nodeList.item(x).getAttributes().getNamedItem("to").getNodeValue()); }//from w w w .j av a 2s .co m }
From source file:Main.java
public static void main(String argv[]) throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(Main.class.getResourceAsStream("/foo.xml")); NodeList nodeNodeList = document.getElementsByTagName("node"); for (int i = 0; i < nodeNodeList.getLength(); i++) { Node nNode = nodeNodeList.item(i); System.out.println(nNode.getAttributes().getNamedItem("lat").getNodeValue()); System.out.println(nNode.getAttributes().getNamedItem("lon").getNodeValue()); }/*from ww w .j a v a 2 s . c om*/ }
From source file:MainClass.java
static public void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/*www . j ava 2 s. c o m*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("y.xml"); NodeList configs = doc.getElementsByTagName("C"); for (int i = 0; i < configs.getLength(); i++) { Element config = (Element) configs.item(i); String runMode = config.getAttribute("r").trim(); if (runMode.equals("test")) { NodeList connectionURLs = config.getElementsByTagName("URL"); System.out.println(connectionURLs.item(0).getNodeName() + "=" + connectionURLs.item(0).getFirstChild().getNodeValue()); return; } } }
From source file:Main.java
public static void main(String[] args) throws Exception { InputStream is = new URL("http://www.your server.com/daily.xml").openStream(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(is);//from w ww .ja va 2 s . c o m NodeList nodeList = doc.getElementsByTagName("v"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; if (element.getAttribute("currency").equals("BRL")) { System.out.println(element.getAttribute("rate")); } } } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//from w ww. j ava 2s .co m factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); NodeList list = doc.getElementsByTagName("entry"); for (int i = 0; i < list.getLength(); i++) { Element element = (Element) list.item(i); Comment comment = doc.createComment("index=" + i); element.getParentNode().insertBefore(comment, element.getNextSibling()); } }
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 2s .c o m factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); // Find all elements with the name "entry" and append a comment NodeList list = doc.getElementsByTagName("entry"); for (int i = 0; i < list.getLength(); i++) { Element element = (Element) list.item(i); Comment comment = doc.createComment("index=" + i); // Add the comment after this element element.getParentNode().insertBefore(comment, element.getNextSibling()); } }
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);/* w w w . j ava2 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 { File file = new File("data.xml"); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(file); NodeList nodes = doc.getElementsByTagName("topic"); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); NodeList title = element.getElementsByTagName("title"); Element line = (Element) title.item(0); System.out.println("Title: " + getCharacterDataFromElement(line)); }// w w w .j a va 2 s.com }
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 .c o m factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getDocumentElement(); ProcessingInstruction pi = doc.createProcessingInstruction("target", "instruction"); NodeList list = doc.getElementsByTagName("entry"); for (int i = 0; i < list.getLength(); i++) { element = (Element) list.item(i); pi = doc.createProcessingInstruction("target", "instruction=" + i); element.appendChild(pi); } }