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 boolean validateWithDTDUsingDOM(String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*  ww w  .ja va  2s.c o m*/
    factory.setNamespaceAware(true);

    DocumentBuilder builder = factory.newDocumentBuilder();

    builder.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException e) throws SAXException {
            System.out.println("WARNING : " + e.getMessage()); // do nothing
        }

        public void error(SAXParseException e) throws SAXException {
            System.out.println("ERROR : " + e.getMessage());
            throw e;
        }

        public void fatalError(SAXParseException e) throws SAXException {
            System.out.println("FATAL : " + e.getMessage());
            throw e;
        }
    });
    builder.parse(new InputSource(xml));
    return true;
}

From source file:Main.java

public static Document getXMLDocumentFromString(String xml)
        throws SAXException, IOException, ParserConfigurationException {
    Document doc = null;/*from   w  ww  .  ja v a 2s.com*/
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(true);

    DocumentBuilder db = docBuilderFactory.newDocumentBuilder();
    doc = db.parse(new InputSource(new StringReader(xml)));
    return doc;
}

From source file:Main.java

public static Document createDocument(Reader reader) throws IllegalArgumentException {
    // init DOM builder
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setCoalescing(true);/*from   w ww .  j  ava2s . com*/
    factory.setIgnoringComments(true);
    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(false);

    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource inputSource = new InputSource(reader);
        Document doc = builder.parse(inputSource);
        return doc;
    } catch (Exception ex) {
        IllegalArgumentException iae = new IllegalArgumentException(ex.getMessage());
        iae.initCause(ex);
        throw iae;
    }
}

From source file:Main.java

public static Document getXMLDocumentFromScratch(String rootElementName)
        throws SAXException, IOException, ParserConfigurationException {
    Document doc = null;/*from   ww w. j  a  v  a2  s.  c o m*/
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(true);

    DocumentBuilder db = docBuilderFactory.newDocumentBuilder();
    doc = db.parse(new InputSource(new StringReader("<" + rootElementName + "></" + rootElementName + ">")));
    return doc;
}

From source file:Main.java

public static Document openXmlReader(Reader reader, Schema schema, boolean isNamespaceAware,
        boolean isXIncludeAware) throws IOException, SAXParseException, SAXException {
    return openXmlStream((new InputSource(reader)).getByteStream(), schema, isNamespaceAware, isXIncludeAware);
}

From source file:Main.java

public static Document string2Document(String xml) throws Exception {
    InputSource src = new InputSource(new StringReader(xml));
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);//from w w  w.  j  a va  2 s .  c  o m
    return dbFactory.newDocumentBuilder().parse(src);
}

From source file:Main.java

public static Document stringToDocument(String assumedXml)
        throws ParserConfigurationException, SAXException, IOException {
    Document doc = null;//from w w  w .  j  av  a 2s .co  m
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    doc = builder.parse(new InputSource(new ByteArrayInputStream(assumedXml.getBytes())));

    return doc;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T readXML(Class<?> class1, File file)
        throws JAXBException, IOException, SAXException, ParserConfigurationException {
    JAXBContext context = JAXBContext.newInstance(class1);
    Unmarshaller um = context.createUnmarshaller();

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    spf.setFeature("http://xml.org/sax/features/validation", false);

    XMLReader xr = (XMLReader) spf.newSAXParser().getXMLReader();
    try (FileReader reader = new FileReader(file)) {
        SAXSource source = new SAXSource(xr, new InputSource(reader));

        T obj = (T) um.unmarshal(source);
        return obj;
    }/*from w ww . jav  a 2  s.  c  o  m*/
}

From source file:Main.java

private static Document getDocument(String xml) throws ParserConfigurationException, SAXException, IOException {
    // Create a builder factory
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*from  w  ww .  ja v a 2  s  . co m*/

    return factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
}

From source file:Main.java

private static Document parseXmlFile(String in) {
    try {//w w w  .j  a  va  2 s. c  o m
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(in));
        return db.parse(is);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}