List of usage examples for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder
public abstract DocumentBuilder newDocumentBuilder() throws ParserConfigurationException;
From source file:Main.java
public static void main(String args[]) throws Exception { File stylesheet = new File("style.xsl"); File xmlSource = new File("data.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(xmlSource); StreamSource stylesource = new StreamSource(stylesheet); Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource); Source source = new DOMSource(document); Result outputTarget = new StreamResult(new File("/tmp/x.csv")); transformer.transform(source, outputTarget); }
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);//ww w. j a v a 2 s .c om traverseLevel(walker, ""); }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<metadata><codes class = 'class1'>" + "<code code='ABC'>" + "<detail x='blah blah'/>" + "</code>" + "</codes>" + "<codes class = 'class2'>" + "<code code = '123'>" + "<detail x='blah blah'/></code></codes></metadata>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new ByteArrayInputStream(xml.getBytes())); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xPath.compile("//codes/code[@code ='ABC']"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; System.out.println(nodes.getLength() > 0 ? "Yes" : "No"); for (int i = 0; i < nodes.getLength(); i++) { System.out.println("nodes: " + nodes.item(i).getNodeValue()); }//from ww w . j av a2 s .c om }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); DocumentTraversal traversal = (DocumentTraversal) document; TreeWalker walker = traversal.createTreeWalker(document.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);/*from ww w .j a v a 2 s .co m*/ traverseLevel(walker, ""); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(new File("data.xml")); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); Element element = (Element) xpath.evaluate("//node3/name", document, XPathConstants.NODE); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); removeAttribute(doc.getDocumentElement(), "attr"); System.out.println(documentToString(doc)); }
From source file:Main.java
License:asdf
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); deleteFirstElement(doc);//from w w w .jav a 2 s.c o m System.out.println(documentToString(doc)); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(new File("filename.xml")); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); Element element = (Element) xpath.evaluate("//*[@id='one']", document, XPathConstants.NODE); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); addText(doc.getDocumentElement(), "new text"); System.out.println(documentToString(doc)); }
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(); Attr orderDate = purchaseOrder.getAttributeNode("date"); System.out.println(orderDate.getValue()); NamedNodeMap attrs = purchaseOrder.getAttributes(); int attrsCount = attrs.getLength(); for (int i = 0; i < attrsCount; i++) { Attr item = (Attr) attrs.item(i); System.out.println("'" + item.getName() + "' = '" + item.getValue() + "'"); }//w w w. j a v a 2s .c o m }