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

/**
 * Read an input stream in as an XML document.
 * @param xmlString/*  w w  w . j  av a2  s  . co  m*/
 * @return the XML document
 */
public static Document readXmlDocumentFromString(String xmlString) {
    try {
        DocumentBuilder documentBuilder = documentBuilderFactory.get().newDocumentBuilder();
        return documentBuilder.parse(new InputSource(new StringReader(xmlString)));
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String prettyPrintXML(String xml, int indentAmount)
        throws TransformerConfigurationException, TransformerException {

    Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();

    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indentAmount));
    Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
    StreamResult res = new StreamResult(new ByteArrayOutputStream());
    serializer.transform(xmlSource, res);

    return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray());
}

From source file:Main.java

private static String indent(String xml) {
    try {//from   w  w  w.java 2 s.c  o  m
        InputSource is = new InputSource(new StringReader(xml));
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
        OutputFormat format = new OutputFormat();
        format.setIndenting(true);
        format.setIndent(4);
        Writer out = new StringWriter();
        new XMLSerializer(out, format).serialize(document);
        return out.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Get root element from XML String/*from   w  ww  . ja v a2s  . c o  m*/
 * 
 * @param arg
 *            XML String
 * @return Root Element
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Element getDocumentElementFromString(String arg)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf;
    DocumentBuilder db;
    Document document;
    dbf = DocumentBuilderFactory.newInstance();
    db = dbf.newDocumentBuilder();
    document = db.parse(new InputSource(new StringReader(arg)));
    Element element = document.getDocumentElement();
    return element;

}

From source file:Main.java

public static Document loadFromStream(InputStream inputStream) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);/*  w w w  . j av a  2 s .  c o  m*/
    factory.setNamespaceAware(false);

    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            //System.out.println("Ignoring " + publicId + ", " + systemId);
            return new InputSource(new StringReader(""));
        }
    });

    //System.out.println(StringUtil.toString(inputStream));

    Document doc = builder.parse(inputStream);
    return doc;
}

From source file:Main.java

static Document getLoadingDoc(InputStream in) throws SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setValidating(false);//from   w w w .  j  a  v a  2s  .c o  m
    dbf.setCoalescing(true);
    dbf.setIgnoringComments(true);
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(in);
        return db.parse(is);
    } catch (ParserConfigurationException x) {
        throw new Error(x);
    }
}

From source file:Main.java

private static Element loadRootElement(InputStream inputStream) throws Exception {
    // Get XML document from the URL
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    //as a default, set validating to false
    factory.setValidating(false);//from w  w  w .  j av a2s . c o m
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource inputSource = new InputSource(inputStream);
    Document doc = builder.parse(inputSource);
    return doc.getDocumentElement();
}

From source file:Main.java

public static Document readXMLFromFile(String fileName)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf;//from  www.  ja  va 2s . c  om
    DocumentBuilder db;
    Document document;

    dbf = DocumentBuilderFactory.newInstance();
    db = dbf.newDocumentBuilder();

    File file = new File(fileName);
    InputStream inputStream = new FileInputStream(file);
    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 Object parseRequestObjectFromSoap(String soapXml) throws Exception {
    Object result = null;//from w ww  .ja va2s.com

    if (soapXml != null && soapXml.trim().length() > 0) {
        DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
        xmlFact.setNamespaceAware(true);

        DocumentBuilder builder = xmlFact.newDocumentBuilder();
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        StringReader xsr = new StringReader(soapXml);
        InputSource is = new InputSource(xsr);
        Document doc = builder.parse(is);

        //Node requestNode = (Node) xpath.evaluate("/soap:Envelope/soap:Body/*", doc, XPathConstants.NODE);
        Node requestNode = (Node) xpath.evaluate("/*[local-name()='Envelope']/*[local-name()='Body']/*", doc,
                XPathConstants.NODE);

        JAXBContext ctx = JAXBContext.newInstance("xo.xoi.orderentry.ebonding");
        Unmarshaller unmarshaller = ctx.createUnmarshaller();
        result = unmarshaller.unmarshal(requestNode);
    }

    return result;
}

From source file:Main.java

/**
 * @param string Creates a {@link Document} from a string.
 * @return A {@link Document} representation of the string.
 * @throws ParserConfigurationException if a DocumentBuilder cannot be
 * created which satisfies the configuration requested
 * @throws SAXException if any parse errors occur
 * @throws IOException if any IO errors occur.
 *
 *//*from w  ww . j a  v a2  s .c o m*/
public static Document stringToXml(String string)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(string));
    if (builder.isNamespaceAware()) {
        System.out.println("#######################3Is aware");
    } else {
        System.out.println("#######################Not aware");
    }
    return builder.parse(is);
}