DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader("<root><nod1></node1></root>")); Document doc = db.parse(is);
private static InputSource createInputSource(String publicId, Template raw) throws IOException, SAXException { StringWriter sw = new StringWriter(); try { raw.process(Collections.EMPTY_MAP, sw); } catch (TemplateException e) { throw new SAXException(e); } InputSource is = new InputSource(); is.setPublicId(publicId); is.setSystemId(raw.getName()); is.setCharacterStream(new StringReader(sw.toString())); return is; }
private static InputSource streamSourceToInputSource( StreamSource ss ) { InputSource is = new InputSource(); is.setSystemId( ss.getSystemId() ); is.setByteStream( ss.getInputStream() ); is.setCharacterStream( ss.getReader() ); return is; }
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource src = new InputSource(); src.setCharacterStream(new StringReader(xml)); Document doc = builder.parse(src); String age = doc.getElementsByTagName("age").item(0).getTextContent(); String name = doc.getElementsByTagName("name").item(0).getTextContent();
InputSource inputSource = new InputSource(); inputSource.setEncoding("UTF-8"); inputSource.setCharacterStream(new StringReader(response));
private static InputSource streamSourceToInputSource( StreamSource ss ) { InputSource is = new InputSource(); is.setSystemId( ss.getSystemId() ); is.setByteStream( ss.getInputStream() ); is.setCharacterStream( ss.getReader() ); return is; }
String xml = "<message>HELLO!</message>"; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; try { db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xml)); try { Document doc = db.parse(is); String message = doc.getDocumentElement().getTextContent(); System.out.println(message); } catch (SAXException e) { // handle SAXException } catch (IOException e) { // handle IOException } } catch (ParserConfigurationException e1) { // handle ParserConfigurationException }
response = response.replaceAll("&", "&"); InputSource inputSource = new InputSource(); inputSource.setEncoding("UTF-8"); Log.i("TAG", "response" + response); inputSource.setCharacterStream(new StringReader(response)); xr.parse(inputSource);
@Override public void writeTo(Source source, Class<?> t, Type gt, Annotation[] as, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { try { if (source instanceof StreamSource) { StreamSource stream = (StreamSource) source; InputSource inputStream = new InputSource(stream.getInputStream()); inputStream.setCharacterStream(inputStream.getCharacterStream()); inputStream.setPublicId(stream.getPublicId()); inputStream.setSystemId(source.getSystemId()); source = new SAXSource(saxParserFactory.get().newSAXParser().getXMLReader(), inputStream); } StreamResult sr = new StreamResult(entityStream); transformerFactory.get().newTransformer().transform(source, sr); } catch (SAXException ex) { throw new InternalServerErrorException(ex); } catch (ParserConfigurationException ex) { throw new InternalServerErrorException(ex); } catch (TransformerException ex) { throw new InternalServerErrorException(ex); } } }
HttpResponse response = client.execute(post); HttpEntity r_entity = response.getEntity(); String xmlString = EntityUtils.toString(r_entity); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); InputSource inStream = new InputSource(); inStream.setCharacterStream(new StringReader(xmlString)); Document doc = db.parse(inStream); String playcount = "empty"; NodeList nl = doc.getElementsByTagName("playcount"); for(int i = 0; i < nl.getLength(); i++) { if (nl.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nl.item(i); playcount = nameElement.getFirstChild().getNodeValue().trim(); } }
try { httpTransport.call(SOAP_ACTION, envelope); sb.append(httpTransport.requestDump); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } InputSource inStream = new InputSource(); inStream.setCharacterStream(new StringReader(sb.toString()));
@Override public void writeTo(Source source, Class<?> t, Type gt, Annotation[] as, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { try { if (source instanceof StreamSource) { StreamSource stream = (StreamSource) source; InputSource inputStream = new InputSource(stream.getInputStream()); inputStream.setCharacterStream(inputStream.getCharacterStream()); inputStream.setPublicId(stream.getPublicId()); inputStream.setSystemId(source.getSystemId()); source = new SAXSource(saxParserFactory.get().newSAXParser().getXMLReader(), inputStream); } StreamResult sr = new StreamResult(entityStream); transformerFactory.get().newTransformer().transform(source, sr); } catch (SAXException ex) { throw new InternalServerErrorException(ex); } catch (ParserConfigurationException ex) { throw new InternalServerErrorException(ex); } catch (TransformerException ex) { throw new InternalServerErrorException(ex); } } }
protected static String injectAttrs(String content, String classVal, String styleVal) throws ParserConfigurationException, SAXException, IOException { // Convert xhtml string to DOM int firstChar = content.codePointAt(0); if (firstChar==0xFEFF) { log.info("Removing BOM.."); content = content.substring(1); } InputSource is = new InputSource(); is.setCharacterStream(new StringReader(content)); Document doc = XmlUtils.getNewDocumentBuilder().parse(is); // traverse + inject (need traverse, to get p in table) injectAttrs(doc, classVal, styleVal); // convert back to string again return XmlUtils.w3CDomNodeToString(doc); }
response = response.replaceAll("&", "&"); // Your Server Response InputSource inputSource = new InputSource(); inputSource.setEncoding("UTF-8"); Log.i("TAG", "Response>>>>" + response); inputSource.setCharacterStream(new StringReader(response)); xr.parse(inputSource);
/** * Attempt to obtain a SAX InputSource object from a Source * object. * * @param source Must be a non-null Source reference. * * @return An InputSource, or null if Source can not be converted. */ public static InputSource sourceToInputSource(Source source) { if (source instanceof SAXSource) { return ((SAXSource) source).getInputSource(); } else if (source instanceof StreamSource) { StreamSource ss = (StreamSource) source; InputSource isource = new InputSource(ss.getSystemId()); isource.setByteStream(ss.getInputStream()); isource.setCharacterStream(ss.getReader()); isource.setPublicId(ss.getPublicId()); return isource; } else { return null; } } }
private static Document XMLfromString(String v) { Document doc = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(v)); doc = db.parse(is); } catch (ParserConfigurationException e) { } catch (SAXException e) { } catch (IOException e) { } return doc; }
getsyncFlag(String feedData) { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); xr.setContentHandler(this); InputSource is=new InputSource(); is.setCharacterStream(new StringReader((feedData))); xr.parse(is); }
private static InputSource streamSourceToInputSource( StreamSource ss ) { InputSource is = new InputSource(); is.setSystemId( ss.getSystemId() ); is.setByteStream( ss.getInputStream() ); is.setCharacterStream( ss.getReader() ); return is; }
public LibvirtStorageVolumeDef parseStorageVolumeXML(String volXML) { DocumentBuilder builder; try { builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(volXML)); Document doc = builder.parse(is); Element rootElement = doc.getDocumentElement(); String VolName = getTagValue("name", rootElement); Element target = (Element)rootElement.getElementsByTagName("target").item(0); String format = getAttrValue("type", "format", target); Long capacity = Long.parseLong(getTagValue("capacity", rootElement)); return new LibvirtStorageVolumeDef(VolName, capacity, LibvirtStorageVolumeDef.VolumeFormat.getFormat(format), null, null); } catch (ParserConfigurationException e) { s_logger.debug(e.toString()); } catch (SAXException e) { s_logger.debug(e.toString()); } catch (IOException e) { s_logger.debug(e.toString()); } return null; }
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); XMLHandler xh = new XMLHandler(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xmlst)); xr.setContentHandler(xh); xr.parse(new InputSource(is.getByteStream()));