List of usage examples for javax.xml.parsers DocumentBuilderFactory newInstance
public static DocumentBuilderFactory newInstance()
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 w w w.j ava 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)); }// w w w . j a v a 2 s . c o m }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder loader = factory.newDocumentBuilder(); Document document = loader.parse("sample.xml"); DocumentTraversal traversal = (DocumentTraversal) document; NodeIterator iterator = traversal.createNodeIterator(document.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);//from w w w. j av a2 s. com for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) { System.out.println("Element: " + ((Element) n).getTagName()); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder loader = factory.newDocumentBuilder(); Document document = loader.parse("sample.xml"); DocumentTraversal traversal = (DocumentTraversal) document; TreeWalker walker = traversal.createTreeWalker(document.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);/*from w ww . j a va 2 s. c om*/ traverseLevel(walker, ""); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder() .parse("http://stackoverflow.com/feeds/tag?tagnames=java&sort=newest"); Element root = doc.getDocumentElement(); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expression = xPath.compile("//entry"); NodeList nl = (NodeList) expression.evaluate(root, XPathConstants.NODESET); System.out.println("Found " + nl.getLength() + " items..."); for (int index = 0; index < nl.getLength(); index++) { Node node = nl.item(index); expression = xPath.compile("title"); Node child = (Node) expression.evaluate(node, XPathConstants.NODE); System.out.println(child.getTextContent()); }/*from w w w .jav a 2 s .c om*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(false); // never forget this! DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("/data.xml"); XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xpath = xPathFactory.newXPath(); XPathExpression xPathExpression = xpath.compile("//city/text()"); Object result = xPathExpression.evaluate(doc, XPathConstants.NODESET); System.out.println(result.toString()); NodeList nodes = (NodeList) result; System.out.println(nodes.getLength()); for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); }//from w w w. jav a 2s. c o m }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder loader = factory.newDocumentBuilder(); Document document = loader.parse("sample.xml"); DocumentTraversal traversal = (DocumentTraversal) document; NodeIterator iterator = traversal.createNodeIterator(document.getDocumentElement(), NodeFilter.SHOW_ALL, new ItemFilter(), true); for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) { System.out.println("Element: " + ((Element) n).getTagName()); }/*from w ww . jav a 2s . c o m*/ }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*from ww w . j ava 2s.com*/ factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Map entityValues = new HashMap(); getEntityValues(doc, entityValues); NamedNodeMap entities = doc.getDoctype().getEntities(); for (int i = 0; i < entities.getLength(); i++) { Entity entity = (Entity) entities.item(i); System.out.println(entity); String entityName = entity.getNodeName(); System.out.println(entityName); String entityPublicId = entity.getPublicId(); System.out.println(entityPublicId); String entitySystemId = entity.getSystemId(); System.out.println(entitySystemId); Node entityValue = (Node) entityValues.get(entityName); System.out.println(entityValue); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder loader = factory.newDocumentBuilder(); Document document = loader.parse("sample.xml"); Element purchaseOrder = document.getDocumentElement(); printElement(purchaseOrder, ""); }
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);//from www. j av a 2s . c om 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")); } } } }