List of usage examples for javax.xml.parsers DocumentBuilderFactory newInstance
public static DocumentBuilderFactory newInstance()
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()))); NamedNodeMap notations = doc.getDoctype().getNotations(); for (int i = 0; i < notations.getLength(); i++) { Notation notation = (Notation) notations.item(i); String notationName = notation.getNodeName(); System.out.println(notationName); String notationPublicId = notation.getPublicId(); String notationSystemId = notation.getSystemId(); }//from www.jav a 2 s. com }
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()))); Element element = (Element) doc.getElementsByTagName("x").item(0); Node parent = element.getParentNode(); parent.removeChild(element);/*from w w w. j a v a2s.c om*/ parent.normalize(); System.out.println(parent); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); Element e = document.getDocumentElement(); System.out.println(e);/* w w w .ja v a 2 s. c o m*/ }
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)); }//from w w w .java 2 s . co m } }
From source file:Main.java
public static void main(String[] args) throws Exception { String xmlfile = "data.xml"; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(xmlfile); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); Element element = (Element) xpath.evaluate("/root/param[@name='name2']", doc, XPathConstants.NODE); System.out.println(element.getTextContent()); }
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)); }/* ww w . ja v a 2 s .c om*/ } }
From source file:TestDOM.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = factory.newDocumentBuilder(); Document document = parser.parse("zooinventory.xml"); Element inventory = document.getDocumentElement(); NodeList animals = inventory.getElementsByTagName("Animal"); System.out.println("Animals = "); for (int i = 0; i < animals.getLength(); i++) { String name = DOMUtil.getSimpleElementText((Element) animals.item(i), "Name"); String species = DOMUtil.getSimpleElementText((Element) animals.item(i), "Species"); System.out.println(" " + name + " (" + species + ")"); }//from w w w . ja v a 2s .c o m Element foodRecipe = DOMUtil.getFirstElement((Element) animals.item(1), "FoodRecipe"); String name = DOMUtil.getSimpleElementText(foodRecipe, "Name"); System.out.println("Recipe = " + name); NodeList ingredients = foodRecipe.getElementsByTagName("Ingredient"); for (int i = 0; i < ingredients.getLength(); i++) System.out.println(" " + DOMUtil.getSimpleElementText((Element) ingredients.item(i))); }
From source file:Main.java
public static void main(String argv[]) 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("staff"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); System.out.println("\nCurrent Element :" + nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("Staff id : " + eElement.getAttribute("id")); System.out.println(// www . j av a 2 s . co m "First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent()); System.out.println( "Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent()); System.out.println( "Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent()); System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent()); } } }
From source file:Main.java
public static void main(String[] args) throws Throwable { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);// w w w . jav a 2s . c o m DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElement("root"); root.setAttribute("xmlns:m", "http://www.java2s.com/blog"); root.setAttribute("xmlns:rt", "http://www.java2s.com/forum"); doc.appendChild(root); Element elt = doc.createElement("simple"); elt.setAttribute("m:Path", "false"); elt.setAttribute("m:Content", "false"); elt.setAttribute("rt:file", "false"); root.appendChild(doc.createTextNode("\n\t")); root.appendChild(elt); root.appendChild(doc.createTextNode("\n")); TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(System.out)); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/*from w w w. j a v a 2 s. co m*/ DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("test.xml"); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); XPathExpression expr = xpath.compile("//element[@key='property1']/text()"); 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()); } }