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 {
    InputSource inputSource = new InputSource("input.xml");
    XPath xPath = XPathFactory.newInstance().newXPath();
    String text = xPath.evaluate("/note/body", inputSource);
    System.out.println(text);/*from  w  w w.  j  ava  2s .  co m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xp = XPathFactory.newInstance().newXPath();
    InputSource xml = new InputSource(new FileInputStream("input.xml"));
    double count = (Double) xp.evaluate("count(//slot[@name='slot1']/port)", xml, XPathConstants.NUMBER);
}

From source file:Main.java

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

    InputSource xml = new InputSource("input.xml");
    String health = (String) xpath.evaluate("/Game/health", xml, XPathConstants.STRING);
    System.out.println(health);/*ww w .  j  a va2  s .co m*/
}

From source file:GetName.java

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

    String result = xPath.evaluate("/schedule/@name", new InputSource(new FileReader("tds.xml")));
    System.out.println(result);//from w  w w . j  av a  2s . co  m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();//w ww  .j a v a 2s  .  c o m

    InputSource xml = new InputSource("input.xml");
    NodeList leafNodeObjects = (NodeList) xp.evaluate("//*[not(*)]", xml, XPathConstants.NODESET);

    for (int x = 0; x < leafNodeObjects.getLength(); x++) {
        System.out.print("nodeElement = ");
        System.out.print(leafNodeObjects.item(x).getNodeName());
        System.out.print(" and node value = ");
        System.out.println(leafNodeObjects.item(x).getTextContent());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    InputSource xml = new InputSource(new FileReader("input.xml"));
    Node result = (Node) xpath.evaluate("/tag1", xml, XPathConstants.NODE);
    System.out.println(result);//ww  w.jav  a2  s  .com
}

From source file:Main.java

public static void main(String[] args) {

    String simpleXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><person><name>Bob</name></person>";
    InputSource inputSource = new InputSource(new StringReader(simpleXML));
    XPath xpath = XPathFactory.newInstance().newXPath();

    try {/*w w  w  .  ja  v  a2s . c o m*/
        String name = xpath.evaluate("//name", inputSource);
        System.out.println(name);
    } catch (XPathExpressionException e) {
        System.out.println(e.getMessage());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nodes = (NodeList) xpath.evaluate("/bookshelf/shelf/book/*",
            new InputSource(Main.class.getResourceAsStream("/books.xml")), XPathConstants.NODESET);
    System.out.println("First  node: " + nodes.item(0));
    System.out.println("Second node: " + nodes.item(1));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<a><o><u>ok</u></o></a>";
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new InputSource(new StringReader(xml)));
    NodeList nl = doc.getElementsByTagName("*");
    for (int i = 0; i < nl.getLength(); i++) {
        System.out.println("name is : " + nl.item(i).getNodeName());
    }//from w w w . j  a  v  a  2  s  .  c o  m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    String xpathExpression = "/howto/topic/@name";
    InputSource inputSource = new InputSource("howto.xml");

    NodeList nodes = (NodeList) xpath.evaluate(xpathExpression, inputSource, XPathConstants.NODESET);

    int j = nodes.getLength();

    for (int i = 0; i < j; i++) {
        System.out.println(nodes.item(i).getTextContent());
    }//from   w  ww  . java  2  s  .  co  m

}