List of usage examples for javax.xml.parsers DocumentBuilder parse
public abstract Document parse(InputSource is) throws SAXException, IOException;
From source file:Main.java
/** * Convert XML string to a XML DOM document * * @param strXML//from www .ja va 2 s.c om * XML * @return XML DOM document * @throws Exception * in error case */ public static Document xmlStringToDOMDocument(String strXML) throws Exception { if (strXML == null) { throw new RuntimeException("No XML input given(null)!"); } StringReader reader = null; Document doc = null; try { reader = new StringReader(strXML); InputSource inputSource = new InputSource(reader); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(inputSource); doc.getDocumentElement().normalize(); } catch (Exception e) { // Logger.XMLEval.logState("Parsing of XML input failed: " + e.getMessage(), LogLevel.Error); throw e; } finally { if (reader != null) { reader.close(); reader = null; } } return doc; }
From source file:Main.java
public static Document parse(InputStream inputStream) throws IOException, ParserConfigurationException, SAXException { DocumentBuilder builder = builderFact.newDocumentBuilder(); return builder.parse(inputStream); }
From source file:Main.java
public static Map<String, String> getConfigs(InputStream is) throws IOException, SAXException, ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document dt = db.parse(is); Element element = dt.getDocumentElement(); Map<String, String> config = new TreeMap<String, String>(); NodeList propertyList = element.getElementsByTagName("property"); int length = propertyList.getLength(); for (int i = 0; i < length; i++) { Node property = propertyList.item(i); String key = property.getChildNodes().item(0).getTextContent(); String value = property.getChildNodes().item(1).getTextContent(); config.put(key, value);//www . ja v a2 s . c o m } return config; }
From source file:Main.java
public static Document loadDocument(DocumentBuilder documentBuilder, File file) throws SAXException, IOException { return documentBuilder.parse(file); }
From source file:Main.java
public static Document parseXml(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputStream); doc.getDocumentElement().normalize(); return doc;/*from w w w . ja va2s.co m*/ }
From source file:Main.java
/** * Read the contents of the specified file into a DOM document * @param f the input XML file/*from w w w .ja v a 2s. c o m*/ * @return the document * @throws Exception */ public static final Document parseFileToDocument(File f) throws Exception { DocumentBuilderFactory docBuilderFact = DocumentBuilderFactory.newInstance(); docBuilderFact.setNamespaceAware(true); DocumentBuilder docBuilder = docBuilderFact.newDocumentBuilder(); Document document = docBuilder.parse(f); return document; }
From source file:Main.java
/** * Creates a document from the given xml bytes. * @return The desired document. Never returns null but throws some Exception. * @throws ParserConfigurationException, IOException, SAXException */// ww w . j a va2 s . c om public static Document getDocument(byte[] xml) throws ParserConfigurationException, IOException, SAXException { if (xml != null) { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // factory.setNamespaceAware( true ); // factory.setValidating( true ); final DocumentBuilder builder = factory.newDocumentBuilder(); final Document document = builder.parse(new ByteArrayInputStream(xml)); return document; } throw new IOException("No xml data"); }
From source file:Main.java
/** * @param is the input stream to read/*from w w w. ja v a2 s. c o m*/ * @return the document contained in that input stream */ public static Document readStream(InputStream is) { ensureFactory(); try { DocumentBuilder builder = mFactory.newDocumentBuilder(); return builder.parse(is); } catch (IOException | ParserConfigurationException | SAXException e) { e.printStackTrace(); return null; } }
From source file:Main.java
/** * Converts a String representing an XML snippet into an {@link org.w3c.dom.Element}. * * @param xml snippet as a string/*from ww w . j a v a2 s .c o m*/ * * @return a DOM Element * * @throws Exception if unable to parse the String or if it doesn't contain valid XML. */ public static Element elementFromString(String xml) throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes("UTF-8")); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(bais); bais.close(); return document.getDocumentElement(); }
From source file:Main.java
public static Document loadDocument(String filePath) throws ParserConfigurationException, SAXException, IOException { File file = new File(filePath); DocumentBuilder documentBuilder = s_documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(file); return document; }