List of usage examples for java.io StringReader StringReader
public StringReader(String s)
From source file:Main.java
/** * @param xml/* w w w .j ava 2 s. c om*/ * @return pretty xml */ public static String prettyXml(String xml) { Transformer serializer; try { Source source = new StreamSource(new StringReader(xml)); StringWriter writer = new StringWriter(); serializer = transFactory.newTransformer(); // Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); serializer.transform(source, new StreamResult(writer)); return writer.toString(); } catch (Exception e) { return xml; } }
From source file:Main.java
public static Document parseXml(String s) throws IOException, SAXException, ParserConfigurationException { DocumentBuilder builder = getBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(s)); Document ret = builder.parse(is); return ret;//from w ww. j ava2 s .com }
From source file:Main.java
/** * Transforms a string representation to a DOM representation * @param xmlString XML as string// w ww . j a v a2s . c o m * @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 Element createParagraphElement(String name, String content, Document doc) { Element element = doc.createElement(name); BufferedReader br = new BufferedReader(new StringReader(content)); try {//from www .j a v a 2 s.c o m while (br.ready()) { // Create an element for this node String p = br.readLine(); // To avoid an infinite loop if (p == null) break; Element pe = doc.createElement("p"); //Add text to paragraph node Text t = doc.createTextNode(p); pe.appendChild(t); element.appendChild(pe); } } catch (IOException ioe) { ioe.printStackTrace(); } return element; }
From source file:Main.java
/** * @param string Creates a {@link Document} from a string. * @return A {@link Document} representation of the string. * @throws ParserConfigurationException if a DocumentBuilder cannot be * created which satisfies the configuration requested * @throws SAXException if any parse errors occur * @throws IOException if any IO errors occur. * *//*from w ww. j a v a 2 s . c o m*/ public static Document stringToXml(String string) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(string)); if (builder.isNamespaceAware()) { System.out.println("#######################3Is aware"); } else { System.out.println("#######################Not aware"); } return builder.parse(is); }
From source file:Main.java
/** * Parse une string contenant un document XML dans un DOM Element. * /*w ww . j a v a 2 s .c o m*/ * @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
/** * Uses the current DocumentBuilderFactory to converts a String * representation of XML into a Document. * * @param xml XML serialized as a String * @param nameSpaceAware determines whether the constructed Document * is name-space aware/*from w w w . j av a 2 s . c om*/ * @return Document */ public static Document stringToDocument(String xml, boolean nameSpaceAware) { Document document = null; try { if (xml != null) { StringReader reader = new StringReader(xml); InputSource input = new InputSource(reader); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(nameSpaceAware); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(input); } } catch (Exception ex) { ex.printStackTrace(); } return document; }
From source file:org.json.StackExchangeAPI.java
private static void parseStackExchange(String jsonStr) { JsonReader reader = null;/*from w w w .j a v a 2s .c o m*/ try { reader = Json.createReader(new StringReader(jsonStr)); JsonObject jsonObject = reader.readObject(); reader.close(); JsonArray array = jsonObject.getJsonArray("items"); for (JsonObject result : array.getValuesAs(JsonObject.class)) { JsonObject ownerObject = result.getJsonObject("owner"); // int ownerReputation = ownerObject.getInt("reputation"); // System.out.println("Reputation:"+ownerReputation); int viewCount = result.getInt("view_count"); System.out.println("View Count :" + viewCount); int answerCount = result.getInt("answer_count"); System.out.println("Answer Count :" + answerCount); String link = result.getString("link"); System.out.println("URL: " + link); String title = result.getString("title"); System.out.println("Title: " + title); String body = result.getString("body"); System.out.println("Body: " + body); JsonArray tagsArray = result.getJsonArray("tags"); StringBuilder tagBuilder = new StringBuilder(); int i = 1; for (JsonValue tag : tagsArray) { tagBuilder.append(tag.toString()); if (i < tagsArray.size()) tagBuilder.append(","); i++; } System.out.println("Tags: " + tagBuilder.toString()); System.out.println("------------------------------------------"); } } catch (Exception ex) { ex.printStackTrace(); } }
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 //ww w . j av a 2 s . c om * @throws IOException */ public static Node getXMLNode(String xmlString) throws ParserConfigurationException, SAXException, IOException { InputSource inputSource = new InputSource(new StringReader(xmlString)); return (getXMLNode(inputSource)); }