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 { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("path/to/file.xml")); XPathFactory xFactory = XPathFactory.newInstance(); XPath xPath = xFactory.newXPath(); XPathExpression expression = xPath.compile("PersonList/Person/Age/text() | PersonList/Person/Name/text()"); NodeList nodes = (NodeList) expression.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getParentNode().getNodeName().equals("Name")) { node.setNodeValue("new name"); } else {/*from w w w . j a v a2s . c om*/ node.setNodeValue("42"); } } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//from ww w . j a v a 2s.com DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("yourFile.xml"); Element rootElement = doc.getDocumentElement(); NodeList children = rootElement.getChildNodes(); Node current = null; for (int i = 0; i < children.getLength(); i++) { current = children.item(i); if (current.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) current; if (element.getTagName().equalsIgnoreCase("tableOfContents")) { rootElement.removeChild(element); } } } System.out.println(doc.getDocumentElement()); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().parse(new File("Sample.xml")); XPathFactory xFactory = XPathFactory.newInstance(); XPath xPath = xFactory.newXPath(); XPathExpression exp = xPath.compile( "/article/body/section/region[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'perfect')]"); NodeList nl = (NodeList) exp.evaluate(doc.getFirstChild(), XPathConstants.NODESET); for (int index = 0; index < nl.getLength(); index++) { Node node = nl.item(index); System.out.println(node.getTextContent()); }/*from ww w. j a v a2s. c o 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))); }//from w w w . j av a2s . c o m }
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 ww w .j a v a2 s. co m*/ 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[] args) throws Exception { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setValidating(false);/*from ww w .j av a 2s .c o m*/ domFactory.setNamespaceAware(true); domFactory.setIgnoringComments(true); domFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document dDoc = builder.parse("C:/data.xsd"); Node rootNode = dDoc.getElementsByTagName("xs:schema").item(0); System.out.println(rootNode.getNodeName()); XPath xPath1 = XPathFactory.newInstance().newXPath(); NamespaceContext nsContext = new NamespaceContext() { @Override public String getNamespaceURI(String prefix) { return "http://www.w3.org/2001/XMLSchema"; } @Override public String getPrefix(String namespaceURI) { return "xs"; } @Override public Iterator getPrefixes(String namespaceURI) { Set s = new HashSet(); s.add("xs"); return s.iterator(); } }; xPath1.setNamespaceContext((NamespaceContext) nsContext); NodeList nList1 = (NodeList) xPath1.evaluate("//xs:schema", dDoc, XPathConstants.NODESET); System.out.println(nList1.item(0).getNodeName()); NodeList nList2 = (NodeList) xPath1.evaluate("//xs:element", rootNode, XPathConstants.NODESET); System.out.println(nList2.item(0).getNodeName()); }
From source file:Main.java
public static void main(String[] args) throws Exception { XPath xPath = XPathFactory.newInstance().newXPath(); FileReader reader = new FileReader("input.xml"); InputSource xml = new InputSource(reader); NodeList titleNodes = (NodeList) xPath.evaluate("//item/title", xml, XPathConstants.NODESET); for (int x = 0; x < titleNodes.getLength(); x++) { System.out.println(titleNodes.item(x).getTextContent()); }//from w w w .ja v a2s.c o m }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = db.parse(new InputSource( new StringReader("<emp><empname><firstName></firstName><lastName></lastName></empname></emp>"))); NodeList customerNodes = doc.getElementsByTagName("empname"); for (int i = 0; i < customerNodes.getLength(); i++) { NodeList children = customerNodes.item(i).getChildNodes(); for (int j = 0; j < children.getLength(); j++) { String childNode = children.item(j).getNodeName(); if (childNode.equalsIgnoreCase("firstName")) { children.item(j).setTextContent(String.valueOf("John")); } else if (childNode.equalsIgnoreCase("lastName")) { children.item(j).setTextContent(String.valueOf("Doe")); }//from www. ja v a 2 s . c om } } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); System.out.println(writer.getBuffer().toString()); }
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);/*from www.j ava 2 s.co m*/ 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 source file:MainClass.java
public static void main(String[] args) throws ParserConfigurationException, XPathExpressionException, org.xml.sax.SAXException, java.io.IOException { String documentName = args[0]; String expression = args[1];/*from w ww . j a v a2s . co m*/ DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = parser.parse(new java.io.File(documentName)); XPath xpath = XPathFactory.newInstance().newXPath(); System.out.println(xpath.evaluate(expression, doc)); NodeList nodes = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); for (int i = 0, n = nodes.getLength(); i < n; i++) { Node node = nodes.item(i); System.out.println(node); } }