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[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true);/* ww w . j a va 2 s. c o m*/ Document doc = factory.newDocumentBuilder().parse(new File("infilename.xml")); // doc will not contain any Comment nodes }
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 . ja va 2 s. 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"); Element purchaseOrder = document.getDocumentElement(); printElement(purchaseOrder, ""); }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<root>" + "<ctc:BasePrice>" + "<cgc:PriceAmount currencyID='EUR'>18.75</cgc:PriceAmount>" + "<cgc:BaseQuantity quantityUnitCode='EA'>1</cgc:BaseQuantity>" + "</ctc:BasePrice>" + "<ctc:BasePrice>" + "<cgc:PriceAmount currencyID='EUR'>18.25</cgc:PriceAmount>" + "<cgc:BaseQuantity quantityUnitCode='EA'>1</cgc:BaseQuantity>" + "<cgc:MinimumQuantity quantityUnitCode='EA'>3</cgc:MinimumQuantity>" + "</ctc:BasePrice>" + "</root>"; InputStream xmlStream = new ByteArrayInputStream(xml.getBytes()); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document xmlDocument = builder.parse(xmlStream); XPath xPath = XPathFactory.newInstance().newXPath(); String expression = "//*[local-name() = 'BasePrice' and not(descendant::*[local-name() = 'MinimumQuantity'])]/*[local-name()='PriceAmount']"; NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); i++) { System.out.println(nodeList.item(i).getFirstChild().getNodeValue()); }/* w ww . j a va 2 s .c om*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document dom = db.parse("data.xml"); Element docEle = dom.getDocumentElement(); NodeList nl = docEle.getElementsByTagName("staff"); if (nl != null && nl.getLength() > 0) { for (int i = 0; i < nl.getLength(); i++) { // get the employee element Element el = (Element) nl.item(i); String firstname = getTextValue(el, "firstname"); String lastname = getTextValue(el, "lastname"); String nickname = getTextValue(el, "nickname"); int salary = getIntValue(el, "salary"); System.out.println(firstname); System.out.println(lastname); System.out.println(nickname); System.out.println(salary); }/* w ww .j a v a 2s .c o m*/ } }
From source file:Main.java
public static void main(String[] args) throws Throwable { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); String entryType = null;//w w w . j av a2s. co m XPathExpression[] expressions = new XPathExpression[] { xpath.compile("local-name(*[local-name() = 'package'])"), xpath.compile("local-name(*[local-name() = 'libapp'])"), xpath.compile("local-name(*[local-name() = 'module'])") }; DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = fac.newDocumentBuilder(); Document doc = parser.parse(args[0]); for (int i = 0; i < expressions.length; i++) { String found = (String) expressions[i].evaluate(doc.getDocumentElement(), XPathConstants.STRING); entryType = mappings.get(found); if (entryType != null && !entryType.trim().isEmpty()) { break; } } System.out.println(entryType); }
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()))); makeNamelist(doc);// ww w . j a v a 2s .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 document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); Element purchaseOrder = document.getDocumentElement(); printElement(purchaseOrder, ""); }
From source file:Main.java
static public void main(String[] arg) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("foo.xml"); TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); NodeList list = doc.getFirstChild().getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node element = list.item(i).cloneNode(true); if (element.hasChildNodes()) { Source src = new DOMSource(element); FileOutputStream fs = new FileOutputStream("k" + i + ".xml"); Result dest = new StreamResult(fs); aTransformer.transform(src, dest); fs.close();//from www. j av a 2 s .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()))); Map entityValues = new HashMap(); getEntityValues(doc, entityValues);/*from w w w. java 2s .co m*/ NamedNodeMap entities = doc.getDoctype().getEntities(); for (int i = 0; i < entities.getLength(); i++) { Entity entity = (Entity) entities.item(i); System.out.println(entity); String entityName = entity.getNodeName(); System.out.println(entityName); String entityPublicId = entity.getPublicId(); System.out.println(entityPublicId); String entitySystemId = entity.getSystemId(); System.out.println(entitySystemId); Node entityValue = (Node) entityValues.get(entityName); System.out.println(entityValue); } }