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 {
    String xml = "<soapenv:Envelope " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
            + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
            + "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "
            + "xmlns:ser=\"http://services.web.post.list.com\"><soapenv:Header>"
            + "<authInfo xsi:type=\"soap:authentication\" "
            + "xmlns:soap=\"http://list.com/services/SoapRequestProcessor\">"
            + "<username xsi:type=\"xsd:string\">asdf@g.com</username>"
            + "<password xsi:type=\"xsd:string\">asdf</password></authInfo></soapenv:Header></soapenv:Envelope>";
    System.out.println(xml);/*from ww w  .j  ava  2 s  .  c  om*/
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(xml)));
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(new NamespaceContext() {

        @Override
        public Iterator getPrefixes(String arg0) {
            return null;
        }

        @Override
        public String getPrefix(String arg0) {
            return null;
        }

        @Override
        public String getNamespaceURI(String arg0) {
            if ("soapenv".equals(arg0)) {
                return "http://schemas.xmlsoap.org/soap/envelope/";
            }
            return null;
        }
    });
    XPathExpression expr = xpath.compile("/soapenv:Envelope/soapenv:Header/authInfo/password");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    System.out.println("Got " + nodes.getLength() + " nodes");
}

From source file:MyErrorHandler.java

static public void main(String[] arg) throws Exception {
    boolean validate = false;
    validate = true;//from  w ww .ja  va  2 s  .com

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setValidating(validate);

    XMLReader reader = null;
    SAXParser parser = spf.newSAXParser();
    reader = parser.getXMLReader();

    reader.setErrorHandler(new MyErrorHandler());
    InputSource is = new InputSource(new StringReader(getXMLData()));
    reader.parse(is);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(new InputSource(new StringReader(cfgXml)));
    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("config");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        System.out.println("\nCurrent Element :" + nNode.getNodeName());
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            Config c = new Config();
            c.name = eElement.getAttribute("name");
            c.type = eElement.getAttribute("type");
            c.format = eElement.getAttribute("format");
            c.size = Integer.valueOf(eElement.getAttribute("size"));
            c.scale = Integer.valueOf(eElement.getAttribute("scale"));
            String attribute = eElement.getAttribute("required");
            c.required = Boolean.valueOf("Yes".equalsIgnoreCase(attribute) ? true : false);
            System.out.println("Imported config : " + c);
        }//w  w w .  j  a va  2  s. c o m
    }
}

From source file:Main.java

public static void main(String args[]) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true); // Set namespace aware
    builderFactory.setValidating(true); // and validating parser feaures
    builderFactory.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = null;
    try {//from   w  ww  . j av a 2 s .co  m
        builder = builderFactory.newDocumentBuilder(); // Create the parser
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    Document xmlDoc = null;

    try {
        xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));

    } catch (SAXException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();
    }
    DocumentType doctype = xmlDoc.getDoctype();
    if (doctype == null) {
        System.out.println("DOCTYPE is null");
    } else {
        System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
    }

    System.out.println("\nDocument body contents are:");
    listNodes(xmlDoc.getDocumentElement(), ""); // Root element & children
}

From source file:com.web.server.ShutDownServer.java

/**
 * @param args//from  w w  w  . j av a2 s  .  c o m
 * @throws SAXException 
 * @throws IOException 
 */
public static void main(String[] args) throws IOException, SAXException {
    DigesterLoader serverdigesterLoader = DigesterLoader.newLoader(new FromXmlRulesModule() {

        protected void loadRules() {
            // TODO Auto-generated method stub
            try {
                loadXMLRules(new InputSource(new FileInputStream("./config/serverconfig-rules.xml")));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });
    Digester serverdigester = serverdigesterLoader.newDigester();
    ServerConfig serverconfig = (ServerConfig) serverdigester
            .parse(new InputSource(new FileInputStream("./config/serverconfig.xml")));
    try {
        Socket socket = new Socket("localhost", Integer.parseInt(serverconfig.getShutdownport()));
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("shutdown WebServer\r\n\r\n".getBytes());
        outputStream.close();
    } catch (IOException e1) {

        e1.printStackTrace();
    }

}

From source file:SAXCheck.java

static public void main(String[] arg) {
    String filename = null;/*  w ww.  j  a  va2s  . c  om*/
    boolean validate = false;

    if (arg.length == 1) {
        filename = arg[0];
    } else if (arg.length == 2) {
        if (!arg[0].equals("-v"))
            usage();
        validate = true;
        filename = arg[1];
    } else {
        usage();
    }

    // Create a new factory to create parsers that will
    // validate or not, according to the flag setting.
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setValidating(validate);

    // Create the XMLReader to be used to check for errors.
    XMLReader reader = null;
    try {
        SAXParser parser = spf.newSAXParser();
        reader = parser.getXMLReader();
    } catch (Exception e) {
        System.err.println(e);
        System.exit(1);
    }

    // Install an error handler in the reader.
    reader.setErrorHandler(new MyErrorHandler());
    // Use the XMLReader to parse the entire file.
    try {
        InputSource is = new InputSource(filename);
        reader.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
}

From source file:com.app.server.ShutDownServer.java

/**
 * @param args/* w  ww.j a  v  a  2  s  .  c o  m*/
 * @throws SAXException 
 * @throws IOException 
 */
public static void main(String[] args) throws IOException, SAXException {
    DigesterLoader serverdigesterLoader = DigesterLoader.newLoader(new FromXmlRulesModule() {

        protected void loadRules() {
            // TODO Auto-generated method stub
            try {
                loadXMLRules(new InputSource(new FileInputStream("./config/serverconfig-rules.xml")));
            } catch (FileNotFoundException e) {
                log.error("Could not load rules xml serverconfig-rules.xml", e);
                // TODO Auto-generated catch block
                //e.printStackTrace();
            }

        }
    });
    Digester serverdigester = serverdigesterLoader.newDigester();
    ServerConfig serverconfig = (ServerConfig) serverdigester
            .parse(new InputSource(new FileInputStream("./config/serverconfig.xml")));
    try {
        Socket socket = new Socket("localhost", Integer.parseInt(serverconfig.getShutdownport()));
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("shutdown WebServer\r\n\r\n".getBytes());
        outputStream.close();
    } catch (Exception ex) {
        log.error("Could not able to create socket and write shutdown bytes", ex);
        //e1.printStackTrace();
    }

}

From source file:DOMCheck.java

static public void main(String[] arg) {
    String filename = null;/*  w  w w .j  av a2s.  com*/
    boolean validate = false;

    if (arg.length == 1) {
        filename = arg[0];
    } else if (arg.length == 2) {
        if (!arg[0].equals("-v"))
            usage();
        validate = true;
        filename = arg[1];
    } else {
        usage();
    }

    // Create a new factory to create parsers that will
    // be aware of namespaces and will validate or
    // not according to the flag setting.
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);
    dbf.setNamespaceAware(true);

    // Use the factory to create a parser (builder) and use
    // it to parse the document.
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        builder.setErrorHandler(new MyErrorHandler());
        InputSource is = new InputSource(filename);
        Document doc = builder.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (ParserConfigurationException e) {
        System.err.println(e);
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
}

From source file:DOMDump.java

static public void main(String[] arg) {
    String filename = null;// www  .j a  va2s .  c  o m
    boolean validate = false;

    if (arg.length == 1) {
        filename = arg[0];
    } else if (arg.length == 2) {
        if (!arg[0].equals("-v"))
            usage();
        validate = true;
        filename = arg[1];
    } else {
        usage();
    }

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    // Parse the input to produce a parse tree with its root
    // in the form of a Document object
    Document doc = null;
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        builder.setErrorHandler(new MyErrorHandler());
        InputSource is = new InputSource(filename);
        doc = builder.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (ParserConfigurationException e) {
        System.err.println(e);
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }

    // Use a TreeDumper to list the tree
    TreeDumper td = new TreeDumper();
    td.dump(doc);
}

From source file:ApplyXPathJAXP.java

public static void main(String[] args) {
    QName returnType = null;//from   w ww .j ava2s .  co  m

    if (args.length != 3) {
        System.err.println("Usage: java ApplyXPathAPI xml_file xpath_expression type");
    }

    InputSource xml = new InputSource(args[0]);
    String expr = args[1];

    // set the return type
    if (args[2].equals("num"))
        returnType = XPathConstants.NUMBER;
    else if (args[2].equals("bool"))
        returnType = XPathConstants.BOOLEAN;
    else if (args[2].equals("str"))
        returnType = XPathConstants.STRING;
    else if (args[2].equals("node"))
        returnType = XPathConstants.NODE;
    else if (args[2].equals("nodeset"))
        returnType = XPathConstants.NODESET;
    else
        System.err.println("Invalid return type: " + args[2]);

    // Create a new XPath
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    Object result = null;
    try {
        // compile the XPath expression
        XPathExpression xpathExpr = xpath.compile(expr);

        // Evaluate the XPath expression against the input document
        result = xpathExpr.evaluate(xml, returnType);

        // Print the result to System.out.
        printResult(result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}