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 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))); }//ww w.ja v a 2s .com }
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 . jav a 2s . co m*/ }
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()))); modifyingTextbyCuttingandPasting(doc); System.out.println(documentToString(doc)); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.newDocument(); Element book = document.createElement("book"); book.setAttribute("id", "javanut4"); document.appendChild(book);//from w w w. j a va2s. com for (int i = 1; i <= 3; i++) { Element chapter = document.createElement("chapter"); Element title = document.createElement("title"); title.appendChild(document.createTextNode("Chapter " + i)); chapter.appendChild(title); chapter.appendChild(document.createElement("para")); book.appendChild(chapter); } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(System.out); transformer.transform(source, result); }
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"); }//from w w w . j a v a 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 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[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); editTextbyInsertionandReplacement(doc); System.out.println(documentToString(doc)); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true);//from w w w.j a v a 2 s . c om Document doc = factory.newDocumentBuilder().parse(new File("infilename.xml")); }
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()))); dupAttributes(doc);// w w w .ja v a 2s . c om System.out.println(documentToString(doc)); }
From source file:Main.java
public static void main(String[] args) throws Exception { XPath xPath = XPathFactory.newInstance().newXPath(); FileInputStream file = new FileInputStream(new File("c:/data.xml")); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document xmlDocument = builder.parse(file); XPathExpression expr = xPath.compile("//project/*"); NodeList list = (NodeList) expr.evaluate(xmlDocument, XPathConstants.NODESET); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); System.out.println(node.getNodeName() + "=" + node.getTextContent()); }//from w w w. ja v a2 s .co m }