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

static public void main(String[] arg) throws Exception {
    String filename = "input.xml";
    boolean validate = true;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);//from w  w  w  . j av  a2 s  .c  om
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = dbf.newDocumentBuilder();
    builder.setErrorHandler(new MyErrorHandler());
    InputSource is = new InputSource(filename);
    Document doc = builder.parse(is);
    TreeDumper td = new TreeDumper();
    td.dump(doc);
}

From source file:Main.java

static public void main(String[] arg) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);//from  w  w w. j av  a2s.  c  o  m
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = dbf.newDocumentBuilder();

    StringReader sr = new StringReader("<tag>java2s.com</tag>");
    Document document = builder.parse(new InputSource(sr));
    deleteFirstElement(document);

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer trans = tf.newTransformer();
    StringWriter sw = new StringWriter();
    trans.transform(new DOMSource(document), new StreamResult(sw));
    System.out.println(sw.toString());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    serializer.setOutputProperty("{http://xml.customer.org/xslt}indent-amount", "2");
    Source xmlSource = new SAXSource(new InputSource(
            new ByteArrayInputStream("<a><b><c/><d>text D</d><e value='0'/></b></a>".getBytes())));
    StreamResult res = new StreamResult(new ByteArrayOutputStream());
    serializer.transform(xmlSource, res);
    System.out.println(new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()));
}

From source file:Main.java

static public void main(String[] arg) {
    boolean validate = true;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);/*from ww  w .  ja va  2s.  c  om*/
    dbf.setNamespaceAware(true);

    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        builder.setErrorHandler(new MyErrorHandler());
        InputSource is = new InputSource(new StringReader(getXMLData()));
        Document doc = builder.parse(is);
    } catch (SAXException e) {
        System.out.println(e);
    } catch (ParserConfigurationException e) {
        System.err.println(e);
    } catch (IOException e) {
        System.err.println(e);
    }
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    File documentFile = new File(args[0]);
    File schemaFile = new File(args[1]);

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    Schema schema = null;/*from   www  .ja  v a 2 s  .  co  m*/
    try {
        schema = factory.newSchema(schemaFile);
    } catch (SAXException e) {
        fail(e);
    }

    Validator validator = schema.newValidator();

    SAXSource source = new SAXSource(new InputSource(new FileReader(documentFile)));

    try {
        validator.validate(source);
    } catch (SAXException e) {
        fail(e);
    }
}

From source file:MainClass.java

static public void main(String[] arg) {
    boolean validate = false;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);//  w  ww .  j ava 2  s  .  c  o  m
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    Document doc = null;
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        doc = builder.parse(new InputSource(new StringReader(xmlString)));
    } 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);
    }

    TreeDumper td = new TreeDumper();
    td.dump(doc);
}

From source file:TestModelBuilder.java

public static void main(String[] args) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();
    XMLReader parser = saxParser.getXMLReader();
    SAXModelBuilder mb = new SAXModelBuilder();
    parser.setContentHandler(mb);//from   w ww.j a  va  2s  .  co  m

    parser.parse(new InputSource("zooinventory.xml"));
    Element2 inventory = (Element2) mb.getModel();
    System.out.println("Animals = " + inventory.getAnimals());
    Element3 cocoa = (Element3) (inventory.getAnimals().get(1));
    ElementA recipe = cocoa.getFoodRecipe();
    System.out.println("Recipe = " + recipe);
}

From source file:SAXCopy.java

static public void main(String[] arg) {
    try {//from   w  ww. j  av  a  2s  .co m
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser parser = spf.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setErrorHandler(new MyErrorHandler());
        MyCopyHandler duper = new MyCopyHandler();
        reader.setContentHandler(duper);
        InputSource is = new InputSource("test.xml");
        reader.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:Main.java

static public void main(String[] arg) {
    boolean validate = true;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);/*from  ww  w  . j  av a 2  s.  com*/
    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();
        InputSource is = new InputSource(new StringReader(getXMLData()));
        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);
    }
    dump(doc);
}

From source file:Main.java

static public void main(String[] arg) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);// w w  w .ja  v  a  2  s . co  m
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    Document doc = null;
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();

        InputSource is = new InputSource(new StringReader(getXMLData()));
        doc = builder.parse(is);

        write(doc);
    } catch (Exception e) {
        System.err.println(e);
    }
}