List of usage examples for org.xml.sax InputSource InputSource
public InputSource(Reader characterStream)
From source file:Main.java
public static String formatXml(String xml) { try {// w w w . j ava 2s . c o m final InputSource src = new InputSource(new StringReader(xml)); final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src) .getDocumentElement(); final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml")); //May need this: System.setProperty(DOMImplementationRegistry. //PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl"); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); final LSSerializer writer = impl.createLSSerializer(); // Set this to true if the output needs to be beautified. writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the declaration is needed to be outputted. writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); return writer.writeToString(document); } catch (Exception e) { System.err.println("Exception thrown"); throw new RuntimeException(e); } }
From source file:Main.java
public static Document loadDocument(Reader reader) throws SAXException, IOException { return getBuilder().parse(new InputSource(reader)); }
From source file:Main.java
public static Document loadXMLFromString(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); return builder.parse(is); }
From source file:Main.java
public static boolean isValidXml(String xml) { try {//from w ww.jav a 2 s . c o m StringReader reader = new StringReader(xml); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); db.parse(new InputSource(reader)); } catch (Exception e) { return false; } return true; }
From source file:Main.java
public static Document stringToDoc(final String xml) throws Exception { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final InputSource is = new InputSource(new StringReader(xml)); return builder.parse(is); }
From source file:Main.java
/** * @Description: This method will return the xml document from the input stream * @param is/*from ww w .j a v a 2 s.c om*/ * @return document */ public static Document loadXML(final InputStream is) { Document document = null; try { InputSource src = new InputSource(is); DOMParser parser = new DOMParser(); parser.parse(src); document = parser.getDocument(); } catch (Exception e) { e.printStackTrace(); } return document; }
From source file:Main.java
public static String prettyFormat(String input) { try {// w ww . ja v a 2 s .c o m final InputSource src = new InputSource(new StringReader(input)); final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src) .getDocumentElement(); final Boolean keepDeclaration = Boolean.valueOf(input.startsWith("<?xml")); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); final LSSerializer writer = impl.createLSSerializer(); writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified. writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted. return writer.writeToString(document); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static Document stringToXML(String xmlString) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/* w ww . j ava 2 s. com*/ Document document = null; try { document = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xmlString))); } catch (Exception e) { e.printStackTrace(); } return document; }
From source file:Main.java
public static Document load(URI uri) { return load(new InputSource(uri.toString())); }
From source file:Main.java
/** Parse a XML document from string */ public static Document parse(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); return builder.parse(is); }