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 void main(String[] args) throws Exception {
    String docroot = "<div><i>items <b>sold</b></i></div>";
    XPath xxpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(new StringReader(docroot));
    String result = (String) xxpath.evaluate("//b", inputSource, XPathConstants.STRING);
    System.out.println(result);/* w  w w  .j  av  a2s .  c  o  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream inputStream = new FileInputStream("data.xml");
    InputSource inputSource = new InputSource(inputStream);
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile("/PaymentElement/Payment/@seqID");
    Object result = expr.evaluate(inputSource, XPathConstants.STRING);
    System.out.println(result);/*from   w w w . j  a  v a  2 s  .co  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xPath = XPathFactory.newInstance().newXPath();

    FileReader reader = new FileReader("input.xml");
    InputSource xml = new InputSource(reader);
    NodeList titleNodes = (NodeList) xPath.evaluate("//item/title", xml, XPathConstants.NODESET);

    for (int x = 0; x < titleNodes.getLength(); x++) {
        System.out.println(titleNodes.item(x).getTextContent());
    }//from   w  w w .j  a va  2  s.com
}

From source file:GetSeriesId.java

public static void main(String[] args) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();

    Double result = (Double) xPath.evaluate("/schedule/@seriesId", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NUMBER);
    System.out.println(result.intValue());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<car><manufacturer>toyota</manufacturer></car>";
    String xpath = "/car/manufacturer";
    XPath xPath = XPathFactory.newInstance().newXPath();
    assertEquals("toyota", xPath.evaluate(xpath, new InputSource(new StringReader(xml))));
}

From source file:GuestListCounter.java

public static void main(String[] args) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();

    Number shows = (Number) xPath.evaluate("count(/schedule/show)", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NUMBER);
    System.out.println("Document has " + shows.intValue() + " shows.");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputSource source = new InputSource(new StringReader("<root>\n" + "<field name='firstname'>\n"
            + "    <value>John</value>\n" + "</field>\n" + "<field name='lastname'>\n"
            + "    <value>Citizen</value>\n" + "</field>\n" + "<field name='DoB'>\n"
            + "    <value>01/01/1980</value>\n" + "</field>\n" + "<field name='Profession'>\n"
            + "    <value>Manager</value>\n" + "</field>\n" + "</root>"));

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    Document document = documentBuilder.parse(source);

    NodeList allFields = (NodeList) document.getElementsByTagName("field");

    Map<String, String> data = new HashMap<>();
    for (int i = 0; i < allFields.getLength(); i++) {
        Element field = (Element) allFields.item(i);
        String nameAttribute = field.getAttribute("name");
        Element child = (Element) field.getElementsByTagName("value").item(0);
        String value = child.getTextContent();
        data.put(nameAttribute, value);// w w  w  .  j  av a2  s  .  c  om
    }

    for (Map.Entry field : data.entrySet()) {
        System.out.println(field.getKey() + ": " + field.getValue());
    }
}

From source file:GetNameAsAttr.java

public static void main(String[] args) throws Exception {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();

    Attr result = (Attr) xPath.evaluate("/schedule/@name", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NODE);
    System.out.println(result.getValue());

    result.setValue("The Colbert Report");

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    Element root = doc.getDocumentElement();
    System.out.println(root);//from ww  w.j a va  2s. co  m
}

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder(); // Create the parser
    Document xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    xmlDoc.setXmlVersion("2.0");

}