Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to interface with SaxParser code after RESTClient returned XML data?
    text
    copied!<p>I need to parse the XML data returned from accessing a REST-based service to display only one single tag. For example, parse the XML data shown below to display firstname tag and value John only.</p> <pre><code>&lt;company&gt; &lt;staff&gt; &lt;firstname&gt;John&lt;/firstname&gt; &lt;lastname&gt;Doe&lt;/lastname&gt; &lt;/staff&gt; &lt;/company&gt; </code></pre> <p>I am struggling to figure out how to interface with the SAX parsing code once XML data is returned by RESTClient. I tried different approaches after learning from different example codes but still cannot figure it out partly because they do not have the same exact purpose. So please kindly teach me how to call/pass the data to the parsing code and what to return from the parsing code, whether the parsing code should be in a separate class, etc. I am basically clueless without some guidance. Relevant RESTClient code is presented below. Thanks! </p> <pre><code>public class RESTClient { public static String callRESTService(String url) { String result = null; HttpClient httpclient = new DefaultHttpClient(); // Prepare a request object HttpGet httpget = new HttpGet(url); // Execute the request HttpResponse response; try { response = httpclient.execute(httpget); // Get hold of the response entity HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need // to worry about connection release if (entity != null) { InputStream instream = entity.getContent(); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp; try ( sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); DefaultHandler handler = new DefaultHandler(); xr.setContentHandler(handler); InputSource is = new InputSource(instream); xr.parse(is); //what should/can be returned here from the parsing code: //String, InputSource, InputStream?. Convert data type? } catch (SAXException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } ) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload