0

So I have been through countless tutorials and tried a bunch of different ideas, but for the life of me can not figure out how to parse my xml feed below. I'm trying to access the values in the 'top_guessed_letters' & top_not_guessed_letters nodes.

<REQUEST_GUESS_LETTER>
     <top_guessed_letters>E-T-R-C-A</top_guessed_letters>
     <top_not_guessed_letters>O-U-B</top_not_guessed_letters>
     <top_correct>I-P-D</top_correct>
     <smart_guess>
         <smart_guess>R-C-A</smart_guess>
         <smart_guess>E-T-R</smart_guess>
      </smart_guess>
</REQUEST_GUESS_LETTER>

Here is my code for myActivity:

XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL);
    Document doc = parser.getDomElement(xml);

    NodeList nl = doc.getElementsByTagName("REQUEST_GUESS_LETTER");

    for (int i = 0; i < nl.getLength(); i++) {

        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);

        map.put("top_guessed_letters", parser.getValue(e, top_guessed_letters));

        menuItems.add(map);
    }

    ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.list_item,
            new String[]{top_guessed_letters}, new int[]{
            R.id.topguessed});

    listView.setAdapter(adapter);

XMLPullParser Activity

public class XMLParser {
public String getXmlFromUrl(String url) {
    String xml = null;

    Log.d("xmlTree", url);


    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    Log.d("getXmlFromUrl ", xml);
    return xml;


}

public Document getDomElement(String xml) {
    Document doc = null;
    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) {
        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;
    }

    Log.d("thisstuff", doc.toString());
    // return DOM
    return doc;

}

public String getValue(Element item, String str) {
    NodeList n = item.getElementsByTagName(str);
    return this.getElementValue(n.item(0));
}

public final String getElementValue(Node elem) {
    Node child;
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if (child.getNodeType() == Node.TEXT_NODE) {
                    return child.getNodeValue();
                }
            }
        }
    }
    return "";
}

}

Any help would be appreciated.

1 Answer 1

0

I suggest you take a look at the simple library for (de)serializing XML.

All it takes is creating a class and deserialize the xml. For example:

@Root(name="REQUEST_GUESS_LETTER")
public class Example {

   @Element(name="top_guessed_letters")
   private String guessed;

   @Element(name="top_not_guessed_letters")
   private String not_guessed;

   public Example() {
      super();
   }  

   public Example(String guessed, String not_guessed) {
      this.guessed = guessed;
      this. not_guessed = not_guessed;
   }

   public String getGuessed() {
      return guessed;
   }

   public String getNotGuessed() {
      return not_guessed;
   }
}

Then execute this code.

Serializer serializer = new Persister();
File source = new File("example.xml");

Example example = serializer.read(Example.class, source);
Sign up to request clarification or add additional context in comments.

2 Comments

This seems perfect and exactly what I am looking for. My question would be how could I get my xml URL feed converted to a file?
It doesn't need to be a File. It could also be an InputStream. See the documentation: simple.sourceforge.net/download/stream/doc/javadoc/org/…

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.