List of usage examples for javax.xml.parsers DocumentBuilder parse
public abstract Document parse(InputSource is) throws SAXException, IOException;
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuild = dbf.newDocumentBuilder(); Document doc = docBuild.parse(new File("test2.xml")); Element x = doc.getDocumentElement(); NodeList m = x.getChildNodes(); for (int i = 0; i < m.getLength(); i++) { Node it = m.item(i);//from w w w .j a v a 2 s. c o m if (it.getNodeType() == 3) { System.out.println(it.getNodeValue()); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<?xml version='1.0' encoding='UTF-8'?><users><user id='0' firstname='John'/></users>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(new InputSource(new StringReader(xml))); XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); Element userElement = (Element) xpath.evaluate("/users/user", document, XPathConstants.NODE); System.out.println(userElement.getAttribute("id")); System.out.println(userElement.getAttribute("firstname")); }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<xml xmlns:log='http://sample.com'><test log:writer='someWriter'/></xml>"; StringReader xmlReader = new StringReader(xml); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from www . j ava 2 s . c o m DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(xmlReader)); Element currentNode = (Element) doc.getElementsByTagName("test").item(0); String attributeValue = currentNode.getAttributes().getNamedItemNS("http://sample.com", "writer") .getNodeValue(); System.out.println("Attribute value is " + attributeValue); xmlReader.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document originalDocument = db.parse(new File("input.xml")); Node originalRoot = originalDocument.getDocumentElement(); Document copiedDocument = db.newDocument(); Node copiedRoot = copiedDocument.importNode(originalRoot, true); copiedDocument.appendChild(copiedRoot); }
From source file:Main.java
public static void main(String[] args) throws Exception { String xmlfile = "data.xml"; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(xmlfile); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); Element element = (Element) xpath.evaluate("/root/param[@name='name2']", doc, XPathConstants.NODE); System.out.println(element.getTextContent()); }
From source file:Main.java
public static void main(String[] args) throws Exception { String s = "<p>" + " <media type='audio' id='au008093' rights='aaa'>" + " <title>title</title>" + " this is a test." + "</p>"; InputStream is = new ByteArrayInputStream(s.getBytes()); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document d = db.parse(is); Node rootElement = d.getDocumentElement(); System.out.println(nodeToString(rootElement)); }
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 a va2 s. co m 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 dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(new File("data.xml")); doc.getDocumentElement().normalize(); System.out.println("City: " + doc.getDocumentElement().getChildNodes().item(0).getFirstChild() .getChildNodes().item(0).getAttributes().getNamedItem("data").getNodeValue()); System.out.println("Postal Code: " + doc.getDocumentElement().getChildNodes().item(0).getFirstChild() .getChildNodes().item(1).getAttributes().getNamedItem("data").getNodeValue()); System.out.println("Date: " + doc.getDocumentElement().getChildNodes().item(0).getFirstChild() .getChildNodes().item(4).getAttributes().getNamedItem("data").getNodeValue()); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true);/* www.j a v a 2s. c o m*/ DocumentBuilder builder = docFactory.newDocumentBuilder(); Document doc = builder.parse("data.xml"); XPathExpression expr = XPathFactory.newInstance().newXPath().compile("/script/data"); Object hits = expr.evaluate(doc, XPathConstants.NODESET); if (hits instanceof NodeList) { NodeList list = (NodeList) hits; for (int i = 0; i < list.getLength(); i++) { System.out.println(list.item(i).getTextContent()); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/*from ww w. j ava2 s . c o m*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("test.xml"); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); XPathExpression expr = xpath.compile("//element[@key='property1']/text()"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNodeValue()); } }