artist
artist

Reputation: 6371

Need help in reading an xml file

I have created an xml folder under res and placed myxml.xml in xml folder. res/xml/myxml.xml

I want to read the description according to the id number. How can I achieve that? Please help.

myxml.xml is like this;

<?xml version="1.0" encoding="utf-8" ?>
<Books>
  <Number id ="1">
    <Description>This is childrens book.
    </Description>
  </Number> 
  <Number id = "2">
    <Description>This is about cooking.
   </Description>
</Books>

Here is the code I am working with (but doesn't seems to be doing anything)

try{
        XmlPullParser xpp = getResources().getXml(R.xml.myxml);
                while(xpp.getEventType() != XmlPullParser.END_DOCUMENT){
            if(xpp.getEventType() == XmlPullParser.START_TAG){ 
              if(xpp.getName().equals("Description")){

                  show1.setText(" " + xpp.getAttributeValue(1));
              }
            }
        xpp.next();
         }
        }
        catch(Throwable t){

        }

Upvotes: 1

Views: 1813

Answers (9)

VJ V&#233;lan Solutions
VJ V&#233;lan Solutions

Reputation: 6554

I don't think any of your "if" condition is getting satisfies. Return a string that is not empty and see if that gets displayed. If it does, then re-look at your logic for getEventsFromAnXML method.

Upvotes: 0

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

The reason it isn't working is because you are calling xrp.getName() method when your current eventType is XmlPullParser.TEXT.

According to the documentation:

public abstract String getName()

For START_TAG or END_TAG events...If the current event is not START_TAG, END_TAG, or ENTITY_REF, null is returned.

I would create a Stack<String> to hold the names of the element I am currently on. When I get to a start tag, I would push the name to the top, and when i hit an end tag, I would pop the name off the top. This way, when I am in a text element, I would know the whole xPath of my current location.

Upvotes: 1

You most likely have an unexpected situation and your program throws an exception and dies.

Find the printed stack trace and add it to your question. This will most likely tell you why. A very common reason is to have uninitialized variables which you then try to invoke a method on giving a NullPointerException.

Upvotes: 0

ngesh
ngesh

Reputation: 13501

How will it even work when activity has null in it.. u r doin null.getResources();

Upvotes: 2

actionshrimp
actionshrimp

Reputation: 5229

For what you're trying to achieve here, I'd recommend using javax.xml.xpath. Taken from the example on that page, adapted to your XML file (the XPath here will get the description of Number with id attribute of 2):

XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "//Number[@id='2']/Description";

try {
    InputSource inputSource = new InputSource(activity.getResources().openRawResource(R.raw.myxml));
    Node node = (Node) xpath.evaluate(expression, inputSource, XPathConstants.NODE);
    String description = node.getTextContent();
} catch (Exception e) {
    Log.d("ParseException", e.toString());
}

I've updated the example slightly to load the InputSource from a resource input stream. To do this it looks like you'll need to put your xml file in the res/raw folder rather than res/xml (based on this answer: problem loading and parsing xml from resources). I also had to do project > clean to get this to work.

You'll probably also want to handle the parse exception error in a slightly better way!

Upvotes: 2

Jess
Jess

Reputation: 42928

The problem is that you can't access APK resources like they're on the path, because they're compiled into your application. If you want to access a resource directly, try this:

InputStream inputStream = context.getResources().openRawResource(R.xml.myxml);
InputSource inputSource = new InputSource(inputStream);

You may need use move the XML file into the res/raw/ folder and use R.raw.myxml to access it instead.

Upvotes: 0

Lukas Knuth
Lukas Knuth

Reputation: 25755

I wouldn't use XPath for this, the XMLResourceParser-class was meant to do that (tutorial).

Upvotes: 0

George
George

Reputation: 1327

you can use this, I am sure that this help you

XmlResourceParser pars = res.getXml(R.xml.myxml);
while (pars.getEventType() != XmlResourceParser.END_DOCUMENT) {
    if (pars.getEventType() == XmlResourceParser.START_TAG) {
              if(pars.getName().equals("Number")) {
                   int id = pars.getAttributeIntValue(null, "id", 0);
              }
        }
    pars.next();
}
pars.close();

in your Xml you have forgotten to put / in your Number tag

<Number id ="1" />

use this link to understand XmlResourceParser, it is evry easy to use http://www.anddev.org/using_xmlresourceparser_to_parse_custom_compiled_xml-t9313.html

Upvotes: 1

crsierra
crsierra

Reputation: 1045

I noticed in your example the XML appears to missing the closing </Number> tag where the id attribute is equal to "2". Its possible a formatting error such as this may be causing your error.

Upvotes: 0

Related Questions