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 convertStringToDocument(String domStr) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try {/*from ww w . ja va2 s .com*/ builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(domStr))); return doc; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * * @param xmlContent/*from w w w.ja va 2 s . c o m*/ * @param charset * @return * @throws SAXException * @throws IOException * @throws ParserConfigurationException */ private static Document parse(String xmlContent, Charset charset) throws SAXException, IOException, ParserConfigurationException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(false); documentBuilderFactory.setValidating(false); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(new ByteArrayInputStream(xmlContent.getBytes(charset))); return document; }
From source file:Main.java
public static Object getBean(String args) { try {/*from w w w . j a v a 2s . com*/ DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document doc; doc = builder.parse(new File("config.xml")); NodeList nl = null; Node classNode = null; String cName = null; nl = doc.getElementsByTagName(args); classNode = nl.item(0).getFirstChild(); cName = classNode.getNodeValue(); Class c = Class.forName(cName); Object obj = c.newInstance(); return obj; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
/** * Read an input stream in as an XML document. * @param xmlString/* w ww. jav a2s.c o m*/ * @return the XML document */ public static Document readXmlDocumentFromString(String xmlString) { try { DocumentBuilder documentBuilder = documentBuilderFactory.get().newDocumentBuilder(); return documentBuilder.parse(new InputSource(new StringReader(xmlString))); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } catch (SAXException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Parses a XML document./* w w w. j a va 2 s . c o m*/ * * @param xml the XML document * @return DOM */ public static Document parse(String xml) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(xml))); removeWhitespaces((Element) document.getChildNodes().item(0)); return document; }
From source file:Main.java
/** * Parses an XML file and returns the name of the document element. * @param file the file containing the XML to parse. * @return the name of the document element, or null if the file does not parse. *//*w w w . ja va 2s. c o m*/ public static String getDocumentElementName(File file) { try { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document d = db.parse(file); Element root = d.getDocumentElement(); return root.getTagName(); } catch (Exception e) { return null; } }
From source file:Main.java
/** * This will parse an XML stream and create a DOM document. * * @param is The stream to get the XML from. * @return The DOM document./* w w w. j a va 2 s. c o m*/ * @throws IOException It there is an error creating the dom. */ public static Document parse(InputStream is) throws IOException { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); builderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); builderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); builderFactory.setXIncludeAware(false); builderFactory.setExpandEntityReferences(false); DocumentBuilder builder = builderFactory.newDocumentBuilder(); return builder.parse(is); } catch (FactoryConfigurationError e) { throw new IOException(e.getMessage(), e); } catch (ParserConfigurationException e) { throw new IOException(e.getMessage(), e); } catch (SAXException e) { throw new IOException(e.getMessage(), e); } }
From source file:Main.java
/** * Get the searchHandler Node from the solrconfig.xml file * * @param solrconfig//from www . j a v a 2 s . c o m * the solrconfig.xml File * * @return searchHandler XML Node or null if not found * * @throws ParserConfigurationException * @throws SAXException * @throws IOException * @throws XPathExpressionException */ public static Node getSearchHandlerNode(final File solrconfig) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); final Document docSchem = dBuilder.parse(solrconfig); final XPathFactory xPathfactory = XPathFactory.newInstance(); final XPath xpath = xPathfactory.newXPath(); final XPathExpression expr = xpath .compile("//requestHandler[@class=\"solr.SearchHandler\" and @name=\"/select\"]"); final Node requestHandler = (Node) expr.evaluate(docSchem, XPathConstants.NODE); return requestHandler; }
From source file:Main.java
static public Document getDocument(InputStream stream) throws IOException, SAXException, ParserConfigurationException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(stream); doc.getDocumentElement().normalize(); return doc;/*from www . j av a 2s . c o m*/ }
From source file:Main.java
public static Document read(String path) { Document doc = null;/* w w w .j a v a 2 s.c om*/ try { File file = new File(path); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); if (file.length() != 0) { doc = db.parse(file); } else { return null; } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return doc; }