List of usage examples for javax.xml.parsers DocumentBuilderFactory newInstance
public static DocumentBuilderFactory newInstance()
From source file:MainClass.java
public static void main(String[] args) throws IOException, ParserConfigurationException, org.xml.sax.SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true);//from w ww . j a va 2s . co m factory.setCoalescing(true); // Convert CDATA to Text nodes factory.setNamespaceAware(false); // No namespaces: this is default factory.setValidating(false); // Don't validate DTD: also default DocumentBuilder parser = factory.newDocumentBuilder(); Document document = parser.parse(new File(args[0])); NodeList sections = document.getElementsByTagName("sect1"); int numSections = sections.getLength(); for (int i = 0; i < numSections; i++) { Element section = (Element) sections.item(i); // A <sect1> Node title = section.getFirstChild(); while (title != null && title.getNodeType() != Node.ELEMENT_NODE) title = title.getNextSibling(); if (title != null) System.out.println(title.getFirstChild().getNodeValue()); } }
From source file:Main.java
public static void main(String[] args) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = factory.newDocumentBuilder(); Document xmlDoc = docBuilder.parse(new File("sample.xml")); NodeList nodes = xmlDoc.getElementsByTagName("fr"); for (int i = 0, length = nodes.getLength(); i < length; i++) { ((Element) nodes.item(i)).setTextContent("Modified"); }//ww w .java 2 s.c o m xmlDoc.getDocumentElement().normalize(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource domSource = new DOMSource(xmlDoc); StreamResult result = new StreamResult(new File("sample.xml")); transformer.transform(domSource, result); System.out.println("Modification Done"); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = factory.newDocumentBuilder(); Document doc;//from w w w . ja v a2s. co 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
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/* ww w .j a va 2 s . c o m*/ DocumentBuilder builder; Document doc = null; builder = factory.newDocumentBuilder(); doc = builder.parse("employees.xml"); // Create XPathFactory object XPathFactory xpathFactory = XPathFactory.newInstance(); // Create XPath object XPath xpath = xpathFactory.newXPath(); String name = getEmployeeNameById(doc, xpath, 4); System.out.println("Employee Name with ID 4: " + name); List<String> names = getEmployeeNameWithAge(doc, xpath, 30); System.out.println("Employees with 'age>30' are:" + Arrays.toString(names.toArray())); List<String> femaleEmps = getFemaleEmployeesName(doc, xpath); System.out.println("Female Employees names are:" + Arrays.toString(femaleEmps.toArray())); }
From source file:Main.java
public static void main(String arg[]) throws Exception { String xmlRecords = "<data><employee><name>A</name>" + "<title>Manager</title></employee></data>"; DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xmlRecords)); Document doc = db.parse(is);// w ww . j av a 2s . c o m NodeList nodes = doc.getElementsByTagName("employee"); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); NodeList name = element.getElementsByTagName("name"); Element line = (Element) name.item(0); System.out.println("Name: " + getCharacterDataFromElement(line)); NodeList title = element.getElementsByTagName("title"); line = (Element) title.item(0); System.out.println("Title: " + getCharacterDataFromElement(line)); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);// w ww . ja va 2s .c o m DocumentBuilder loader = factory.newDocumentBuilder(); Document document = loader.newDocument(); edit(document); System.out.println(documentToString(document)); }
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 { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource("data.xml")); XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList) xpath.evaluate("//employee/name[text()='old']", doc, XPathConstants.NODESET); for (int idx = 0; idx < nodes.getLength(); idx++) { nodes.item(idx).setTextContent("new value"); }//from w w w . j a v a 2 s. c o m Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(new DOMSource(doc), new StreamResult(new File("data_new.xml"))); }
From source file:Main.java
public static void main(String[] args) throws Exception { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new File("data.xsd")); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/* w ww.j a v a2s . c o m*/ dbf.setSchema(schema); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(new File("data.xml")); Element result = document.getElementById("abc"); System.out.println(result); }
From source file:Main.java
static public void main(String[] arg) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(true);//w ww. ja v a 2 s. c o m dbf.setNamespaceAware(true); dbf.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = dbf.newDocumentBuilder(); StringReader sr = new StringReader("<tag>java2s.com</tag>"); Document document = builder.parse(new InputSource(sr)); deleteFirstElement(document); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); StringWriter sw = new StringWriter(); trans.transform(new DOMSource(document), new StreamResult(sw)); System.out.println(sw.toString()); }