Example usage for java.io StringReader StringReader

List of usage examples for java.io StringReader StringReader

Introduction

In this page you can find the example usage for java.io StringReader StringReader.

Prototype

public StringReader(String s) 

Source Link

Document

Creates a new string reader.

Usage

From source file:Main.java

static public Document getDomElement(String xml) {
    Document doc = null;// w  ww .  j  av  a  2  s.  c  om
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();

        //Set the input to our xml string
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is);

    } catch (ParserConfigurationException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (SAXException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    }

    //return DOM
    return doc;
}

From source file:Main.java

public static String getRequestNameFull(String xml)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));

    NodeList nodeList = document.getDocumentElement().getChildNodes();
    Node reqName = document.getElementsByTagName("request-name").item(0);
    System.out.println(reqName.getTextContent());

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        System.out.println(i + "--" + node);
        if (node instanceof Element) {
            NodeList childNodes = node.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node cNode = childNodes.item(j);
                System.out.println(i + "--" + j + "--" + cNode);
                if (cNode instanceof Element) {
                    String content = cNode.getLastChild().getTextContent().trim();
                    System.out.println(i + "--" + j + "--" + content);
                }/*  w  ww . j a v  a2 s .co m*/
            }
        }
    }
    /*
     * Do the parsing for reqname
     */
    return reqName.getTextContent();
}

From source file:Main.java

public static Document getDocumentFromString(String xmlString) throws Exception {
    DocumentBuilderFactory oDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    try {/*from   w ww.  j av a  2  s  . c o m*/
        // Do not load the DTD
        oDocumentBuilderFactory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd",
                Boolean.FALSE);
    } catch (IllegalArgumentException e) {
    }
    DocumentBuilder oDocumentBuilder = oDocumentBuilderFactory.newDocumentBuilder();

    InputSource inputSource = new InputSource(new StringReader(xmlString));
    return oDocumentBuilder.parse(inputSource);
}

From source file:com.bootcamp.utils.FreeMarkers.java

public static String renderString(String templateString, Map<String, ?> model) {
    try {// w  ww .j  a v  a2  s .co  m
        StringWriter result = new StringWriter();
        Template t = new Template("name", new StringReader(templateString), new Configuration());
        t.process(model, result);
        return result.toString();
    } catch (Exception e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:Main.java

/**
 * Convert XML string to a XML DOM document
 *
 * @param strXML//from   w ww.j  av a2s .  c o m
 *            XML
 * @return XML DOM document
 * @throws Exception
 *             in error case
 */
public static Document xmlStringToDOMDocument(String strXML) throws Exception {
    if (strXML == null) {
        throw new RuntimeException("No XML input given(null)!");
    }

    StringReader reader = null;
    Document doc = null;
    try {
        reader = new StringReader(strXML);
        InputSource inputSource = new InputSource(reader);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(inputSource);
        doc.getDocumentElement().normalize();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Parsing of XML input failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (reader != null) {
            reader.close();
            reader = null;
        }
    }

    return doc;
}

From source file:Main.java

/**
 * Get the XML string as a StAX stream reader instance.
 * @param string a string containing XML
 * @return An <tt>XMLStreamReader</tt> instance wrapping the XML string
 * @throws XMLStreamException if something went wrong
 *///from   w w  w . j  a va2 s  .  co m
public static XMLStreamReader getStringAsXMLStreamReader(final String string) throws XMLStreamException {
    if (xmlInputFactory == null) {
        createXMLInputFactory();
    }

    // Give the string to the StAX reader
    return xmlInputFactory.createXMLStreamReader(new StringReader(string));
}

From source file:Main.java

/**
 * Format the given string as xml content.
 * @param xml/*  ww  w.j av  a  2s.c  o  m*/
 * @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

/**
 * *****************************************
 * 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 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;/*from   w w w .ja v  a  2  s .  c om*/
}

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;/*  w  w  w .j  a  v  a 2 s  .co  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;

}