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 String formatXML(String xml) {
    try {//from  w  ww.j av  a 2s. c o  m
        Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
        StreamResult res = new StreamResult(new ByteArrayOutputStream());
        serializer.transform(xmlSource, res);
        return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray());
    } catch (Exception e) {

        return xml;
    }
}

From source file:Main.java

public static Map<String, String> load(Reader reader) throws IOException {
    try {/*from  www  .ja  v a2  s. c  om*/
        // Load XML into JDOM Document
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db;

        db = dbf.newDocumentBuilder();

        Document doc = db.parse(new InputSource(reader));
        doc.getDocumentElement().normalize();

        Map<String, String> result = new HashMap<String, String>();
        loadFromElements(result, doc.getDocumentElement().getChildNodes(),
                new StringBuffer(doc.getDocumentElement().getNodeName()));
        return result;
    } catch (ParserConfigurationException e) {
        throw new IOException(e);
    } catch (SAXException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

public static Document stringToDocument(final String string)
        throws ParserConfigurationException, UnsupportedEncodingException, SAXException, IOException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from   ww w .  j a v a  2 s.c  om
    final DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new ByteArrayInputStream(string.getBytes(ENCODING))));
}

From source file:Main.java

/**
 * Parse the XML data in the given input stream, using the
 * specified handler object as both the content and error handler.
 *
 * @param handler the SAX event handler//from ww w .j av  a 2s. c om
 * @param in the input stream containing the XML to be parsed
 */
public static void parse(DefaultHandler handler, InputStream in)
        throws IOException, ParserConfigurationException, SAXException {
    XMLReader xr = _pfactory.newSAXParser().getXMLReader();

    xr.setContentHandler(handler);
    xr.setErrorHandler(handler);

    xr.parse(new InputSource(in));
}

From source file:Main.java

public static Document StringToXML(String xmlString) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    DocumentBuilder builder = null;
    Document document = null;/* w  w w . j a  v a 2  s .  c o m*/
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        document = builder.parse(new InputSource(new StringReader(xmlString)));
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return document;
}

From source file:Main.java

/**
 * Parse XML//from   w  w  w  .j  a v a2s  .c o m
 * @throws ParserConfigurationException 
 * @throws IOException 
 * @throws SAXException 
 * */
public static Document parseXMLFromInputStream(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(new InputSource(new InputStreamReader(is)));
    return doc;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unmarshallXml(Class<T> clazz, InputStream in)
        throws JAXBException, UnsupportedEncodingException {

    String className = clazz.getPackage().getName();
    JAXBContext context = JAXBContext.newInstance(className);
    Unmarshaller unmarshaller = context.createUnmarshaller();

    //Reader reader = new IgnoreIllegalCharactersXmlReader(in);

    Reader reader = new InputStreamReader(in, "UTF-8");

    InputSource is = new InputSource(reader);
    is.setEncoding("UTF-8");

    Object result = unmarshaller.unmarshal(is); //file);
    return (T) result;
}

From source file:Main.java

public static Document doc(String xml) {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = null;
    try {/*from w  ww.j  a  v  a  2 s .co  m*/
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
    Document doc = null;
    try {
        doc = documentBuilder.parse(new InputSource(new StringReader(xml)));
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return doc;
}

From source file:Main.java

/**
 * Convert XML string to a XML DOM document
 *
 * @param strXML/*  w w  w  .j  a v a 2  s. c  o  m*/
 *            XML
 * @return XML DOM document
 * @throws Exception
 *             in error case
 */
public static Document xmlStringToDOMDocument(String strXML) throws Exception {
    if (strXML == null) {
        throw new RuntimeException("No XML input given(null)!");
    }

    StringReader reader = null;
    Document doc = null;
    try {
        reader = new StringReader(strXML);
        InputSource inputSource = new InputSource(reader);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(inputSource);
        doc.getDocumentElement().normalize();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Parsing of XML input failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (reader != null) {
            reader.close();
            reader = null;
        }
    }

    return doc;
}

From source file:com.atomiton.watermanagement.ngo.util.WaterMgmtNGOUtility.java

public static String getXMLElementValue(String tagName, String xmlString) {
    try {/*  w  w w.  j a  v  a2 s  . c om*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(xmlString)));
        Element rootElement = document.getDocumentElement();
        String rootElementTagName = rootElement.getTagName();
        if (rootElementTagName.equalsIgnoreCase(tagName)) {
            return rootElement.getTextContent();
        } else {
            NodeList list = rootElement.getElementsByTagName(tagName);
            if (list != null && list.getLength() > 0) {
                NodeList subList = list.item(0).getChildNodes();
                if (subList != null && subList.getLength() > 0) {
                    return subList.item(0).getNodeValue();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}