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 Document getDocument(InputStream xmlInputStream) { Document document = null;/* w w w. j a va2s .c o m*/ try { DocumentBuilder builder = docFactory.newDocumentBuilder(); document = builder.parse(xmlInputStream); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } return document; }
From source file:Main.java
public static Document parseXML(InputStream in) throws IOException, SAXException { Document doc = null;/*from w w w.j a v a 2 s . co m*/ try { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = builder.parse(in); } catch (ParserConfigurationException e) { throw new IOException(e.getMessage()); } catch (FactoryConfigurationError e) { throw new IOException(e.getMessage()); } return doc; }
From source file:Main.java
public static String[] xpathOnString(String q, String stringdoc) { try {//from w w w. j a v a 2 s. co m DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); // domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(stringdoc.getBytes())); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile(q); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; String res[] = new String[nodes.getLength()]; for (int i = 0; i < nodes.getLength(); i++) { // System.out.println(nodes.item(i).toString()); res[i] = nodes.item(i).getNodeValue(); } return res; } catch (Exception e) { System.out.println("XPathUtils.xpathOnString:caught:" + e); return null; } }
From source file:Main.java
public static Document getDocument(String xmlString) { Document document = null;/*from w ww. j a v a 2 s . com*/ try { DocumentBuilder builder = docFactory.newDocumentBuilder(); document = builder.parse(new ByteArrayInputStream(xmlString.getBytes())); document.getDocumentElement().normalize(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } return document; }
From source file:Main.java
public static Document parse(File file) throws IOException { try {/*from w w w . ja v a 2 s . c om*/ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setCoalescing(true); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); factory.setNamespaceAware(true); DocumentBuilder parser = factory.newDocumentBuilder(); return parser.parse(file); } catch (ParserConfigurationException e) { throw new IOException(e); } catch (SAXException e) { throw new IOException(e); } }
From source file:Main.java
public static Document toDocument(InputStream is) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(is); return (document); }
From source file:Main.java
public static Document string2Document(String xml, String encode) throws ParserConfigurationException, UnsupportedEncodingException, SAXException, IOException { Document document = null;//from ww w .j a v a 2 s. co m DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); try { document = builder.parse(new ByteArrayInputStream(xml.getBytes(encode))); } catch (Exception e) { } return document; }
From source file:Main.java
public static Document getDocument(String fileName) { Document document = null;/*from ww w .ja v a 2s . c om*/ try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); document = documentBuilder.parse(fileName); } catch (SAXException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return document; }
From source file:Main.java
/** * Parses an XML file.//w w w . j a va2 s .c om * @param file the file containing the XML to parse. * @return the XML DOM document. */ public static Document getDocument(File file) throws Exception { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return db.parse(file); }
From source file:Main.java
/** * @param resource/*from ww w. j av a 2s .c om*/ * the XML resource * @return {@link Document} * @throws IOException * on exception * @throws ParserConfigurationException * on exception * @throws SAXException * on exception */ public static Document readDocument(final Resource resource) throws IOException, ParserConfigurationException, SAXException { final InputStream is = resource.getInputStream(); if (is != null) { try { final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); final DocumentBuilder db = dbf.newDocumentBuilder(); return db.parse(is); } finally { is.close(); } } return null; }