List of usage examples for org.xml.sax InputSource InputSource
public InputSource(Reader characterStream)
From source file:Main.java
public static InputSource getInputSource(URL url, InputStream is) throws IOException { // is = new BufferedInputStream(is); // String encoding = getEncoding(is); InputSource inputSource = new InputSource(is); // inputSource.setEncoding(encoding); // [rfeng] Make sure we set the system id as it will be used as the base URI for nested import/include inputSource.setSystemId(url.toString()); return inputSource; }
From source file:Main.java
public static Document getDocument(File file) throws IOException { Document doc = null;/*w ww. j av a2 s . c o m*/ BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); ; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; builder = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(br); doc = builder.parse(inputSource); } catch (Exception e) { System.out.println(e.toString()); } finally { br.close(); br = null; } return doc; }
From source file:Main.java
/** Read an XML file into a DOM tree * //from w ww. ja v a 2 s . c o m * @param file the file to read * @return a new XML document * @throws SAXException if an XML parse error occurs * @throws IOException if a file I/O error occurs */ public static Document read(File file) throws SAXException, IOException { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Workaround for missing external DTD. When the parser looks up // an external reference, we provide an empty document back. While // not correct, we don't expect SVG files loaded by this program // to depend on externally defined entities; nor do we require // validation. // // http://stackoverflow.com/questions/2640825/how-to-parse-a-xhtml-ignoring-the-doctype-declaration-using-dom-parser builder.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new StringReader("")); } }); return builder.parse(file); } catch (ParserConfigurationException e) { // This exception is never thrown, treat as fatal if it is throw new RuntimeException(e); } }
From source file:Main.java
public static Element getElementFromXml(String xmltext) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); StringReader sr = new StringReader(xmltext); InputSource is = new InputSource(sr); Document document = db.parse(is); return document.getDocumentElement(); }
From source file:Main.java
/** * Parses the given input stream and returns the corresponding desirialized * XML document.//from www. jav a 2 s . c o m * * @param reader the reader containing the serialized XML document * @return the deserialized DOM document * @throws Exception */ public static Document readXML(Reader reader) throws Exception { try { DocumentBuilder builder = getDocumentBuilder(); InputSource source = new InputSource(reader); Document doc = builder.parse(source); return doc; } finally { reader.close(); } }
From source file:Main.java
/** * Reads the DOM Document from a XML string *///from w w w.j av a2 s . com public static Document readDocument(final String xml) { return readDocument(new InputSource(new StringReader(xml))); }
From source file:Main.java
/** * Parses the given {@link File} with the specified {@link XMLReader}. * //from ww w .j a v a2 s. c o m * <p>This method assumes the XML is in 'UTF-8', it will not sniff the XML to * determine the encoding to use. * * @param xmlreader The XML reader to use. * @param file The file to parse * * @throws FileNotFoundException If the file does not exists * @throws SAXException Should an SAX exception occur * @throws IOException Should an I/O exception occur */ public static void parse(XMLReader xmlreader, File file) throws FileNotFoundException, SAXException, IOException { // create the input source InputStream bin = new BufferedInputStream(new FileInputStream(file)); Reader reader = new InputStreamReader(bin, "utf-8"); InputSource source = new InputSource(reader); // parse the file xmlreader.parse(source); reader.close(); }
From source file:com.surveypanel.form.xml.ParserHelper.java
/** * Return a reader to parse content/*from ww w .ja v a 2 s . c o m*/ * @param path * @return */ protected static FormValuesHandler getReader(InputStream is) { FormValuesHandler formValuesHandler = new FormValuesHandler(); XMLReader parser = null; try { InputStreamReader inputStreamReader = new InputStreamReader(is, "UTF-8"); parser = XMLReaderFactory.createXMLReader(); parser.setContentHandler(formValuesHandler); parser.setErrorHandler(formValuesHandler); InputSource inputSource = new InputSource(inputStreamReader); parser.parse(inputSource); } catch (Exception e) { e.printStackTrace(); } return formValuesHandler; }
From source file:Main.java
/** * Converts String containing XML code to Document * /*from w w w . j a va 2 s. c o m*/ * @param xmlString * @return <code>Document</code> interface */ public static Document stringToDocument(String xmlString) { if (xmlString == null) return null; DocumentBuilder documentBuilder = getDocumentBuilder(); InputSource inputSource = new InputSource(new StringReader(xmlString)); try { return documentBuilder.parse(inputSource); } catch (SAXException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:Main.java
private static InputSource toInputSource(File xmlFile) { InputSource iso;//from w w w .jav a 2 s . com try { iso = new InputSource(new FileReader(xmlFile)); } catch (FileNotFoundException e) { throw new RuntimeException(e); } return iso; }