List of usage examples for org.xml.sax InputSource InputSource
public InputSource(Reader characterStream)
From source file:Main.java
/** * @param parent/* w w w . j a v a2 s . com*/ * node to add fragment to * @param fragment * a well formed XML fragment * @throws ParserConfigurationException */ public static void appendXmlFragment(Node parent, String fragment) throws IOException, SAXException, ParserConfigurationException { DocumentBuilder docBuilder = getBuilder(); Document doc = parent.getOwnerDocument(); Node fragmentNode = docBuilder.parse(new InputSource(new StringReader(fragment))).getDocumentElement(); fragmentNode = doc.importNode(fragmentNode, true); parent.appendChild(fragmentNode); }
From source file:Main.java
/** * Converts an XML string into an equivalent XML document. Applies the * reverse operation of {@link #xmlDocumentToString(org.w3c.dom.Node)}. * * @param xml the XML string to convert//from w ww.j a va2s . c o m * @return a {@link Document} equivalent to the specified XML string * @throws SAXException if an error occurs building the XML document * @throws IOException if a general IO error occurs * @throws ParserConfigurationException if an error occurs configuring the * XML parser */ static Document xmlStringToDocument(String xml) throws SAXException, IOException, ParserConfigurationException { return DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new InputSource(new StringReader(xml))); }
From source file:Main.java
public static Document parseXmlData(String xmlData, boolean validating) { try {//from w w w . j a v a 2 s.co m // Create a builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validating); // Create the builder and parse the file Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xmlData))); return doc; } catch (SAXException e) { // A parsing error occurred; the xml input is not valid System.err.println("SAXException in parsing XML data, will return null doc"); e.printStackTrace(); } catch (ParserConfigurationException e) { System.err.println("ParserConfigurationException in parsing XML data, will return null doc"); e.printStackTrace(); } catch (IOException e) { System.err.println("IOException in parsing XML data, will return null doc"); e.printStackTrace(); } return null; }
From source file:Main.java
public static DocumentBuilder createDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); DocumentBuilder builder = builderFactory.newDocumentBuilder(); builder.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { InputStream source = !systemId.startsWith("file:") ? null : getClass().getResourceAsStream( "/net/sf/logsupport/" + new File(URI.create(systemId)).getName()); return source == null ? new InputSource(new StringReader("")) : new InputSource(source); }/* w w w .j a v a2s . co m*/ }); return builder; }
From source file:Main.java
/** * Parses a document from the given string * // w w w. ja va 2s. c o m * @param template The string to parse * @return The parsed {@link Document} */ public static Document getTemplateDocument(String template) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; dbf.setValidating(false); try { db = dbf.newDocumentBuilder(); db.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicID, String systemID) throws SAXException { return new InputSource(new StringReader("")); } }); Document doc = db.parse(new ByteArrayInputStream(template.getBytes("utf8"))); return doc; } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Helper method to unmarshall a xml doc * @see http://docs.oracle.com/javase/tutorial/jaxb/intro/basic.html * http://jaxb.java.net/nonav/2.2.6/docs/ch03.html#unmarshalling * this uses <T> JAXBElement<T> unmarshal(Source source, Class<T> declaredType) throws JAXBException//from w ww. j a v a 2s . c om * */ /* public static <T> T unmarshall(Class<T> docClass, InputStream inputStream) throws JAXBException{ String packageName = docClass.getPackage().getName(); JAXBContext jc = JAXBContext.newInstance( packageName ); Unmarshaller u = jc.createUnmarshaller(); JAXBElement<T> root = u.unmarshal(new StreamSource(inputStream),docClass); return root.getValue(); }*/ public static <T> T unmarshall(Class<T> docClass, InputStream inputStream) throws JAXBException, ParserConfigurationException, SAXException { String packageName = docClass.getPackage().getName(); JAXBContext jc = JAXBContext.newInstance(packageName); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://apache.org/xml/features/validation/schema", false); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); InputSource inputSource = new InputSource(inputStream); SAXSource source = new SAXSource(xmlReader, inputSource); Unmarshaller u = jc.createUnmarshaller(); JAXBElement<T> root = u.unmarshal(source, docClass); return root.getValue(); }
From source file:com.thoughtworks.go.util.XpathUtils.java
public static boolean nodeExists(InputStream stream, String xpath) throws XPathExpressionException { return nodeExists(new InputSource(stream), xpath); }
From source file:Main.java
public static Document parseXmlResource(InputStream inputStream) { // decide which class loading mechanism to use for loading the jbpm // configuration (see https://jira.jboss.org/jira/browse/JBPM-1148) if (inputStream == null) throw new IllegalArgumentException("Cannot load stream null"); InputSource inputSource = new InputSource(inputStream); return parseXmlInputSource(inputSource); }
From source file:Main.java
public InputSource resolveEntity(String publicId, String systemId) { if (systemId.equals("http://www.my-company.com/order-1.0.dtd")) { return new InputSource(getClass().getResourceAsStream("order.dtd")); } else {/*w w w.ja va 2 s . c om*/ return null; } }
From source file:Main.java
public static Document newXmlDocument(String xml) { try {/*from w w w. j ava2 s. c o m*/ DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); return docBuilder.parse(new InputSource(new StringReader(xml.toString()))); } catch (ParserConfigurationException | SAXException | IOException ex) { System.err.println("Error: Canot create new XML document"); System.err.println("Cause: " + ex.getMessage()); System.exit(1); return null; } }