List of usage examples for org.xml.sax InputSource InputSource
public InputSource(Reader characterStream)
From source file:Main.java
public static String prettyFormat(String xml) { if (xml == null || xml.isEmpty() || !xml.contains("<")) { // System.out.println("Why?"+xml.startsWith("<", 0)); return xml; }/*from w w w . j av a2 s.c om*/ try { 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"); 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()).replace("><", ">\n<"); } catch (Exception e) { System.out.println("prettyFormat: Error.." + e.getMessage()); //TODO log error return xml.replace("<", "\n<"); // return xml.replace("><", ">\n<"); } }
From source file:Main.java
/** * Parse une string contenant un document XML dans un DOM Element. * /*from ww w. j ava 2 s. c om*/ * @param xml le document XML sous forme de string * @return l'element racine du document (root element), ou null si erreur * @throws IOException * @throws SAXException * @throws ParserConfigurationException */ public static Element stringToDomElement(final String xml) throws IOException, SAXException, ParserConfigurationException { Element element = null; if (xml != null) { final DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); final Document doc = db.parse(new InputSource(new StringReader(xml))); element = doc.getDocumentElement(); } return element; }
From source file:Main.java
static public Document parseXml(String xmlText) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xmlText)); return db.parse(is); }
From source file:Main.java
/** * Return a Node corresponding to the input XML string representation. * @param xmlString XML string representation. * @return An instance of Node corresponding to the input XML string representation. * @throws ParserConfigurationException * @throws SAXException //from ww w .j ava 2 s . co m * @throws IOException */ public static Node getXMLNode(String xmlString) throws ParserConfigurationException, SAXException, IOException { InputSource inputSource = new InputSource(new StringReader(xmlString)); return (getXMLNode(inputSource)); }
From source file:Main.java
public static Document getAbsoluteXML(Node xml) { Document doc = null;//from w w w.j ava 2s .c o m String xmlContent = getString(xml); try { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = db.parse(new InputSource(new ByteArrayInputStream(xmlContent.getBytes("utf-8")))); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return doc; }
From source file:Main.java
/** * Transforms a string representation to a DOM representation * @param xmlString XML as string/*from ww w . j av a2s . c om*/ * @return DOM representation of String * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static Element stringToDOM(String xmlString) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder builder = dbf.newDocumentBuilder(); Reader reader = new StringReader(xmlString); InputSource src = new InputSource(reader); Document domDoc = builder.parse(src); return domDoc.getDocumentElement(); }
From source file:Main.java
public static XMLReader parse(InputStream in, ContentHandler handler) throws SAXException, IOException { XMLReader reader = XMLReaderFactory.createXMLReader(); reader.setContentHandler(handler);/* ww w .ja v a2s . co m*/ reader.parse(new InputSource(in)); in.close(); return reader; }
From source file:Main.java
/** * Creates an {@link InputSource} for the XML document in the given * {@link String}./*from w ww.j av a 2s.c o m*/ * * @param xml the XML document as a {@link String} * * @return an input source for the XML document */ public static InputSource stringInputSource(String xml) { return new InputSource(new StringReader(xml)); }
From source file:Main.java
public static Document parseDocument(final Reader reader) throws ParserConfigurationException, SAXException, IOException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final Document document = builder.parse(new InputSource(reader)); return document; }
From source file:Main.java
public static Document parse(String text) throws IOException, SAXException { final DocumentBuilder documentBuilder = DOCUMENT_BUILDER.get(); final Document document = documentBuilder.parse(new InputSource(new StringReader(text))); documentBuilder.reset();/* www.jav a 2 s . com*/ return document; }