List of usage examples for org.w3c.dom Document getElementsByTagName
public NodeList getElementsByTagName(String tagname);
NodeList
of all the Elements
in document order with a given tag name and are contained in the document. 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); 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 ww w. jav a2 s . c o m*/ } } }
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); 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);//w ww .j av a 2 s.c o m } }
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)); }// ww w .j a v a 2 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("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 ww w .j a v a2 s . c o m }
From source file:MainClass.java
static public void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from w ww . j a va2s .c om 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 { 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 a va 2s . c om*/ }
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()); }// w w w. j a v a 2s .c o m } }
From source file:Main.java
public static void main(String arg[]) throws Exception { String xmlRecords = "<data><employee><name>A</name>" + "<title>Manager</title></employee></data>"; DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xmlRecords)); Document doc = db.parse(is); NodeList nodes = doc.getElementsByTagName("employee"); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); NodeList name = element.getElementsByTagName("name"); Element line = (Element) name.item(0); System.out.println("Name: " + getCharacterDataFromElement(line)); NodeList title = element.getElementsByTagName("title"); line = (Element) title.item(0); System.out.println("Title: " + getCharacterDataFromElement(line)); }//from ww w. j av a 2 s.com }
From source file:Main.java
public static void main(String[] args) throws Exception { File CFile = new File("data.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true);/*from w w w . j a v a 2 s.com*/ factory.setIgnoringElementContentWhitespace(true); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(CFile); NodeList pizzas = document.getElementsByTagName("Pizza"); for (int i = 0; i < pizzas.getLength(); i++) { Element pizzaSize = (Element) pizzas.item(i); String pSize = pizzaSize.getAttribute("Size"); if (pSize.equalsIgnoreCase("Large")) System.out.println(10.0); if (pSize.equalsIgnoreCase("Medium")) System.out.println(7.0); if (pSize.equalsIgnoreCase("Small")) System.out.println(5.0); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*ww w . j a v a2 s .co m*/ 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); } }