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 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 . ja va 2 s . co m*/ }
From source file:Main.java
public static void main(String argv[]) throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(Main.class.getResourceAsStream("/foo.xml")); NodeList nodeNodeList = document.getElementsByTagName("node"); for (int i = 0; i < nodeNodeList.getLength(); i++) { Node nNode = nodeNodeList.item(i); System.out.println(nNode.getAttributes().getNamedItem("lat").getNodeValue()); System.out.println(nNode.getAttributes().getNamedItem("lon").getNodeValue()); }/*from ww w . j a v a 2 s. com*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document dom = db.parse("data.xml"); Element docEle = dom.getDocumentElement(); NodeList nl = docEle.getElementsByTagName("staff"); if (nl != null && nl.getLength() > 0) { for (int i = 0; i < nl.getLength(); i++) { // get the employee element Element el = (Element) nl.item(i); String firstname = getTextValue(el, "firstname"); String lastname = getTextValue(el, "lastname"); String nickname = getTextValue(el, "nickname"); int salary = getIntValue(el, "salary"); System.out.println(firstname); System.out.println(lastname); System.out.println(nickname); System.out.println(salary); }/*from ww w .j a va 2 s . c om*/ } }
From source file:Main.java
public static void main(String[] args) throws Throwable { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); String entryType = null;/* w ww .j a va2 s . com*/ XPathExpression[] expressions = new XPathExpression[] { xpath.compile("local-name(*[local-name() = 'package'])"), xpath.compile("local-name(*[local-name() = 'libapp'])"), xpath.compile("local-name(*[local-name() = 'module'])") }; DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = fac.newDocumentBuilder(); Document doc = parser.parse(args[0]); for (int i = 0; i < expressions.length; i++) { String found = (String) expressions[i].evaluate(doc.getDocumentElement(), XPathConstants.STRING); entryType = mappings.get(found); if (entryType != null && !entryType.trim().isEmpty()) { break; } } System.out.println(entryType); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true);//w ww. jav a 2 s. c om DocumentBuilder builder; builder = docFactory.newDocumentBuilder(); Document doc = builder.parse(CFG_FILE); XPathExpression expr = XPathFactory.newInstance().newXPath().compile(XPATH_FOR_PRM_MaxThread); Object result = expr.evaluate(doc, XPathConstants.NUMBER); if (result instanceof Double) { System.out.println(((Double) result).intValue()); } }
From source file:Main.java
static public void main(String[] arg) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("foo.xml"); TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); NodeList list = doc.getFirstChild().getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node element = list.item(i).cloneNode(true); if (element.hasChildNodes()) { Source src = new DOMSource(element); FileOutputStream fs = new FileOutputStream("k" + i + ".xml"); Result dest = new StreamResult(fs); aTransformer.transform(src, dest); fs.close();//w w w . j a va 2s .c om } } }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false);/*from w w w. ja v a2 s . c o m*/ DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new FileInputStream(new File("input.xml"))); File OutputDOM = new File("out.txt"); FileOutputStream fostream = new FileOutputStream(OutputDOM); OutputStreamWriter oswriter = new OutputStreamWriter(fostream); BufferedWriter bwriter = new BufferedWriter(oswriter); if (!OutputDOM.exists()) { OutputDOM.createNewFile(); } visitRecursively(doc, bwriter); bwriter.close(); oswriter.close(); fostream.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = factory.newDocumentBuilder(); Document doc;/* w w w . jav a 2s . c o m*/ doc = parser.parse(args[0]); TransformerFactory transFactory = TransformerFactory.newInstance(); System.out.println(transFactory.getClass().getName()); transFactory.setAttribute("indent-number", 2); Transformer idTransform = transFactory.newTransformer(); idTransform.setOutputProperty(OutputKeys.METHOD, "xml"); idTransform.setOutputProperty(OutputKeys.INDENT, "yes"); Source input = new DOMSource(doc); Result output = new StreamResult(System.out); idTransform.transform(input, output); }
From source file:Main.java
License:asdf
public static void main(String args[]) throws Exception { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Create the parser Document xmlDoc = builder.parse(new InputSource(new StringReader(xmlString))); xmlDoc.setXmlVersion("2.0"); }
From source file:Main.java
public static void main(String args[]) throws Exception { File stocks = new File("Stocks.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(stocks); doc.getDocumentElement().normalize(); System.out.println(doc.getDocumentElement().getNodeName()); NodeList nodes = doc.getElementsByTagName("stock"); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; System.out.println("Stock Symbol: " + getValue("symbol", element)); System.out.println("Stock Price: " + getValue("price", element)); System.out.println("Stock Quantity: " + getValue("quantity", element)); }//from w w w . ja v a 2s . c o m } }