List of usage examples for org.xml.sax InputSource InputSource
public InputSource(Reader characterStream)
From source file:Main.java
/** * Helper program: Transforms a String to a XML Document. * * @param InputXMLString the input xml string * @return parsed document//ww w . java 2 s . com * @throws javax.xml.parsers.ParserConfigurationException the parser configuration exception * @throws java.io.IOException Signals that an I/O exception has occurred. */ public static Document String2Doc(String InputXMLString) { try { DocumentBuilder builder = getDocumentBuilder(); InputSource is = new InputSource(new StringReader(InputXMLString)); is.setEncoding("UTF-8"); return builder.parse(is); } catch (Exception e) { System.out.println("cannot parse following content\\n\\n" + InputXMLString); e.printStackTrace(); return null; } }
From source file:Main.java
/** * Parse an XML document.//ww w .ja v a 2 s . com * * @param in * The input stream. * * @return A Document. * * @throws SAXException * If there is a parsing error. * @throws IOException * If there is an I/O error. * @throws IllegalArgumentException * If there is a parser configuration error. */ public static Document parse(final InputStream in) throws SAXException, IOException { return parse(new InputSource(in)); }
From source file:Main.java
public static String prettyXML(String xml, int indent) { try {/*from w w w . j av a2 s. com*/ // Turn xml string into a document Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8")))); // Remove whitespaces outside tags XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); ++i) { Node node = nodeList.item(i); node.getParentNode().removeChild(node); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter stringWriter = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); return stringWriter.toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static Document readXMLFromInputStream(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf;//ww w .j av a 2 s . co m DocumentBuilder db; Document document; dbf = DocumentBuilderFactory.newInstance(); db = dbf.newDocumentBuilder(); Reader reader = new InputStreamReader(inputStream, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); document = db.parse(is); return document; }
From source file:Main.java
public static void parse(final XMLReader xmlReader, final URL url) throws IOException, SAXException { final InputStream inputStream = url.openStream(); try {//from w ww. j a v a2 s . c om final InputSource inputSource = new InputSource(inputStream); xmlReader.parse(inputSource); } finally { inputStream.close(); } }
From source file:Main.java
public static Document read(String filename) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false);//from w w w . j a va 2 s .c om DocumentBuilder builder = null; Document document = null; try { builder = factory.newDocumentBuilder(); builder.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new StringReader("")); } }); document = builder.parse(new File(filename)); document.getDocumentElement().normalize(); } catch (Exception e) { } return document; }
From source file:Main.java
public static String getQuery(String id) throws Exception { InputStream is = new ByteArrayInputStream(xml.getBytes("UTF8")); InputSource inputSource = new InputSource(is); XPath xpath = XPathFactory.newInstance().newXPath(); return xpath.evaluate("/queries/query[@id='" + id + "']", inputSource); }
From source file:Main.java
/** * @param xmlString/*w w w.j a v a 2s .c om*/ * @return * @throws IOException * @throws SAXException * @throws ParserConfigurationException */ public synchronized static Document parse(String xmlString) throws IOException, SAXException, ParserConfigurationException { StringReader reader = new StringReader(xmlString); InputSource inputSource = new InputSource(reader); return getDocumentBuilder().parse(inputSource); }
From source file:Main.java
/** * Parses an XML string.//from w w w .java 2s. c o m * @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)); }
From source file:Main.java
public static Document toDocument(String xml, boolean namespaceAware, boolean ignoreDtd) throws Exception { Reader input = new StringReader(xml); InputSource is = new InputSource(input); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(namespaceAware); // ignore dtd files if (ignoreDtd) { dbf.setValidating(false);//www. ja v a 2s .com dbf.setFeature("http://xml.org/sax/features/namespaces", false); dbf.setFeature("http://xml.org/sax/features/validation", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } DocumentBuilder db = dbf.newDocumentBuilder(); db.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException exception) throws SAXException { throw exception; } public void fatalError(SAXParseException exception) throws SAXException { throw exception; } public void error(SAXParseException exception) throws SAXException { throw exception; } }); return db.parse(is); }