0

How do I get the values of id, created_at and updated at from an XML response after creating a new record on a web server.

The web server responds with an xml response as follows

<mobile-user>
    <active type="boolean">true</active>
    <created-at type="datetime">2011-10-14T14:20:51Z</created-at>
    <id type="integer">4</id>
    <quiz-id type="integer">0</quiz-id>
    <updated-at type="datetime">2011-10-14T14:20:51Z</updated-at>
</mobile-user>

The code I use to create the record on the android app (inside an intent service) looks like this

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(NEW_USER_URL);
    StringBuilder sb = new StringBuilder();

    try {
    sb.append("<mobile_user>");
    sb.append("<auth>");
    sb.append(regId);
    sb.append("</auth>");
    sb.append("<quiz_id>");
    sb.append("1");
    sb.append("</quiz_id>");
    sb.append("</mobile_user>");

    StringEntity entity = new StringEntity(sb.toString(), "UTF-8");
    httppost.setEntity(entity);  
    httppost.addHeader("Accept", "application/xml");
    httppost.addHeader("Content-Type", "application/xml");

    HttpResponse response = httpclient.execute(httppost);       
    String responseXml = EntityUtils.toString(response.getEntity());
...

So I assume (correctly?) that responseXml string contains the xml response above All I need to do now is as efficiently as possible extract the id, created_at and updated_at values from that string into a new string for each value.

Any help appreciated

1 Answer 1

2

Using DOM it seems to be like:

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        Document dom;
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            StringReader reader = new StringReader(responseXml);
            InputSource source = new InputSource(reader);
            dom = builder.parse(source);
        } catch (ParserConfigurationException e) {
        } catch (SAXException e) {
        } catch (IOException e) {
        }
        if (dom != null) {
            Element root = dom.getDocumentElement();
            NodeList idItem = root.getElementsByTagName("id");
            NodeList createdAtItem = root.getElementsByTagName("created-at");
            NodeList updatedAtItem = root.getElementsByTagName("updated-at");

            String id = idItem.item(0).getChildNodes().item(0).getNodeValue();
            String createdAt = createdAtItem.item(0).getChildNodes().item(0).getNodeValue();
            String updatedAt = updatedAtItem.item(0).getChildNodes().item(0).getNodeValue();
        }

Ugly variant, but it must be work.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. To get your code to work all I had to do was add the factory declaraton {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();} and tidy up the element names (- instead of underscores - My Bad) Got it working really well and now have a better understanding of the whole thing. Thanks again. Life Saver :)
Yeah, lost fatory definition. :) Added it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.