List of usage examples for org.xml.sax InputSource InputSource
public InputSource(Reader characterStream)
From source file:Main.java
/** * parse document from xml String//from w w w . j a va 2 s. com * * @param xml xml string * @throws IOException if failed to create XML document * @throws ParserConfigurationException e * @throws SAXException e */ public static Document loadDocument(String xml) throws IOException, ParserConfigurationException, SAXException { return dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xml))); }
From source file:Main.java
public static Document documentFromString(final String documentContents) throws Exception { return createDocumentBuilder().parse(new InputSource(new StringReader(documentContents))); }
From source file:Main.java
public static Document xmlFromString(final String data) throws RuntimeException { if (data == null) { throw new RuntimeException("data is null"); }/*from w ww. ja va 2 s . c om*/ StringReader stringReader = new StringReader(data); InputSource inputSource = new InputSource(stringReader); // parse inputs DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); return builder.parse(inputSource); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } catch (SAXException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.thoughtworks.go.util.XpathUtils.java
public static String evaluate(File file, String xpath) throws XPathExpressionException, IOException { try (InputStream stream = new FileInputStream(file)) { InputSource inputSource = new InputSource(stream); return evaluate(xPathFactory, inputSource, xpath); }/*from w w w .jav a 2 s . c o m*/ }
From source file:Main.java
private static void validateDTD0(String xmlFile, final String dtdPath) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setValidating(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); if (dtdPath != null && !dtdPath.isEmpty()) { documentBuilder.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new FileInputStream(dtdPath)); }// ww w . j ava2s . c o m }); } documentBuilder.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException exception) throws SAXException { } public void fatalError(SAXParseException exception) throws SAXException { throw exception; } public void error(SAXParseException exception) throws SAXException { throw exception; } }); documentBuilder.parse(new FileInputStream(xmlFile)); }
From source file:Main.java
public static String valueForXPath(String xpath, InputStream inputStream) throws XPathExpressionException { /*/* w w w. j a va 2 s . c om*/ * Report.systemProperty("javax.xml.parsers.DocumentBuilderFactory"); * Report.systemProperty("javax.xml.parsers.SAXParserFactory"); * Report.systemProperty("javax.xml.transform.TransformerFactory"); */ XPathFactory factory = XPathFactory.newInstance(); InputSource is = new InputSource(inputStream); XPath xp = factory.newXPath(); return xp.evaluate(xpath, is); }
From source file:Main.java
public static String toPrettyString(String xml) { int indent = 4; try {/*from w w w. j a v a 2s . c o m*/ // 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); } // Setup pretty print options 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, "no"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // Return pretty print xml string StringWriter stringWriter = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); return stringWriter.toString(); } catch (Exception e) { System.out.println(xml); throw new RuntimeException( "Problems prettyfing xml content [" + Splitter.fixedLength(100).split(xml) + "]", e); } }
From source file:Main.java
/** * Reads the DOM Document from an input stream *///ww w . j a va 2 s . c om public static Document readDocument(final InputStream in) { return readDocument(new InputSource(in)); }
From source file:Main.java
public static Document getNormalizedDocument(Reader input) throws ParserConfigurationException, SAXException, IOException { InputSource is = new InputSource(input); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*w ww . j a v a 2s.c o m*/ DocumentBuilder db = dbf.newDocumentBuilder(); Document d = db.parse(is); return d; // return (Document) Nodes.normalizeWhitespace(d); // FIXME: is this required?? }
From source file:Main.java
private static InputSource toInputSource(String xml) { InputSource iso = new InputSource(new StringReader(xml)); return iso;// w w w . ja va 2 s . c om }