6

Here's an example of my XML file:

<?xml version="1.0" encoding="utf-8"?>
<assurances>
<assurance hasReference="true">
    <message>You're Awesome!</message>
    <reference>Genesis 1:26</reference>
</assurance>
<assurance hasReference="true">
    <message>Your Wonderfull!</message>
    <reference>Genesis 1:26</reference>
</assurance>
</assurances>

I'm using code like this to try to retrieve it:

int eventType = -1;
    while(eventType != XmlResourceParser.END_DOCUMENT)
    {
        XmlResourceParser assurances = getResources().getXml(R.xml.assurances); 
        String name = assurances.getText();
        Log.d(TAG, name);

        try {
            if (assurances.getEventType() == XmlResourceParser.START_TAG) {
                String s = assurances.getName();

                if (s.equals("assurance")) {
                    String strMessage = assurances.getAttributeValue(null, "message");
                    String strReference = assurances.getAttributeValue(null, "reference");

                    Log.d(TAG, strMessage);
                    Log.d(TAG, strReference);
                }
            }
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

It's not getting the data and I'm not sure where to go from here.

jaysonpowers
  • 290
  • 2
  • 3
  • 16

2 Answers2

5

You don't need to use the getAttributeValue method instead you need to use the getText() method. I have done some changes to your code so if you use below code then it will work fine:

int eventType = -1;
while(eventType != XmlResourceParser.END_DOCUMENT)
{
    XmlResourceParser assurances = getResources().getXml(R.xml.assurances); 
    String name = assurances.getText();
    Log.d(TAG, name);

    try {
        if (assurances.getEventType() == XmlResourceParser.START_TAG) {
            String s = assurances.getName();

            if (s.equals("assurance")) {
                assurances.next();   /// moving to the next node
                if(assurances.getName() != null && assurances.getName().equalsIgnoreCase("message")){
                    String strMessage = assurances.getText();  ///to get value getText() method should be used
                    assurances.next();   ///jumping on to the next node
                String strReference = assurances.getText();  ////similar to above
            }

                Log.d(TAG, strMessage);
                Log.d(TAG, strReference);
            }
        }
    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
user_CC
  • 4,686
  • 3
  • 20
  • 15
  • Thanks for that, I did get it working with that code, I ended up doing a sqlite database though. – jaysonpowers Apr 05 '13 at 15:33
  • How would you chose my question on stackoverflow. This is the link. https://stackoverflow.com/questions/53570140/how-to-parse-an-xml-to-java-class-with-recycler-view-or-in-adapter-of-recyclervi/53593870?noredirect=1#comment94077713_53593870 – TheCoderGuy Dec 04 '18 at 17:21
1

Hope this will help you on the way. Regards S

public static void main(String[] args) {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new File("c:\\_work\\test.xml"));

        //get nodes by name
        NodeList nl = doc.getElementsByTagName("assurance");
        int nbrOfElements = nl.getLength();
        for (int i = 0; i < nbrOfElements; i++) {
            Element e = (Element) nl.item(i);
            System.out.print(e.getTagName());
            System.out.print(":");
            System.out.println(e.getTextContent());
        }
    } catch (Exception ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}
stbas
  • 61
  • 2