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

public static Element byteArrayToElement(byte[] cont) {
    if (cont == null || cont.length <= 0)
        return null;

    try {/*from   ww  w . j av  a  2 s  .  c  o  m*/
        ByteArrayInputStream bais = new ByteArrayInputStream(cont);
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(false);
        docBuilderFactory.setValidating(false);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        InputSource is = new InputSource(bais);
        // is.setEncoding("gb2312");
        Document doc = docBuilder.parse(is);
        return doc.getDocumentElement();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Format the given string as xml content.
 * @param xml/* w ww .  j  av a 2s  .  c om*/
 * @return
 */
public static String formatXml(String xml) {
    try {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(xml));
        Document doc = domBuilder.parse(is);
        OutputFormat format = new OutputFormat(doc);
        format.setLineWidth(80);
        format.setIndent(2);
        format.setIndenting(true);
        StringWriter out = new StringWriter();
        XMLSerializer xmls = new XMLSerializer(out, format);
        xmls.serialize(doc);
        return out.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return xml;
}

From source file:Main.java

public static Document createDocumentFromXml(String input)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

    StringReader stringReader = new StringReader(input);
    InputSource inputSource = new InputSource(stringReader);
    return documentBuilder.parse(inputSource);
}

From source file:Main.java

/**
 * *****************************************
 * Load XML document from string/*from www  .  j  a v  a 2  s.c  o m*/
 * ******************************************.
 *
 * @param xmlString the xml string
 * @return the document
 * @throws Exception the exception
 */
public static Document loadDocument(String xmlString) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource source = new InputSource(new StringReader(xmlString));

    Document doc = db.parse(source);

    return doc;
}

From source file:Main.java

public static String evaluate(File xmlFile, String xPathExpression) {
    String result = null;//from w  w w  .  ja v a  2  s  . c om

    try {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        result = xPath.evaluate(xPathExpression, new InputSource(new FileInputStream(xmlFile)));
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

    return result;
}

From source file:Main.java

public static Document getDocumentRoot(String xmlFile) {
    // open up the XML file
    DocumentBuilderFactory factory = null;
    DocumentBuilder parser = null;
    Document document = null;//from ww w  .  j a  v a 2  s .  c o m
    InputSource inputSource = null;

    InputStream xmlInputStream = null;

    try {
        xmlInputStream = new File(xmlFile).toURL().openStream();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    inputSource = new InputSource(xmlInputStream);

    try {
        factory = DocumentBuilderFactory.newInstance();
        parser = factory.newDocumentBuilder();
        document = parser.parse(inputSource);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return document;
}

From source file:Main.java

/**
 * Convert string to XML {@link Document}
 *
 * @param string - string to be converted
 * @return - {@link Document}/*from   www  .j  ava  2  s  .  c  o  m*/
 * @throws Exception - if {@link DocumentBuilder} is not initialized
 */
public static Document stringToXml(String string) throws Exception {
    if (builder == null) {
        throw new Exception("DocumentBuilder is null.");
    }
    return builder.parse(new InputSource(new ByteArrayInputStream(string.getBytes("UTF-8"))));
}

From source file:Main.java

public static Document parseXML(String s) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(new StringReader(s)));
    return doc;//w  w  w  . j a v a2s. c  o  m
}

From source file:Main.java

public static org.w3c.dom.Document writeToFile(String xmlContent, String path) {
    System.out.println("This is the path " + path);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;//from   ww  w.  ja v  a  2s .c  o m
    try {
        builder = factory.newDocumentBuilder();
        org.w3c.dom.Document doc = builder.parse(new InputSource(new StringReader(xmlContent)));

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(path));

        transformer.transform(source, result);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

static void formatXMLFile(String file) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new InputStreamReader(new FileInputStream(file))));

    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.METHOD, "xml");
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.setOutputProperty("b;http://xml.apache.org/xsltd;indent-amount", "4");
    xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    Source source = new DOMSource(document);
    Result result = new StreamResult(new File(file));
    xformer.transform(source, result);/*from   w w w .ja va  2s. c om*/
}