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

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.setDocumentURI("http://java2s.com");

}

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.setStrictErrorChecking(true);

}

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)));

    System.out.println(xmlDoc.getXmlVersion());

}

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)));

    System.out.println(xmlDoc.getXmlStandalone());

}

From source file:Main.java

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

    InputSource doc = new InputSource(new InputStreamReader(new FileInputStream(new File("file.xml"))));

    String expression = "//Home/data";
    XPathExpression xPathExpression = xPath.compile(expression);

    NodeList elem1List = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET);
    xPathExpression = xPath.compile("@type");

    for (int i = 0; i < elem1List.getLength(); i++) {
        System.out.println(xPathExpression.evaluate(elem1List.item(i), XPathConstants.STRING));
    }/*from  w  w w . j a v a 2s .c  o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource xml = new InputSource(new StringReader(XML));
    String result = (String) xpath.evaluate("//@Build-Label", xml, XPathConstants.STRING);
    System.out.println(result);// w w w  .  j a v  a  2 s.c o m
}

From source file:PrintDOM.java

public static void main(String[] args) throws Exception {
    DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = parser.parse(new InputSource("zooinventory.xml"));
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    Source source = new DOMSource(document);
    Result output = new StreamResult(System.out);
    transformer.transform(source, output);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource("data.xml"));

    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nodes = (NodeList) xpath.evaluate("//employee/name[text()='old']", doc, XPathConstants.NODESET);

    for (int idx = 0; idx < nodes.getLength(); idx++) {
        nodes.item(idx).setTextContent("new value");
    }/*  w  w  w  . ja  v  a 2s.  c  o m*/
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(new DOMSource(doc), new StreamResult(new File("data_new.xml")));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xPath = xpf.newXPath();
    XPathExpression schoolNameExpression = xPath.compile("SchoolName");
    XPathExpression classNameExpression = xPath.compile("Classes/Class/ClassName");

    InputSource inputSource = new InputSource("input.xml");
    NodeList schoolNodes = (NodeList) xPath.evaluate("/Data/Schools/School", inputSource,
            XPathConstants.NODESET);
    for (int x = 0; x < schoolNodes.getLength(); x++) {
        Node schoolElement = schoolNodes.item(x);
        System.out.println(schoolNameExpression.evaluate(schoolElement, XPathConstants.STRING));
        NodeList classNames = (NodeList) classNameExpression.evaluate(schoolElement, XPathConstants.NODESET);
        for (int y = 0; y < classNames.getLength(); y++) {
            System.out.println(classNames.item(y).getTextContent());
        }//from  w  ww  .j a  v a2s  . c o m
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);//from  w ww .j  ava2s . c o m
    SAXParser parser = factory.newSAXParser();
    parser.parse(new InputSource(new StringReader(getXMLData())), new SampleOfXmlLocator());
}