List of usage examples for javax.xml.parsers DocumentBuilderFactory newInstance
public static DocumentBuilderFactory newInstance()
From source file:Main.java
public static void main(String[] args) throws Exception { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new ByteArrayInputStream( ("<foo><foo1>Foo Test 1</foo1><foo2><another1><test1>Foo Test 2</test1></another1></foo2><foo3>Foo Test 3</foo3><foo4>Foo Test 4</foo4></foo>") .getBytes())); String xpath = "/" + getXPath(document, "test1"); Node node1 = (Node) XPathFactory.newInstance().newXPath().compile(xpath).evaluate(document, XPathConstants.NODE); Node node2 = (Node) XPathFactory.newInstance().newXPath().compile("//test1").evaluate(document, XPathConstants.NODE); System.out.println(node1.equals(node2)); }
From source file:Main.java
License:asdf
public static void main(String[] args) throws Exception { String xml = "<soapenv:Envelope " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " + "xmlns:ser=\"http://services.web.post.list.com\"><soapenv:Header>" + "<authInfo xsi:type=\"soap:authentication\" " + "xmlns:soap=\"http://list.com/services/SoapRequestProcessor\">" + "<username xsi:type=\"xsd:string\">asdf@g.com</username>" + "<password xsi:type=\"xsd:string\">asdf</password></authInfo></soapenv:Header></soapenv:Envelope>"; System.out.println(xml);// w w w .j a v a2 s .c o m DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(xml))); XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(new NamespaceContext() { @Override public Iterator getPrefixes(String arg0) { return null; } @Override public String getPrefix(String arg0) { return null; } @Override public String getNamespaceURI(String arg0) { if ("soapenv".equals(arg0)) { return "http://schemas.xmlsoap.org/soap/envelope/"; } return null; } }); XPathExpression expr = xpath.compile("/soapenv:Envelope/soapenv:Header/authInfo/password"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; System.out.println("Got " + nodes.getLength() + " nodes"); }
From source file:DOMDump.java
static public void main(String[] arg) { String filename = null;//w w w. j a v a 2s . co m boolean validate = false; if (arg.length == 1) { filename = arg[0]; } else if (arg.length == 2) { if (!arg[0].equals("-v")) usage(); validate = true; filename = arg[1]; } else { usage(); } DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate); dbf.setNamespaceAware(true); dbf.setIgnoringElementContentWhitespace(true); // Parse the input to produce a parse tree with its root // in the form of a Document object Document doc = null; try { DocumentBuilder builder = dbf.newDocumentBuilder(); builder.setErrorHandler(new MyErrorHandler()); InputSource is = new InputSource(filename); doc = builder.parse(is); } catch (SAXException e) { System.exit(1); } catch (ParserConfigurationException e) { System.err.println(e); System.exit(1); } catch (IOException e) { System.err.println(e); System.exit(1); } // Use a TreeDumper to list the tree TreeDumper td = new TreeDumper(); td.dump(doc); }
From source file:DOMCheck.java
static public void main(String[] arg) { String filename = null;//from www . java 2s . c om boolean validate = false; if (arg.length == 1) { filename = arg[0]; } else if (arg.length == 2) { if (!arg[0].equals("-v")) usage(); validate = true; filename = arg[1]; } else { usage(); } // Create a new factory to create parsers that will // be aware of namespaces and will validate or // not according to the flag setting. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(validate); dbf.setNamespaceAware(true); // Use the factory to create a parser (builder) and use // it to parse the document. try { DocumentBuilder builder = dbf.newDocumentBuilder(); builder.setErrorHandler(new MyErrorHandler()); InputSource is = new InputSource(filename); Document doc = builder.parse(is); } catch (SAXException e) { System.exit(1); } catch (ParserConfigurationException e) { System.err.println(e); System.exit(1); } catch (IOException e) { System.err.println(e); System.exit(1); } }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(new InputSource(new StringReader(cfgXml))); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("config"); 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; Config c = new Config(); c.name = eElement.getAttribute("name"); c.type = eElement.getAttribute("type"); c.format = eElement.getAttribute("format"); c.size = Integer.valueOf(eElement.getAttribute("size")); c.scale = Integer.valueOf(eElement.getAttribute("scale")); String attribute = eElement.getAttribute("required"); c.required = Boolean.valueOf("Yes".equalsIgnoreCase(attribute) ? true : false); System.out.println("Imported config : " + c); }/*from w w w . j a va 2 s.co m*/ } }
From source file:com.wrmsr.nativity.x86.App.java
public static void main(String[] args) throws Exception { logger.info("hi"); Document doc;//from w w w.j a v a2 s . c o m try (InputStream is = App.class.getClassLoader().getResourceAsStream("x86reference.xml")) { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); doc = dBuilder.parse(is); } //optional, but recommended //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work doc.getDocumentElement().normalize(); List<Ref.Entry> entries = Lists.newArrayList(); Ref.Parsing.parseRoot(doc, entries); ByteTrie<Ref.Entry> trie = DisImpl.buildTrie(entries); System.out.println(trie.toDetailedString()); System.out.println(); System.out.println(); // Dis.run(trie); Ordering<Pair<Ref.Operand.Type, Ref.Operand.Address>> ord = Ordering.from((o1, o2) -> { int c = ObjectUtils.compare(o1.getLeft(), o2.getLeft()); if (c == 0) { c = ObjectUtils.compare(o1.getRight(), o2.getRight()); } return c; }); Set<Pair<Ref.Operand.Type, Ref.Operand.Address>> set = Sets.newHashSet(); for (Ref.Entry entry : entries) { for (Ref.Syntax syntax : entry.getSyntaxes()) { for (Ref.Operand operand : syntax.getOperands()) { set.add(new ImmutablePair<>(operand.type, operand.address)); } } } for (Pair<Ref.Operand.Type, Ref.Operand.Address> pair : ord.sortedCopy(set)) { System.out.println(pair); } System.out.println("\n"); DisImpl.run(trie); }
From source file:DOMGenerate.java
public static void main(String[] argv) { try {/* ww w .java 2 s. c o m*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElementNS(null, "person"); // Create Root Element Element item = doc.createElementNS(null, "name"); // Create element item.appendChild(doc.createTextNode("Jeff")); root.appendChild(item); // Attach element to Root element item = doc.createElementNS(null, "age"); // Create another Element item.appendChild(doc.createTextNode("28")); root.appendChild(item); // Attach Element to previous element down tree item = doc.createElementNS(null, "height"); item.appendChild(doc.createTextNode("1.80")); root.appendChild(item); // Attach another Element - grandaugther doc.appendChild(root); // Add Root to Document DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer // for the DOM LSOutput out = domImplLS.createLSOutput(); StringWriter stringOut = new StringWriter(); // Writer will be a String out.setCharacterStream(stringOut); ser.write(doc, out); // Serialize the DOM System.out.println("STRXML = " + stringOut.toString()); // Spit out the // DOM as a String } catch (Exception ex) { ex.printStackTrace(); } }
From source file:MainClass.java
public static void main(String args[]) { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setValidating(true); // and validating parser feaures builderFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = null; try {/*from w ww .j a va 2s .co m*/ builder = builderFactory.newDocumentBuilder(); // Create the parser } catch (ParserConfigurationException e) { e.printStackTrace(); } Document xmlDoc = null; try { xmlDoc = builder.parse(new InputSource(new StringReader(xmlString))); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } DocumentType doctype = xmlDoc.getDoctype(); if (doctype == null) { System.out.println("DOCTYPE is null"); } else { System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset()); } System.out.println("\nDocument body contents are:"); listNodes(xmlDoc.getDocumentElement(), ""); // Root element & children }
From source file:Main.java
public static void main(String[] args) throws Exception { SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); SOAPHeader soapHeader = soapEnvelope.getHeader(); SOAPHeaderElement headerElement = soapHeader.addHeaderElement(soapEnvelope.createName("Signature", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12")); SOAPBody soapBody = soapEnvelope.getBody(); soapBody.addAttribute(/*from www .j ava2 s. c o m*/ soapEnvelope.createName("id", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"), "Body"); Name bodyName = soapEnvelope.createName("FooBar", "z", "http://example.com"); SOAPBodyElement gltp = soapBody.addBodyElement(bodyName); Source source = soapPart.getContent(); Node root = null; if (source instanceof DOMSource) { root = ((DOMSource) source).getNode(); } else if (source instanceof SAXSource) { InputSource inSource = ((SAXSource) source).getInputSource(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = null; db = dbf.newDocumentBuilder(); Document doc = db.parse(inSource); root = (Node) doc.getDocumentElement(); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); SOAPHeader soapHeader = soapEnvelope.getHeader(); SOAPHeaderElement headerElement = soapHeader.addHeaderElement(soapEnvelope.createName("Signature", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12")); SOAPBody soapBody = soapEnvelope.getBody(); soapBody.addAttribute(//from w w w. jav a2 s . c o m soapEnvelope.createName("id", "SOAP-SEC", "http://schemas.xmlsoap.org/soap/security/2000-12"), "Body"); Name bodyName = soapEnvelope.createName("FooBar", "z", "http://example.com"); SOAPBodyElement gltp = soapBody.addBodyElement(bodyName); Source source = soapPart.getContent(); Node root = null; if (source instanceof DOMSource) { root = ((DOMSource) source).getNode(); } else if (source instanceof SAXSource) { InputSource inSource = ((SAXSource) source).getInputSource(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = null; db = dbf.newDocumentBuilder(); Document doc = db.parse(inSource); root = (Node) doc.getDocumentElement(); } Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(root), new StreamResult(System.out)); }