List of usage examples for java.io StringReader StringReader
public StringReader(String s)
From source file:Main.java
public static Document toXML(String input) { return toXML(new InputSource(new StringReader(input))); }
From source file:Main.java
public static String prettyPrintXML(String xml) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); StreamSource source = new StreamSource(new StringReader(xml)); transformer.transform(source, result); return result.getWriter().toString(); }
From source file:Main.java
public static Document parseXmlDocument(String xml, boolean namespaceAware) throws ParserConfigurationException, SAXException, IOException { DocumentBuilder docBuilder = buildDocumentBuilder(namespaceAware); Document doc = docBuilder.parse(new InputSource(new StringReader(xml))); // normalize text representation doc.getDocumentElement().normalize(); return doc;/*from w ww.j av a2 s .c o m*/ }
From source file:Main.java
/** * This method validate XML by input XML as String and XSD path to File. * * @param xml input XML as String// ww w . j av a 2 s . c o m * @param xsdPath input XSD File Path * * @return true or false, valid or not */ public static boolean validateXMLByXSD(String xml, String xsdPath) { try { SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new URL(xsdPath)).newValidator() .validate(new StreamSource(new StringReader(xml))); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
From source file:Main.java
/** * XML to Object// w ww. ja va2 s. co m * @param <T> T * @param clazz clazz * @param xml xml * @return T */ public static <T> T convertToObject(Class<T> clazz, String xml) { return convertToObject(clazz, new StringReader(xml)); }
From source file:Main.java
/** * Returns root tagname/*from w ww. j av a 2 s. c o m*/ * * @param xml The xml document to be searched. * @return String result. */ public static String getRootTagName(String xml) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder builder = dbf.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(xml))); return document.getDocumentElement().getTagName(); }
From source file:Main.java
/** * @param xml/* w w w.j a v a2 s . c om*/ */ public static void prettyPrint(String xml) { Source xmlInput = new StreamSource(new StringReader(xml)); StreamResult xmlOutput = new StreamResult(new StringWriter()); Transformer transformer = null; try { transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); try { transformer.transform(xmlInput, xmlOutput); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } String formattedxml = xmlOutput.getWriter().toString(); System.out.println(formattedxml); }
From source file:Main.java
public static Object xmlToJaxb(Class<?> xmlClass, String xml) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); JAXBElement<?> element; try (StringReader reader = new StringReader(xml)) { element = (JAXBElement<?>) unmarshaller.unmarshal(reader); }/*from w ww .j a v a2 s. c om*/ return element.getValue(); }
From source file:Main.java
/** * @param xml//from w w w .j a v a2 s . c o m */ public static String prettyPrintToString(String xml) { Source xmlInput = new StreamSource(new StringReader(xml)); StreamResult xmlOutput = new StreamResult(new StringWriter()); Transformer transformer = null; try { transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); try { transformer.transform(xmlInput, xmlOutput); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } String formattedxml = xmlOutput.getWriter().toString(); return formattedxml; }
From source file:Main.java
/** * Parses an XML string./*from ww w . j av a 2s. c o m*/ * @param xmlString the file containing the XML to parse. * @return the XML DOM document. */ public static Document getDocumentFromString(String xmlString) throws Exception { StringReader sr = new StringReader(xmlString); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return db.parse(new InputSource(sr)); }