List of usage examples for org.xml.sax InputSource InputSource
public InputSource(Reader characterStream)
From source file:Main.java
public static Document parseXML(String xml) throws ParserConfigurationException, IOException, SAXException { DocumentBuilder builder;/* w ww.j a va2 s . co m*/ synchronized (factory) { builder = factory.newDocumentBuilder(); } return builder.parse(new InputSource(new StringReader(xml))); }
From source file:Main.java
public static Vector<String> readXMLNode222(String xmlFile, String xpath) { try {// w w w. ja v a 2 s . co m XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); // XPathExpression xPathExpression = // xPath.compile("/history"); File xmlDocument = new File(xmlFile); InputSource inputSource = new InputSource(new FileInputStream(xmlDocument)); // String root = xPath.evaluate("/", inputSource); NodeList nodes = (NodeList) xPath.evaluate(xpath, inputSource, XPathConstants.NODESET); Vector<String> vector = new Vector<String>(); for (int x = 0; x < nodes.getLength(); x++) { vector.add(nodes.item(x).getTextContent()); } return vector; } catch (Exception ex) { ex.printStackTrace(); return new Vector(); } }
From source file:Main.java
public static Node getRoot(String documentAsString) { final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); try {/*from w w w.j a va2 s .c o m*/ final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); final Document document = documentBuilder.parse(new InputSource(new StringReader(documentAsString))); return document.getFirstChild(); } catch (ParserConfigurationException e) { throw new IllegalStateException(e); } catch (SAXException e) { throw new IllegalArgumentException(e); } catch (IOException e) { throw new IllegalStateException(e); } }
From source file:Main.java
public static Document parseDocument(final InputStream inputStream) throws ParserConfigurationException, SAXException, IOException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final Document document = builder.parse(new InputSource(inputStream)); return document; }
From source file:Main.java
/** * src:http://www.journaldev.com/1237/java-convert-string-to-xml-document-and-xml-document-to-string * @param xmlStr//from w ww. j a v a 2s . c om * @return */ public static Document convertStringToDocument(String xmlStr) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(xmlStr))); return doc; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static Document readFileAsDoc(String xml) throws SAXException, IOException, Exception { org.w3c.dom.Document doc = null; StringReader sr = new StringReader(xml); DocumentBuilder bldr = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = bldr.parse(new InputSource(sr)); return doc;//from w w w . j ava 2 s . c om }
From source file:Main.java
public static Document toDocument(String content) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); return content == null ? null : db.parse(new InputSource(new StringReader(content))); }
From source file:Main.java
/** * Convert and xml String to an org.w3c.dom.Element * @param xml - the XML//from w w w . j a va 2 s . c o m * @return - the Element */ public static Element stringToElement(String xml) { Element element = null; try { Document document = getDocumentBuilder().parse(new InputSource(new StringReader(xml))); element = (Element) document.getDocumentElement().cloneNode(true); } catch (Exception e) { throw new RuntimeException(e); } return element; }
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 <T> T unMarshal(String xml, Class<T> clazz, String encoding) { T result = null;/* w ww . ja v a2 s. co m*/ try { ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes()); JAXBContext context = JAXBContext.newInstance(clazz); result = (T) context.createUnmarshaller() .unmarshal(new InputSource(new InputStreamReader(is, encoding))); } catch (JAXBException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; }