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 createXmlDoc(InputStream is) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(is); return doc;/*from www. j a v a 2s .c o m*/ }
From source file:Main.java
/** * Creates a DOM from a file representation of an xml record * /* ww w.ja v a2s .c o m*/ * @param reader * the xml reader * @return the DOM document */ public static Document parseDom(Reader reader) { try { DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(new org.xml.sax.InputSource(reader)); } catch (Exception e) { throw new RuntimeException("Could not parse DOM for '" + reader.toString() + "'!", e); } }
From source file:Main.java
public static Document getDocument(final String xml) throws ParserConfigurationException, SAXException, IOException { final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);//from w w w . j a v a 2 s. c o m final DocumentBuilder builder = domFactory.newDocumentBuilder(); return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8"))); }
From source file:Main.java
public static Document returnDocFromString(String str) throws ParserConfigurationException, UnsupportedEncodingException, SAXException, IOException { Document document = null;/*from w w w . j a va2s . c o m*/ DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); document = db.parse(new ByteArrayInputStream(str.getBytes("UTF-8"))); return document; }
From source file:Main.java
public static Object getBean() { try {/*from ww w .jav a2 s .c o m*/ DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document doc = builder.parse(new File("src/config.xml")); NodeList nl = doc.getElementsByTagName("className"); Node node = nl.item(0).getFirstChild(); String cName = node.getNodeValue(); Class clazz = Class.forName(cName); Object obj = clazz.newInstance(); return obj; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static Document returnDocFromStringUTF16(String str) throws ParserConfigurationException, UnsupportedEncodingException, SAXException, IOException { Document document = null;//from w w w . j a va 2 s.co m DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); document = db.parse(new ByteArrayInputStream(str.getBytes("utf-16"))); return document; }
From source file:Main.java
/** * Creates W3C DOM Document object from XML file * @param filePath path to xml file including file name and extension *//*from ww w . jav a 2s . c o m*/ public static Document parseXml(String filePath) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(filePath); doc.getDocumentElement().normalize(); return doc; } catch (ParserConfigurationException pce) { pce.printStackTrace(); return null; } catch (SAXException se) { se.printStackTrace(); return null; } catch (IOException ioe) { ioe.printStackTrace(); return null; } }
From source file:Main.java
public static Document parse(InputStream anInputStream) { Document document;/*from ww w . ja v a2s . c o m*/ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(anInputStream); } catch (Exception e) { throw new RuntimeException(e); } return document; }
From source file:Main.java
public static Document parseDom(InputStream byteArrayInputStream) { try {//from ww w. j av a2s. c o m DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(new org.xml.sax.InputSource(byteArrayInputStream)); } catch (Exception e) { throw new RuntimeException("Could not parse DOM for '" + byteArrayInputStream.toString() + "'!", e); } }
From source file:Main.java
/** * Parses an XML string.//w ww .j a va 2 s .c om * @param xmlString the file containing the XML to parse. * @return the XML DOM document. */ public static Document getDocumentFromString(String xmlString) throws Exception { StringReader sr = new StringReader(xmlString); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return db.parse(new InputSource(sr)); }