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 docBuilder = dbf.newDocumentBuilder(); Document doc = docBuilder.parse(new FileInputStream("data.xml")); Element root = doc.getDocumentElement(); org.w3c.dom.NodeList nodeList = root.getElementsByTagName("key"); for (int i = 0; i < nodeList.getLength(); i++) { System.out.print(((Node) nodeList.item(i)).getAttributes().getNamedItem("keyname")); System.out.println("\tvalue: " + ((Node) nodeList.item(i)).getTextContent()); }//from ww w . j a v a2 s .c o m }
From source file:Main.java
public static void main(String[] args) throws Exception { List<String> names = new ArrayList<>(); URL oracle = new URL("http://weather.yahooapis.com/forecastrss?w=2502265"); InputStream is = oracle.openStream(); DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);/*from www. ja va 2 s.com*/ DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(is); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//*:*/@*"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nl = (NodeList) result; for (int i = 0; i < nl.getLength(); i++) { names.add(nl.item(i).getNodeName()); Node node = nl.item(i); String path = "." + node.getNodeName() + " = " + node.getNodeValue(); node = ((Attr) node).getOwnerElement(); while (node != null) { path = node.getNodeName() + '/' + path; node = node.getParentNode(); } System.out.println(path); } }
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()); }// w ww. j a va 2 s.c o m } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("Test.xml")); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression exp = xPath.compile("//data"); NodeList nl = (NodeList) exp.evaluate(doc, XPathConstants.NODESET); System.out.println("Found " + nl.getLength() + " results"); }
From source file:Main.java
public static void main(String[] args) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = factory.newDocumentBuilder(); Document xmlDoc = docBuilder.parse(new File("sample.xml")); NodeList nodes = xmlDoc.getElementsByTagName("fr"); for (int i = 0, length = nodes.getLength(); i < length; i++) { ((Element) nodes.item(i)).setTextContent("Modified"); }/*ww w. j a v a 2s . c o m*/ xmlDoc.getDocumentElement().normalize(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource domSource = new DOMSource(xmlDoc); StreamResult result = new StreamResult(new File("sample.xml")); transformer.transform(domSource, result); System.out.println("Modification Done"); }
From source file:Main.java
public static void main(String args[]) throws IOException, SAXException { DOMParser parser = new DOMParser(); parser.parse("games.xml"); Document dom = parser.getDocument(); NodeList games = dom.getElementsByTagName("game"); for (int i = 0; i < games.getLength(); i++) { Node aNode = games.item(i); System.out.println(aNode.getFirstChild().getNodeValue()); NamedNodeMap attributes = aNode.getAttributes(); for (int a = 0; a < attributes.getLength(); a++) { Node theAttribute = attributes.item(a); System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue()); }/*from ww w . j a v a2 s . c om*/ } }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(new File("D:/test.xml")); NodeList elt = doc.getElementsByTagName("EMPLOYEE"); for (int k = 0; k < elt.getLength(); k++) { Node firstNode3 = elt.item(k); Element elt1 = (Element) firstNode3; String att = elt1.getAttribute("PERMANENT"); System.out.println("\n\nPERMANENT: " + att); NodeList nodes = elt1.getElementsByTagName("DETAILS"); for (int i = 0; i < nodes.getLength(); i++) { Node childNode = nodes.item(i); Element elt2 = (Element) childNode; System.out.println("---" + elt2.getNodeName()); System.out.println("NAME:" + elt2.getAttribute("NAME")); System.out.println("ID:" + elt2.getAttribute("ID")); System.out.println("AGE:" + elt2.getAttribute("AGE")); }/*ww w. j a v a2 s. co m*/ } }
From source file:Main.java
public static void main(String[] args) throws Exception { File fXmlFile = new File("data.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("Employee"); for (int temp = 0; temp < nList.getLength(); temp++) { System.out.println(nodeToString(nList.item(temp))); }// w w w. ja va 2s . c om }
From source file:Main.java
public static void main(String args[]) throws Exception { File stocks = new File("Stocks.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(stocks); doc.getDocumentElement().normalize(); System.out.println(doc.getDocumentElement().getNodeName()); NodeList nodes = doc.getElementsByTagName("stock"); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; System.out.println("Stock Symbol: " + getValue("symbol", element)); System.out.println("Stock Price: " + getValue("price", element)); System.out.println("Stock Quantity: " + getValue("quantity", element)); }//from w w w .ja va 2 s . c o m } }
From source file:Main.java
public static void main(String argv[]) throws Exception { File myfile = new File("data.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(myfile); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("test"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("First Name : " + getTagValue("id", eElement)); System.out.println("Last Name : " + getTagValue("result", eElement)); }/*from www . j a v a 2s . com*/ } }