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 dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(new InputSource(new StringReader(cfgXml))); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("config"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); System.out.println("\nCurrent Element :" + nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; Config c = new Config(); c.name = eElement.getAttribute("name"); c.type = eElement.getAttribute("type"); c.format = eElement.getAttribute("format"); c.size = Integer.valueOf(eElement.getAttribute("size")); c.scale = Integer.valueOf(eElement.getAttribute("scale")); String attribute = eElement.getAttribute("required"); c.required = Boolean.valueOf("Yes".equalsIgnoreCase(attribute) ? true : false); System.out.println("Imported config : " + c); }/*from w ww.j a va2 s . c o m*/ } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*from w w w . ja v a 2 s . c o m*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("yourFile.xml"); Element rootElement = doc.getDocumentElement(); NodeList children = rootElement.getChildNodes(); Node current = null; int count = children.getLength(); for (int i = 0; i < count; i++) { current = children.item(i); if (current.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) current; if (element.getTagName().equalsIgnoreCase("tableOfContents")) { // Get the list of <tocEntry> items NodeList tocitems = element.getElementsByTagName("tocEntry"); // Obtain a reference to the second one Node secondChild = tocitems.item(1); // Create a new <tocEntry> element Element newTOCItem = doc.createElement("tocEntry"); // Create a new "Help" text node Text newText = doc.createTextNode("Help"); // Make it a child of the new <tocEntry> element // <tocEntry>Help</tocEntry> newTOCItem.appendChild(newText); // Add the new <tocEntry> element to <tableOfContents> element.insertBefore(newTOCItem, secondChild); } } } System.out.println(doc.getDocumentElement()); }
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("in.xml")); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); NodeList nl = doc.getDocumentElement().getChildNodes(); DOMSource source = null;// ww w . j a v a2 s. com for (int x = 0; x < nl.getLength(); x++) { Node e = nl.item(x); if (e instanceof Element) { source = new DOMSource(e); break; } } transformer.transform(source, result); System.out.println(sw.toString()); }
From source file:Main.java
public static void main(String[] args) throws Exception { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource("data.xml")); XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList) xpath.evaluate("//employee/name[text()='old']", doc, XPathConstants.NODESET); for (int idx = 0; idx < nodes.getLength(); idx++) { nodes.item(idx).setTextContent("new value"); }//from ww w. ja va2 s . c o m Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(new DOMSource(doc), new StreamResult(new File("data_new.xml"))); }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<root>" + "<ctc:BasePrice>" + "<cgc:PriceAmount currencyID='EUR'>18.75</cgc:PriceAmount>" + "<cgc:BaseQuantity quantityUnitCode='EA'>1</cgc:BaseQuantity>" + "</ctc:BasePrice>" + "<ctc:BasePrice>" + "<cgc:PriceAmount currencyID='EUR'>18.25</cgc:PriceAmount>" + "<cgc:BaseQuantity quantityUnitCode='EA'>1</cgc:BaseQuantity>" + "<cgc:MinimumQuantity quantityUnitCode='EA'>3</cgc:MinimumQuantity>" + "</ctc:BasePrice>" + "</root>"; InputStream xmlStream = new ByteArrayInputStream(xml.getBytes()); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document xmlDocument = builder.parse(xmlStream); XPath xPath = XPathFactory.newInstance().newXPath(); String expression = "//*[local-name() = 'BasePrice' and not(descendant::*[local-name() = 'MinimumQuantity'])]/*[local-name()='PriceAmount']"; NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); i++) { System.out.println(nodeList.item(i).getFirstChild().getNodeValue()); }//ww w . j a va 2 s .c om }
From source file:Main.java
public static void main(String[] args) throws Exception { XPathFactory xpf = XPathFactory.newInstance(); XPath xPath = xpf.newXPath(); XPathExpression schoolNameExpression = xPath.compile("SchoolName"); XPathExpression classNameExpression = xPath.compile("Classes/Class/ClassName"); InputSource inputSource = new InputSource("input.xml"); NodeList schoolNodes = (NodeList) xPath.evaluate("/Data/Schools/School", inputSource, XPathConstants.NODESET); for (int x = 0; x < schoolNodes.getLength(); x++) { Node schoolElement = schoolNodes.item(x); System.out.println(schoolNameExpression.evaluate(schoolElement, XPathConstants.STRING)); NodeList classNames = (NodeList) classNameExpression.evaluate(schoolElement, XPathConstants.NODESET); for (int y = 0; y < classNames.getLength(); y++) { System.out.println(classNames.item(y).getTextContent()); }/*from w w w . j a v a 2s .c om*/ } }
From source file:Main.java
public static void main(String args[]) throws Exception { FileInputStream fileInputStream = new FileInputStream(new File("src/file.xml")); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document doc1 = builder.parse(fileInputStream); doc1.getDocumentElement().normalize(); NodeList kList1 = doc1.getElementsByTagName("item"); StringBuilder stringBuilder = new StringBuilder(); for (int temp = 0; temp < kList1.getLength(); temp++) { Node kNode1 = kList1.item(temp); System.out.println("\nCurrent Element :" + kNode1.getNodeName()); if (kNode1.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) kNode1; System.out.println("node name" + eElement.getNodeName()); Node in = eElement.getFirstChild(); if ((in.getTextContent() != null) && !(in.getTextContent()).isEmpty() && !(in.getTextContent().length() == 0)) stringBuilder.append(in.getTextContent()); }/*from w w w .ja v a 2s.co m*/ } System.out.println(stringBuilder); }
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("Table.xml")); XPathFactory xFactory = XPathFactory.newInstance(); XPath path = xFactory.newXPath(); XPathExpression exp = path.compile("/tables/table"); NodeList nlTables = (NodeList) exp.evaluate(doc, XPathConstants.NODESET); for (int tblIndex = 0; tblIndex < nlTables.getLength(); tblIndex++) { Node table = nlTables.item(tblIndex); Node nAtt = table.getAttributes().getNamedItem("title"); System.out.println(nAtt.getTextContent()); exp = path.compile("headings/heading"); NodeList nlHeaders = (NodeList) exp.evaluate(table, XPathConstants.NODESET); Set<String> headers = new HashSet<String>(25); for (int index = 0; index < nlHeaders.getLength(); index++) { headers.add(nlHeaders.item(index).getTextContent().trim()); }/*from www . java 2s.c om*/ for (String header : headers) { System.out.println(header); } exp = path.compile("tablebody/tablerow"); NodeList nlRows = (NodeList) exp.evaluate(table, XPathConstants.NODESET); for (int index = 0; index < nlRows.getLength(); index++) { Node rowNode = nlRows.item(index); exp = path.compile("tablecell/item"); NodeList nlValues = (NodeList) exp.evaluate(rowNode, XPathConstants.NODESET); List<String> values = new ArrayList<String>(25); for (int valueIndex = 0; valueIndex < nlValues.getLength(); valueIndex++) { values.add(nlValues.item(valueIndex).getTextContent().trim()); } for (String value : values) { System.out.println(value); } } } }
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)); }//from w w w . jav a 2 s. com }
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")); }//w w w. j a v a 2s. c o m }