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 { File fXmlFile = new File("data.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("Employee"); for (int temp = 0; temp < nList.getLength(); temp++) { System.out.println(nodeToString(nList.item(temp))); }/*from w w w. j a va 2 s. c om*/ }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder .parse(new InputSource(new InputStreamReader(new FileInputStream("inputFile.xml")))); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.METHOD, "xml"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty("http://xml.apache.org/xslt;indent-amount", "4"); xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); Source source = new DOMSource(document); Result result = new StreamResult(new File("result.xml")); xformer.transform(source, result);/* w w w . jav a2s .c o m*/ }
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_ALL, new ItemFilter(), true); for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) { System.out.println("Element: " + ((Element) n).getTagName()); }//www . ja va 2 s .c o m }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/* w w w . ja v a 2 s . c o m*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("yourFile.xml"); Element rootElement = doc.getDocumentElement(); NodeList children = rootElement.getChildNodes(); Node current = null; for (int i = 0; i < children.getLength(); i++) { current = children.item(i); if (current.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) current; if (element.getTagName().equalsIgnoreCase("tableOfContents")) { rootElement.removeChild(element); } } } System.out.println(doc.getDocumentElement()); }
From source file:Main.java
public static void main(String... args) throws IOException, SAXException, ParserConfigurationException { String xml = "<T rn='0'>" + "<I><P>UNKNOWN</P><K>UNKNOWN</K><N><O>UNKNOWN</O><P>UNKNOWN</P><U>UNKNOWN</U><N>UNKNOWN</N></N></I></T>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8"))); print(doc.getDocumentElement(), ""); }
From source file:Main.java
public static void main(String[] args) throws Exception { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder(); Document document = parser.parse(new File("NewFile.xml")); Schema schema = schemaFactory.newSchema(new File("NewFile.xsd")); Validator validator = schema.newValidator(); validator.validate(new DOMSource(document)); }
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 va 2 s . co m 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[] 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"); }// w w w . ja va 2s . c om 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 argv[]) throws Exception { File myfile = new File("data.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(myfile); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("test"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("First Name : " + getTagValue("id", eElement)); System.out.println("Last Name : " + getTagValue("result", eElement)); }/* ww w. j a v a 2 s .com*/ } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*from w w w .j a v a 2 s . c o m*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("yourFile.xml"); Element rootElement = doc.getDocumentElement(); NodeList children = rootElement.getChildNodes(); Node current = null; int count = children.getLength(); for (int i = 0; i < count; i++) { current = children.item(i); if (current.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) current; if (element.getTagName().equalsIgnoreCase("tableOfContents")) { element.setAttribute("showPageNumbers", "no"); } } } System.out.println(doc.getDocumentElement()); }