List of usage examples for org.xml.sax InputSource setCharacterStream
public void setCharacterStream(Reader characterStream)
From source file:Main.java
public static Document parseTheXml(String thePage) throws SAXException, IOException, ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(thePage)); return db.parse(is); //build dat DOM document! }
From source file:Main.java
public static String getNodeCount(String nodeName, String xmlString) { String response = ""; XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); try {// w w w .jav a 2 s .co m XPathExpression xPathExpression = xPath.compile("count(//" + nodeName + ")"); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xmlString)); response = xPathExpression.evaluate(is); } catch (XPathExpressionException e) { e.printStackTrace(); } return response; }
From source file:Main.java
/** * This method will Read the XML and act accordingly * * @param xmlString - the XML String//from ww w .ja v a 2 s.c o m * @return the list of elements within the XML */ public static Document readXML(String xmlString) throws SAXParseException, SAXException, ParserConfigurationException, IOException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); InputSource xmlStream = new InputSource(); xmlStream.setCharacterStream(new StringReader(xmlString)); return dBuilder.parse(xmlStream); }
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 w w .j a va 2 s .c o m*/ }
From source file:Main.java
public static Document xmlFromString(String xml) { Document doc;/*from w ww. j ava2 s . c om*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xml)); doc = db.parse(is); } catch (ParserConfigurationException e) { System.out.println("XML parse error: " + e.getMessage()); return null; } catch (SAXException e) { System.out.println("Wrong XML file structure: " + e.getMessage()); return null; } catch (IOException e) { System.out.println("I/O exeption: " + e.getMessage()); return null; } return doc; }
From source file:Main.java
static public Document getDomElement(String xml) { Document doc = null;/*from w ww . jav a 2s.c o m*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); //Set the input to our xml string InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xml)); doc = db.parse(is); } catch (ParserConfigurationException e) { Log.e("Error: ", e.getMessage()); return null; } catch (SAXException e) { Log.e("Error: ", e.getMessage()); return null; } catch (IOException e) { Log.e("Error: ", e.getMessage()); return null; } //return DOM return doc; }
From source file:Main.java
public static Map handleXMLResponse(HttpResponse response) { Map<String, String> oauthResponse = new HashMap<String, String>(); try {//from w ww . j a va 2s. co m String xmlString = EntityUtils.toString(response.getEntity()); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); InputSource inStream = new InputSource(); inStream.setCharacterStream(new StringReader(xmlString)); Document doc = db.parse(inStream); System.out.println("********** XML Response Received **********"); parseXMLDoc(null, doc, oauthResponse); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Exception occurred while parsing XML response"); } return oauthResponse; }
From source file:Main.java
/** * Reads an xml string into XML Document. * /*from w ww .ja v a 2 s. c om*/ * @param xmlStr String containing xml * @return xml Document * @throws Exception */ public static Document readXmlString(String xmlStr) throws Exception { Document doc = null; try { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); DocumentBuilder builder = domFactory.newDocumentBuilder(); InputSource inStream = new InputSource(); inStream.setCharacterStream(new StringReader(xmlStr)); doc = builder.parse(inStream); } catch (ParserConfigurationException e) { throw new Exception(e.getMessage(), e); } catch (SAXException e) { throw new Exception(e.getMessage(), e); } catch (IOException e) { throw new Exception(e.getMessage(), e); } catch (Exception e) { throw new Exception(e.getMessage(), e); } return doc; }
From source file:Main.java
/** * DOM Parser method. /*from w ww . j ava 2 s.c o m*/ * Converts the input xmlString into List<String> of all the text in the nodes named nodeName. * The memory footprint is small enough when we deal with less than 200 tweets. * * @param xmlString * The xml string * @param nodeName * the name of the node, we are intersted in (e.g. "text") * @param trimString * if we want to trim a particular pattern from every node's text, we pass it as trimString, e.g. "(https?://[^\\s]+)" * @return List of all the text strings from every node named nodeName. trimString text is trimmed from the result strings. */ public static List<String> parseNodesFromXml(String xmlString, String nodeName, String trimString) { List<String> nodeTexts = new ArrayList<String>(); try { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource inputSource = new InputSource(); inputSource.setCharacterStream(new StringReader(xmlString)); try { Document document = db.parse(inputSource); NodeList nodes = document.getElementsByTagName(nodeName); for (int i = 0; i < nodes.getLength(); i++) { String nodeText = nodes.item(i).getTextContent(); String trimmedNodeText = trimStringFromText(trimString, nodeText); nodeTexts.add(trimmedNodeText); } } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } catch (ParserConfigurationException e) { e.printStackTrace(); } return nodeTexts; }
From source file:Main.java
/** * @param reader/*from w w w .j a v a 2 s. c om*/ * @param parser * @param output * @throws TransformerConfigurationException * @throws TransformerFactoryConfigurationError * @throws TransformerException */ public static void write(Reader reader, XMLReader parser, Result output) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { SAXSource input = new SAXSource(); InputSource inputSource = new InputSource(); inputSource.setCharacterStream(reader); input.setInputSource(inputSource); input.setXMLReader(parser); write(input, output); }