Example usage for org.xml.sax InputSource InputSource

List of usage examples for org.xml.sax InputSource InputSource

Introduction

In this page you can find the example usage for org.xml.sax InputSource InputSource.

Prototype

public InputSource(Reader characterStream) 

Source Link

Document

Create a new input source with a character stream.

Usage

From source file:Main.java

/** Parse a valid xml string and return the Element representing this string. */
public static Element parseXMLString(Document document, String string)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*  ww  w .  ja v  a2 s .  com*/
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document subDoc = builder.parse(new InputSource(new StringReader(string)));
    Element e = subDoc.getDocumentElement();
    return (Element) document.importNode(e, true);
}

From source file:Main.java

public static Document parse(InputStream is) throws IOException {
    try {/*from w  ww.  j  a va2  s.com*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setCoalescing(true);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);
        factory.setNamespaceAware(true);
        DocumentBuilder parser = factory.newDocumentBuilder();
        InputSource in = new InputSource(is);
        return parser.parse(in);
    } catch (ParserConfigurationException e) {
        throw new IOException(e);
    } catch (SAXException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

public static Document getXmlDocFromString(String xml) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from w  ww  .  j  av  a  2s. com*/
    dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    DocumentBuilder builder = dbf.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {
        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            return new InputSource(new StringReader(""));
        }
    });
    return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
}

From source file:Main.java

private static InputSource toInputSource(URL xmlFile) {
    InputSource iso;/* w w  w  .  j ava  2 s . c  o m*/
    try {
        iso = new InputSource(xmlFile.openStream());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return iso;
}

From source file:Main.java

public static InputSource createInputSource(InputStream inputStream) {
    return new InputSource(inputStream);
}

From source file:Main.java

public static Document parseDoc(File xmlFile) throws ParserConfigurationException, SAXException, IOException {
    try (InputStream is = new FileInputStream(xmlFile)) {
        BufferedInputStream in = new BufferedInputStream(is);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource source = new InputSource(in);
        return builder.parse(source);
    }//from w  w w  .  j a v  a2  s .  c o m
}

From source file:net.sf.janos.model.xml.ResultParser.java

/**
 * @param xml//from www.j  a va 2s  .c  om
 * @return a list of Entrys from the given xml string.
 * @throws IOException
 * @throws SAXException
 */
public static List<Entry> getEntriesFromStringResult(String xml) throws SAXException {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    EntryHandler handler = new EntryHandler();
    reader.setContentHandler(handler);
    try {
        reader.parse(new InputSource(new StringReader(xml)));
    } catch (IOException e) {
        // This should never happen - we're not performing I/O!
        LOG.error("Could not parse entries: ", e);
    }
    return handler.getArtists();
}

From source file:Main.java

public InputSource resolveEntity(String publicId, String systemId) {
    try {/* w  w w.j  av a  2s . c  om*/
        URI uri = new URI(systemId);
        if ("file".equals(uri.getScheme())) {
            String filename = uri.getSchemeSpecificPart();
            return new InputSource(new FileReader(filename));
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:Main.java

/**
 * Transform XML to JSON/*from w ww  .  j av a  2 s  . c  o m*/
 *
 * @param xml
 * @return
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static String toJson(String xml) throws ParserConfigurationException, SAXException, IOException {
    JsonObject rootJson = new JsonObject();
    DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = dBuilder.parse(new InputSource(new StringReader(xml)));
    if (doc.hasChildNodes()) {
        traverseNode(doc, rootJson, null);
    }
    Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
    String json = gson.toJson(rootJson);

    return json;
}

From source file:Main.java

/**
 * @param parent//from  w  ww . j a v  a2  s .  co  m
 *          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 {

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder docBuilder = domFactory.newDocumentBuilder();
    Document doc = parent.getOwnerDocument();

    Node fragmentNode = docBuilder.parse(new InputSource(new StringReader(fragment))).getDocumentElement();

    fragmentNode = doc.importNode(fragmentNode, true);

    parent.appendChild(fragmentNode);
}