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 { 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:MainClass.java
public static void main(String args[]) throws Exception { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); builderFactory.setValidating(true);//from w w w . ja va2s . c o m DocumentBuilder builder = null; builder = builderFactory.newDocumentBuilder(); builder.setErrorHandler(new MainClass()); Document xmlDoc = null; xmlDoc = builder.parse(new File("y.xml")); listNodes(xmlDoc); }
From source file:Main.java
public static void main(String[] args) throws Exception { List<Car> carsList = new ArrayList<Car>(); Set<Car> carsset = new HashSet<Car>(); File fXmlFile = new File("cars.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("cars"); Car tempCar = null;// www . java2 s . c o m for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { tempCar = new Car(); Element eElement = (Element) nNode; tempCar.setModel(eElement.getElementsByTagName("model").item(0).getTextContent()); tempCar.setVersion(eElement.getElementsByTagName("version").item(0).getTextContent()); carsList.add(tempCar); carsset.add(tempCar); } } for (Car cs : carsset) { int count = 0; for (Car cl : carsList) { if (cs.equals(cl)) { count = count + 1; } } System.out.println(cs + "\t" + count); } }
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 .ja va 2 s. c o m*/ System.out.println(documentToString(doc)); }
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
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);/* w w w . j a va2 s .c om*/ DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse("books.xml"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//numDocs"); 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()); } }
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()))); edit(doc);//w w w .j a v a 2s. c o m System.out.println(documentToString(doc)); }
From source file:MainClass.java
public static void main(String[] args) throws ParserConfigurationException, XPathExpressionException, org.xml.sax.SAXException, java.io.IOException { String documentName = args[0]; String expression = args[1];// w w w.j av a 2 s . c o m DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = parser.parse(new java.io.File(documentName)); XPath xpath = XPathFactory.newInstance().newXPath(); System.out.println(xpath.evaluate(expression, doc)); NodeList nodes = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); for (int i = 0, n = nodes.getLength(); i < n; i++) { Node node = nodes.item(i); System.out.println(node); } }
From source file:Main.java
public static void main(String args[]) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element results = doc.createElement("Results"); doc.appendChild(results);//from ww w. ja va 2s .c o m Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager .getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/access.mdb"); ResultSet rs = con.createStatement().executeQuery("select * from product"); ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); while (rs.next()) { Element row = doc.createElement("Row"); results.appendChild(row); for (int i = 1; i <= colCount; i++) { String columnName = rsmd.getColumnName(i); Object value = rs.getObject(i); Element node = doc.createElement(columnName); node.appendChild(doc.createTextNode(value.toString())); row.appendChild(node); } } DOMSource domSource = new DOMSource(doc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); transformer.transform(domSource, sr); System.out.println(sw.toString()); con.close(); rs.close(); }
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()))); append(doc, "newName", "1111111111", "newEmail"); System.out.println(documentToString(doc)); }