我正在尝试解析 xml,但没有成功,xml 是:
<Menu>
<sunday>
<food Type="soups">someVal</food>
<food Type="soups">someVal</food>
</sunday>
</Menu>
我在android中解析使用:
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;
这就是我尝试解析它的方式:
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();
我得到 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)
有没有更好的方法来解析带有属性的 xml?
非常感谢您的帮助