List of usage examples for java.io StringReader StringReader
public StringReader(String s)
From source file:Main.java
/** * Return an object for xml/*from ww w .jav a 2 s . com*/ * * @param cls * @param xml * @return */ public static Object objectFromXML(Class<?> cls, String xml) { return JAXB.unmarshal(new StringReader(xml), cls); }
From source file:Main.java
public static String getFormattedXml(String xmlString) { // /////////////////////////////////////////////////////////////// // Declarations // /////////////////////////////////////////////////////////////// Source xmlInput = null;/* w w w. j ava 2 s. c o m*/ StringWriter stringWriter = null; StreamResult xmlOutput = null; TransformerFactory transformerFactory = null; Transformer transformer = null; String formattedXml = null; // /////////////////////////////////////////////////////////////// // Code // /////////////////////////////////////////////////////////////// try { xmlInput = new StreamSource(new StringReader(xmlString)); stringWriter = new StringWriter(); xmlOutput = new StreamResult(stringWriter); transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 4); transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); formattedXml = xmlOutput.getWriter().toString(); } catch (Exception e) { // To Do: Handle Exception.. } return formattedXml; }
From source file:Main.java
/** * Creates an {@link InputSource} for the XML document in the given * {@link String}./* w w w .ja va2 s.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:ca.craigthomas.visualclassifier.dataset.DataSetReader.java
/** * Read from a CSV file, and return the samples as a list of doubles. * /* www . j a va 2 s . c o m*/ * @param filename the name of the file to read from * @return the list of samples from the file * @throws IOException */ public static List<List<Double>> readCSVFile(String filename) throws IOException { File file = new File(filename); String fileContents = FileUtils.readFileToString(file); Reader reader = new StringReader(fileContents); CSVFormat format = CSVFormat.EXCEL; CSVParser parser = new CSVParser(reader, format); List<CSVRecord> records = parser.getRecords(); List<List<Double>> inputs = new ArrayList<List<Double>>(); for (CSVRecord record : records) { List<Double> inputLine = new ArrayList<Double>(); for (int index = 0; index < record.size(); index++) { String value = record.get(index); inputLine.add(Double.parseDouble(value)); } inputs.add(inputLine); } parser.close(); return inputs; }
From source file:Main.java
static public XmlPullParser createParser(String xml) throws XmlPullParserException { XmlPullParser xpp = createParser();//from w ww . ja v a 2 s .c om xpp.setInput(new StringReader(xml)); return xpp; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T readXMLFromString(Class<?> class1, String content) throws JAXBException, FileNotFoundException, SAXException, ParserConfigurationException { JAXBContext context = JAXBContext.newInstance(class1); Unmarshaller um = context.createUnmarshaller(); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); spf.setFeature("http://xml.org/sax/features/validation", false); XMLReader xr = (XMLReader) spf.newSAXParser().getXMLReader(); try (StringReader reader = new StringReader(content)) { SAXSource source = new SAXSource(xr, new InputSource(reader)); T obj = (T) um.unmarshal(source); return obj; }//from w ww.ja va2s . c o m }
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();//from ww w .j a v a 2 s .co m return document; }
From source file:Main.java
/** * Converts the xml into a document//from www. j a v a 2 s . c o m * * @param xml the xml formated string * @return a new document * @throws SAXException * @throws IOException * @throws ParserConfigurationException */ public static Document xmlToDoc(String xml) throws SAXException, IOException, ParserConfigurationException { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new InputSource(new StringReader(xml))); return doc; }
From source file:Main.java
public static <T extends Object> T unmarshalFromString(Class clz, String input) throws JAXBException { JAXBContext context = JAXBContext.newInstance(clz); Unmarshaller unmarshaller = context.createUnmarshaller(); T unobj = (T) unmarshaller.unmarshal(new StreamSource(new StringReader(input.toString()))); return unobj; }
From source file:Main.java
public static Document xmlFromString(final String data) throws RuntimeException { if (data == null) { throw new RuntimeException("data is null"); }/*www.j av a2 s . co m*/ StringReader stringReader = new StringReader(data); InputSource inputSource = new InputSource(stringReader); // parse inputs DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); return builder.parse(inputSource); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } catch (SAXException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }