Example usage for org.xml.sax InputSource setCharacterStream

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

Introduction

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

Prototype

public void setCharacterStream(Reader characterStream) 

Source Link

Document

Set the character stream for this input source.

Usage

From source file:Main.java

/**
 * Returns the data structure org.w3c.dom.Document that we obtain by parsing the specified XML
 * text./* www.  ja v  a 2 s.c  o m*/
 * 
 * @param XmlText
 * @return
 */
public static Document getDomDocument(String xmlText) {
    Document doc = null;

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = factory.newDocumentBuilder();
        InputSource inStream = new InputSource();
        inStream.setCharacterStream(new StringReader(xmlText));
        doc = db.parse(inStream);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return doc;
}

From source file:Utils.java

public static Document readXml(StreamSource is) throws SAXException, IOException, ParserConfigurationException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);//from w  w w  .j ava 2 s  . co  m
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());

    // db.setErrorHandler( new MyErrorHandler());
    InputSource is2 = new InputSource();
    is2.setSystemId(is.getSystemId());
    is2.setByteStream(is.getInputStream());
    is2.setCharacterStream(is.getReader());

    return db.parse(is2);
}

From source file:hoot.services.utils.XmlDocumentBuilder.java

/**
 * Parses an XML string into a DOM//from  w ww  .  j av  a  2s  .co  m
 * 
 * @param xml an XML string
 * @param namespaceAware determines whether namespaces are respected during the parsing
 * @return an XML DOM
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
public static Document parse(String xml, boolean namespaceAware)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(namespaceAware); // never forget this!
    DocumentBuilder builder;
    builder = domFactory.newDocumentBuilder();

    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xml));

    return builder.parse(is);
}

From source file:CB_Core.GCVote.GCVote.java

public static ArrayList<RatingData> GetRating(String User, String password, ArrayList<String> Waypoints) {
    ArrayList<RatingData> result = new ArrayList<RatingData>();

    String data = "userName=" + User + "&password=" + password + "&waypoints=";
    for (int i = 0; i < Waypoints.size(); i++) {
        data += Waypoints.get(i);/* ww w  .ja va  2  s  .co m*/
        if (i < (Waypoints.size() - 1))
            data += ",";
    }

    try {
        HttpPost httppost = new HttpPost("http://gcvote.de/getVotes.php");

        httppost.setEntity(new ByteArrayEntity(data.getBytes("UTF8")));

        // Log.info(log, "GCVOTE-Post" + data);

        // Execute HTTP Post Request
        String responseString = Execute(httppost);

        // Log.info(log, "GCVOTE-Response" + responseString);

        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(responseString));

        Document doc = db.parse(is);

        NodeList nodelist = doc.getElementsByTagName("vote");

        for (Integer i = 0; i < nodelist.getLength(); i++) {
            Node node = nodelist.item(i);

            RatingData ratingData = new RatingData();
            ratingData.Rating = Float.valueOf(node.getAttributes().getNamedItem("voteAvg").getNodeValue());
            String userVote = node.getAttributes().getNamedItem("voteUser").getNodeValue();
            ratingData.Vote = (userVote == "") ? 0 : Float.valueOf(userVote);
            ratingData.Waypoint = node.getAttributes().getNamedItem("waypoint").getNodeValue();
            result.add(ratingData);

        }

    } catch (Exception e) {
        String Ex = "";
        if (e != null) {
            if (e != null && e.getMessage() != null)
                Ex = "Ex = [" + e.getMessage() + "]";
            else if (e != null && e.getLocalizedMessage() != null)
                Ex = "Ex = [" + e.getLocalizedMessage() + "]";
            else
                Ex = "Ex = [" + e.toString() + "]";
        }
        Log.err(log, "GcVote-Error" + Ex);
        return null;
    }
    return result;

}

From source file:edu.harvard.i2b2.fhir.Utils.java

public static Document xmltoDOM(String xmlStr) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbFac = DocumentBuilderFactory.newInstance();
    dbFac.setIgnoringElementContentWhitespace(true);
    DocumentBuilder db = dbFac.newDocumentBuilder();

    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlStr));

    return db.parse(is);
}

From source file:ua.kiev.doctorvera.utils.SMSGateway.java

public static Document loadXMLFromString(String xml) {
    Document doc = null;/*from  w w  w  .j a va 2  s.  c om*/
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is);

    } catch (ParserConfigurationException e) {
        LOG.severe(e.getMessage());
        return null;
    } catch (SAXException e) {
        LOG.severe(e.getMessage());
        return null;
    } catch (IOException e) {
        LOG.severe(e.getMessage());
        return null;
    }
    // return DOM
    return doc;
}

From source file:com.tvs.signaltracker.Utils.java

public static Document XMLfromString(String xml) {

    Document doc = null;/*from w  ww.  j  a  v a 2 s .c om*/

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is);

    } catch (ParserConfigurationException e) {
        System.out.println("XML parse error: " + e.getMessage());
        return null;
    } catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
        return null;
    } catch (IOException e) {
        System.out.println("I/O exeption: " + e.getMessage());
        return null;
    }

    return doc;

}

From source file:org.zaizi.oauth2utils.OAuthUtils.java

public static Map handleXMLResponse(HttpResponse response) {
    Map<String, String> oauthResponse = new HashMap<String, String>();
    try {/*w  ww  .  j a  v  a  2s. com*/

        String xmlString = EntityUtils.toString(response.getEntity());
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = factory.newDocumentBuilder();
        InputSource inStream = new InputSource();
        inStream.setCharacterStream(new StringReader(xmlString));
        Document doc = db.parse(inStream);

        System.out.println("********** Response Receieved **********");
        parseXMLDoc(null, doc, oauthResponse);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Exception occurred while parsing XML response");
    }
    return oauthResponse;
}

From source file:com.basp.trabajo_al_minuto.model.business.BusinessUtils.java

public static InputSource characterStreamString(StringReader reader) throws BusinessException {
    InputSource archivo = new InputSource();
    archivo.setCharacterStream(reader);
    return archivo;
}

From source file:dk.statsbiblioteket.util.xml.DOM.java

/**
 * Parses an XML document from a String to a DOM.
 *
 * @param xmlString      a String containing an XML document.
 * @param namespaceAware if {@code true} the parsed DOM will reflect any
 *                       XML namespaces declared in the document
 * @return The document in a DOM or {@code null} on errors.
 *//*  w  w w  . j a  v  a2  s . c o  m*/
public static Document stringToDOM(String xmlString, boolean namespaceAware) {
    try {
        InputSource in = new InputSource();
        in.setCharacterStream(new StringReader(xmlString));

        DocumentBuilderFactory dbFact = DocumentBuilderFactory.newInstance();
        dbFact.setNamespaceAware(namespaceAware);

        return dbFact.newDocumentBuilder().parse(in);
    } catch (IOException e) {
        log.warn("I/O error when parsing XML :" + e.getMessage() + "\n" + xmlString, e);
    } catch (SAXException e) {
        log.warn("Parse error when parsing XML :" + e.getMessage() + "\n" + xmlString, e);
    } catch (ParserConfigurationException e) {
        log.warn("Parser configuration error when parsing XML :" + e.getMessage() + "\n" + xmlString, e);
    }
    return null;
}