List of usage examples for java.io StringReader StringReader
public StringReader(String s)
From source file:com.xyxy.platform.modules.extension.tools.FreeMarkers.java
/** * ?//from w w w . j a v a 2 s . c o m */ public static String renderString(String templateString, Map<String, ?> model) { try { StringWriter result = new StringWriter(); Template t = new Template("default", new StringReader(templateString), new Configuration()); t.process(model, result); return result.toString(); } catch (Exception e) { throw Exceptions.unchecked(e); } }
From source file:Main.java
public static Object parseRequestObjectFromSoap(String soapXml) throws Exception { Object result = null;/*from www . ja v a 2 s. com*/ if (soapXml != null && soapXml.trim().length() > 0) { DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance(); xmlFact.setNamespaceAware(true); DocumentBuilder builder = xmlFact.newDocumentBuilder(); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); StringReader xsr = new StringReader(soapXml); InputSource is = new InputSource(xsr); Document doc = builder.parse(is); //Node requestNode = (Node) xpath.evaluate("/soap:Envelope/soap:Body/*", doc, XPathConstants.NODE); Node requestNode = (Node) xpath.evaluate("/*[local-name()='Envelope']/*[local-name()='Body']/*", doc, XPathConstants.NODE); JAXBContext ctx = JAXBContext.newInstance("xo.xoi.orderentry.ebonding"); Unmarshaller unmarshaller = ctx.createUnmarshaller(); result = unmarshaller.unmarshal(requestNode); } return result; }
From source file:Main.java
public static String pretty(String xml) { try {//from w w w. ja va2s .com Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(new StringWriter()); Source source = new StreamSource(new StringReader(xml)); transformer.transform(source, result); return result.getWriter().toString().replace("\r\n", "\n").replace("\n\r", "\n").replace("\r", "\n"); } catch (TransformerFactoryConfigurationError | TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Read an input stream in as an XML document. * @param xmlString/*from w w w . ja v a 2 s. c o m*/ * @return the XML document */ public static Document readXmlDocumentFromString(String xmlString) { try { DocumentBuilder documentBuilder = documentBuilderFactory.get().newDocumentBuilder(); return documentBuilder.parse(new InputSource(new StringReader(xmlString))); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } catch (SAXException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:Main.java
private static String indent(String xml) { try {//from w w w. j a v a 2 s . co m InputSource is = new InputSource(new StringReader(xml)); Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is); OutputFormat format = new OutputFormat(); format.setIndenting(true); format.setIndent(4); Writer out = new StringWriter(); new XMLSerializer(out, format).serialize(document); return out.toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Get root element from XML String/*w w w . ja v a 2s . c o m*/ * * @param arg * XML String * @return Root Element * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static Element getDocumentElementFromString(String arg) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf; DocumentBuilder db; Document document; dbf = DocumentBuilderFactory.newInstance(); db = dbf.newDocumentBuilder(); document = db.parse(new InputSource(new StringReader(arg))); Element element = document.getDocumentElement(); return element; }
From source file:Main.java
public static Document loadFromStream(InputStream inputStream) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false);// www.ja v a 2 s.c o m factory.setNamespaceAware(false); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { //System.out.println("Ignoring " + publicId + ", " + systemId); return new InputSource(new StringReader("")); } }); //System.out.println(StringUtil.toString(inputStream)); Document doc = builder.parse(inputStream); return doc; }
From source file:Main.java
/** * Creates an XML Source from the given XML String. * @param s//from w w w. ja va 2 s. c o m * XML String. * @return * XML Source. */ public static Source source(String s) { return new StreamSource(new StringReader(s)); }
From source file:Main.java
public static String formatXML(String unformatted) throws SAXException, IOException, TransformerException, ParserConfigurationException { if (unformatted == null) return null; // remove whitespaces between xml tags String unformattedNoWhiteSpaces = unformatted.toString().replaceAll(">[\\s]+<", "><"); // Instantiate transformer input Source xmlInput = new StreamSource(new StringReader(unformattedNoWhiteSpaces)); StreamResult xmlOutput = new StreamResult(new StringWriter()); // Configure transformer Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(xmlInput, xmlOutput); String formatted = xmlOutput.getWriter().toString(); return formatted; }
From source file:Main.java
private static StreamSource asStreamSource(String xmlString) { return new StreamSource(new StringReader(xmlString)); }