0

I'm trying to parse xml with no success the xml is:

<Menu> 
<sunday> 
<food Type="soups">someVal</food>  
<food Type="soups">someVal</food>   
</sunday>  
</Menu>

Im parsing in android using:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

This is how i try to parse it:

                doc.getDocumentElement().normalize();
            typesList = doc.getElementsByTagName("sunday");
            Node node = typesList.item(0);
            Element fstElmnt = (Element) node;
            Attr marakim = fstElmnt.getAttributeNode("soups");
            NodeList marakimList = marakim.getChildNodes();
            Element nameElement = (Element) marakimList.item(0);
            marakimList = nameElement.getChildNodes();
            String test = ((Node)marakimList.item(0)).getNodeValue();

And i get nullPointerException

09-12 16:22:13.703: W/System.err(22570): java.lang.NullPointerException
09-12 16:22:13.710: W/System.err(22570):    at com.bugs3.udios.shultz.ShultzDayChoice$foodTypesTask.doInBackground(ShultzDayChoice.java:116)
09-12 16:22:13.714: W/System.err(22570):    at com.bugs3.udios.shultz.ShultzDayChoice$foodTypesTask.doInBackground(ShultzDayChoice.java:1)
09-12 16:22:13.714: W/System.err(22570):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-12 16:22:13.730: W/System.err(22570):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-12 16:22:13.734: W/System.err(22570):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-12 16:22:13.734: W/System.err(22570):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-12 16:22:13.734: W/System.err(22570):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-12 16:22:13.738: W/System.err(22570):    at java.lang.Thread.run(Thread.java:856)

is there a better way to parse xml with attributes?

thanks very much for the help

Udi Oshi
  • 6,787
  • 7
  • 47
  • 65

3 Answers3

2

This is how I do it though I do not have attributes in my xml. You'd only have to do something like

Element element2 = element.getAttribute("type");

Or something like that just after you use .getElementsByTagName

NodeList nodeList = doc.getElementsByTagName("ConfigIn");
            for(int i = 0; i < nodeList.getLength(); i++)
            {
                Node node = nodeList.item(i);
                if(node.getNodeType() == Node.ELEMENT_NODE)
                {
                    Element element = (Element) node;
                    NodeList nodelist = element.getElementsByTagName("eclairag");
                    Element element1 = (Element) nodelist.item(0);
                    NodeList fstNm = element1.getChildNodes();
                    config_eclairag = fstNm.item(0).getNodeValue();

Hope this can help you in some way...

Ben Lefebvre
  • 359
  • 5
  • 21
  • I do that to parse elements without attributes. Thing is i need to parse some element with attribute and probably need to use class Attr but there are no examples for that... – Udi Oshi Sep 12 '12 at 13:54
  • 1
    Yeah..I think you need to parse the tag 1st and then check the attribute with getAttribute – Ben Lefebvre Sep 12 '12 at 13:58
1

The name of the attribute is "Type" but you are looking for an attribute named "soups".

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Hi Michael thanks for replying, I tried that also and it still throws the same exception. I also tried: Node sundayNode = doc.getElementsByTagName("sunday").item(0); Element sunElem = (Element)sundayNode; Attr typeAttribute = sunElem.getAttributeNode("Type"); tst = typeAttribute.getLocalName(): tst = typeAttribute.getNodeValue(); (tst is String type) Thing is im doing everything without any documentation, spending time for nothing as there is no example / tutorial in the web for that.. I would appreciate if anyone knows how to solve it. THANKS – Udi Oshi Sep 13 '12 at 06:48
  • 1
    If you're doing things without documentation then I'm not surprised you are having trouble. The DOM API is pretty horrible and non-intuitive, but it's certainly well documented. Sorry but I can't give you more help, I can't see you error by staring at the code, I don't have enough info to debug it for you, and anyway, I hate DOM. – Michael Kay Sep 13 '12 at 08:02
0

Ok so I went throw the documentation and after searching I found the following:

Parsing attribute in XML with DOM parser

And succeeding parse the xml with attribute :

                String tst;

                Node sundayNode = doc.getElementsByTagName("sunday").item(0);
                Element sundayElem = (Element)sundayNode;
                Node foodNode = sundayElem.getElementsByTagName("food").item(0);


                NamedNodeMap  attrs = foodNode.getAttributes();

                for (int a = 0; a < attrs.getLength(); a++) 
                {
                        Node theAttribute = attrs.item(a);


                        tst = theAttribute.getNodeName() + "=" + theAttribute.getNodeValue();
                 }

Thanks everyone for the help

Community
  • 1
  • 1
Udi Oshi
  • 6,787
  • 7
  • 47
  • 65