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 { XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader("tds.xml")), XPathConstants.NODESET); for (int i = 0; i < shows.getLength(); i++) { Element show = (Element) shows.item(i); String guestName = xPath.evaluate("guest/name", show); System.out.println(guestName); System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date")); }//from w ww .j a v a2 s .c om }
From source file:GuestList.java
public static void main(String[] args) throws Exception { XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader("tds.xml")), XPathConstants.NODESET); for (int i = 0; i < shows.getLength(); i++) { Element show = (Element) shows.item(i); String guestName = xPath.evaluate("guest/name", show); String guestCredit = xPath.evaluate("guest/credit", show); System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date") + " - " + guestName + " (" + guestCredit + ")"); }/*from w w w. j a v a 2 s . c om*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { List<Car> carsList = new ArrayList<Car>(); Set<Car> carsset = new HashSet<Car>(); File fXmlFile = new File("cars.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("cars"); Car tempCar = null;/*from www. j a va 2s. c o m*/ for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { tempCar = new Car(); Element eElement = (Element) nNode; tempCar.setModel(eElement.getElementsByTagName("model").item(0).getTextContent()); tempCar.setVersion(eElement.getElementsByTagName("version").item(0).getTextContent()); carsList.add(tempCar); carsset.add(tempCar); } } for (Car cs : carsset) { int count = 0; for (Car cl : carsList) { if (cs.equals(cl)) { count = count + 1; } } System.out.println(cs + "\t" + count); } }
From source file:Main.java
public static void main(String[] args) throws Exception { InputSource source = new InputSource(new StringReader("<root>\n" + "<field name='firstname'>\n" + " <value>John</value>\n" + "</field>\n" + "<field name='lastname'>\n" + " <value>Citizen</value>\n" + "</field>\n" + "<field name='DoB'>\n" + " <value>01/01/1980</value>\n" + "</field>\n" + "<field name='Profession'>\n" + " <value>Manager</value>\n" + "</field>\n" + "</root>")); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); Document document = documentBuilder.parse(source); NodeList allFields = (NodeList) document.getElementsByTagName("field"); Map<String, String> data = new HashMap<>(); for (int i = 0; i < allFields.getLength(); i++) { Element field = (Element) allFields.item(i); String nameAttribute = field.getAttribute("name"); Element child = (Element) field.getElementsByTagName("value").item(0); String value = child.getTextContent(); data.put(nameAttribute, value);// w w w .j a v a2 s. c o m } for (Map.Entry field : data.entrySet()) { System.out.println(field.getKey() + ": " + field.getValue()); } }
From source file:XMLInfo.java
public static void main(String args[]) { try {//from ww w. j a va2s . co m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("xmlFileName.xml"); Node root = document.getDocumentElement(); System.out.print("Here is the document's root node:"); System.out.println(" " + root.getNodeName()); System.out.println("Here are its child elements: "); NodeList childNodes = root.getChildNodes(); Node currentNode; for (int i = 0; i < childNodes.getLength(); i++) { currentNode = childNodes.item(i); System.out.println(currentNode.getNodeName()); } // get first child of root element currentNode = root.getFirstChild(); System.out.print("The first child of root node is: "); System.out.println(currentNode.getNodeName()); // get next sibling of first child System.out.print("whose next sibling is: "); currentNode = currentNode.getNextSibling(); System.out.println(currentNode.getNodeName()); // print value of next sibling of first child System.out.println("value of " + currentNode.getNodeName() + " element is: " + currentNode.getFirstChild().getNodeValue()); // print name of parent of next sibling of first child System.out.print("Parent node of " + currentNode.getNodeName() + " is: " + currentNode.getParentNode().getNodeName()); } // handle exception creating DocumentBuilder catch (ParserConfigurationException parserError) { System.err.println("Parser Configuration Error"); parserError.printStackTrace(); } // handle exception reading data from file catch (IOException fileException) { System.err.println("File IO Error"); fileException.printStackTrace(); } // handle exception parsing XML document catch (SAXException parseException) { System.err.println("Error Parsing Document"); parseException.printStackTrace(); } }
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);/*from ww w .j av a 2 s .c om*/ 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 { XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xPath = xPathFactory.newXPath(); InputSource doc = new InputSource(new InputStreamReader(new FileInputStream(new File("file.xml")))); String expression = "//Home/data"; XPathExpression xPathExpression = xPath.compile(expression); NodeList elem1List = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET); xPathExpression = xPath.compile("@type"); for (int i = 0; i < elem1List.getLength(); i++) { System.out.println(xPathExpression.evaluate(elem1List.item(i), XPathConstants.STRING)); }/*w ww .java 2 s . c om*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document dom = db.parse("data.xml"); Element docEle = dom.getDocumentElement(); NodeList nl = docEle.getElementsByTagName("staff"); if (nl != null && nl.getLength() > 0) { for (int i = 0; i < nl.getLength(); i++) { // get the employee element Element el = (Element) nl.item(i); String firstname = getTextValue(el, "firstname"); String lastname = getTextValue(el, "lastname"); String nickname = getTextValue(el, "nickname"); int salary = getIntValue(el, "salary"); System.out.println(firstname); System.out.println(lastname); System.out.println(nickname); System.out.println(salary); }/*from w ww. ja v a2 s .c o m*/ } }
From source file:edu.ucsb.cs.eager.sa.cerebro.ProfessorX.java
public static void main(String[] args) { Options options = new Options(); options.addOption("i", "input-file", true, "Path to input xml file"); options.addOption("r", "root-path", true, "Root path of all Git repositories"); CommandLine cmd;//from w w w. ja v a2 s . c o m try { CommandLineParser parser = new BasicParser(); cmd = parser.parse(options, args); } catch (ParseException e) { System.err.println("Error: " + e.getMessage() + "\n"); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("Cerebro", options); return; } String inputFileName = cmd.getOptionValue("i"); if (inputFileName == null) { System.err.println("input file path is required"); return; } String rootPath = cmd.getOptionValue("r"); if (rootPath == null) { System.err.println("root path is required"); return; } File inputFile = new File(inputFileName); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); Document doc; try { DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); doc = dBuilder.parse(inputFile); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); return; } NodeList repoList = doc.getElementsByTagName("repo"); for (int i = 0; i < repoList.getLength(); i++) { Element repo = (Element) repoList.item(i); String name = repo.getElementsByTagName("name").item(0).getTextContent(); String classPath = repo.getElementsByTagName("classpath").item(0).getTextContent(); Set<String> classes = new LinkedHashSet<String>(); NodeList classesList = repo.getElementsByTagName("classes").item(0).getChildNodes(); for (int j = 0; j < classesList.getLength(); j++) { if (!(classesList.item(j) instanceof Element)) { continue; } classes.add(classesList.item(j).getTextContent()); } analyzeRepo(rootPath, name, classPath, classes); } }
From source file:TestDOM.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = factory.newDocumentBuilder(); Document document = parser.parse("zooinventory.xml"); Element inventory = document.getDocumentElement(); NodeList animals = inventory.getElementsByTagName("Animal"); System.out.println("Animals = "); for (int i = 0; i < animals.getLength(); i++) { String name = DOMUtil.getSimpleElementText((Element) animals.item(i), "Name"); String species = DOMUtil.getSimpleElementText((Element) animals.item(i), "Species"); System.out.println(" " + name + " (" + species + ")"); }// w w w . j a va2 s . c o m Element foodRecipe = DOMUtil.getFirstElement((Element) animals.item(1), "FoodRecipe"); String name = DOMUtil.getSimpleElementText(foodRecipe, "Name"); System.out.println("Recipe = " + name); NodeList ingredients = foodRecipe.getElementsByTagName("Ingredient"); for (int i = 0; i < ingredients.getLength(); i++) System.out.println(" " + DOMUtil.getSimpleElementText((Element) ingredients.item(i))); }