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 stringToElement(String str) {
    StringReader sr = new StringReader(str);

    try {/*from   w  w w  .  j  ava 2s. c  o m*/
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(false);
        docBuilderFactory.setValidating(false);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

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

From source file:Main.java

public static Document deserialize(String sXML) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;//from www.  j  a  v  a2s  .c  om
    builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(sXML)));
    return doc;
}

From source file:Main.java

public static Document getStringToDocument(String xml)
        throws TransformerException, ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbFactory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    return document;
}

From source file:Main.java

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    Node reqName = document.getElementsByTagName("request-id").item(0);
    if (reqName != null)
        return reqName.getTextContent();
    else// ww w.jav a  2s. co m
        return null;
}

From source file:Main.java

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    Node reqName = document.getElementsByTagName("unicast-id").item(0);
    if (reqName != null)
        return reqName.getTextContent();
    else/*from  ww w .  j av a2s.  c o  m*/
        return null;
}

From source file:Main.java

public static Document fileAsDocument(final File file)
        throws ParserConfigurationException, SAXException, IOException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final InputSource is = new InputSource(new FileReader(file));
    return builder.parse(is);

}

From source file:Main.java

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new InputSource(new StringReader(xml)));
    Node reqName = document.getElementsByTagName("request-name").item(0);
    if (reqName != null)
        return reqName.getTextContent();
    else//from w w  w. j  av  a  2s  .co  m
        return null;
}

From source file:Main.java

/**
 * Extract a String node from an XML Message
 *
 * @param xpath     XPath object/*from  ww w.  j  a  va 2s  . c o m*/
 * @param nodePath  XPath statement to locate the node
 * @param xmlString Xml string object to extract the data from
 * @return The requested data, or "" if not found.
 */
public static String extractNode(XPath xpath, String nodePath, String xmlString) {
    InputSource inputSource = new InputSource(new StringReader(xmlString));

    try {
        return (String) xpath.evaluate(nodePath, inputSource, XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        return "";
    }
}

From source file:Main.java

public static Document loadXMLFromString(String xml)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    return builder.parse(is);
}

From source file:Main.java

public static HashMap xmltoHashMap222(String xmlFile, String xpath) {
    try {//from  w  ww .j  av  a  2s  .co  m
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        // XPathExpression xPathExpression =
        // xPath.compile("/history");
        File xmlDocument = new File(xmlFile);
        InputSource inputSource = new InputSource(new FileInputStream(xmlDocument));
        // String root = xPath.evaluate("/", inputSource);
        NodeList nodes = (NodeList) xPath.evaluate(xpath, inputSource, XPathConstants.NODESET);
        HashMap hashmap = new HashMap();
        for (int x = 0; x < nodes.getLength(); x++) {
            hashmap.put(nodes.item(x).getNodeName(), nodes.item(x).getTextContent());
        }
        return hashmap;
    } catch (Exception ex) {
        return null;
    }
}