Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中带属性的xml解析_Android_Xml - Fatal编程技术网

android中带属性的xml解析

android中带属性的xml解析,android,xml,Android,Xml,我试图解析xml,但没有成功 xml是: <Menu> <sunday> <food Type="soups">someVal</food> <food Type="soups">someVal</food> </sunday> </Menu> 以下是我尝试解析它的方式: doc.getDocumentElement().normalize();

我试图解析xml,但没有成功 xml是:

<Menu> 
<sunday> 
<food Type="soups">someVal</food>  
<food Type="soups">someVal</food>   
</sunday>  
</Menu>
以下是我尝试解析它的方式:

                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


非常感谢您的帮助

尽管我的xml中没有属性,但我还是这样做的。你只需要做像这样的事情

Element element2 = element.getAttribute("type");
或者在使用.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();
NodeList NodeList=doc.getElementsByTagName(“ConfigIn”);
for(int i=0;i

希望这能在某种程度上帮助您…

这是我的做法,尽管我的xml中没有属性。您只需执行以下操作

Element element2 = element.getAttribute("type");
或者在使用.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();
NodeList NodeList=doc.getElementsByTagName(“ConfigIn”);
for(int i=0;i

希望这能在某些方面对您有所帮助…

属性的名称是“Type”,但您正在查找名为“soups”的属性。

属性的名称是“Type”,但您正在查找名为“soups”的属性。

好的,所以我抛出了文档,搜索后我发现了以下内容:

然后使用属性解析xml:

                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();
                 }
stringtst;
节点sundayNode=doc.getElementsByTagName(“sunday”).item(0);
元素sundayElem=(元素)sundayNode;
节点foodNode=sundayElem.getElementsByTagName(“食品”).item(0);
NamedNodeMap attrs=foodNode.getAttributes();
对于(int a=0;a

谢谢大家的帮助

好的,我去扔了文档,搜索后我发现了以下内容:

然后使用属性解析xml:

                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();
                 }
stringtst;
节点sundayNode=doc.getElementsByTagName(“sunday”).item(0);
元素sundayElem=(元素)sundayNode;
节点foodNode=sundayElem.getElementsByTagName(“食品”).item(0);
NamedNodeMap attrs=foodNode.getAttributes();
对于(int a=0;a

谢谢大家的帮助

编辑。谢谢,我不知道这是否会有帮助,因为我认为这是一个错误的算法。编辑。谢谢,我不知道这是否会有帮助,因为我认为这是一个错误的算法。我这样做是为了解析没有属性的元素。问题是我需要解析一些带有属性的元素,可能需要使用类Attr,但有一个我们没有这样的例子…是的..我想你需要先解析标签,然后用getAttribute检查属性我这样做是为了解析没有属性的元素。问题是我需要解析一些带有属性的元素,可能需要使用类Attr,但没有这样的例子…是的..我想你需要先解析标签,然后检查带有getAttributeHi Michael的属性感谢您的回复,我也尝试了,但它仍然抛出相同的异常。我也尝试了:Node sundayNode=doc.getElementsByTagName(“sunday”)。item(0);Element sunElem=(Element)sundayNode;Attr typeAttribute=sunElem.getAttributeNode(“Type”);tst=typeAttribute.getLocalName():tst=typeAttribute.getNodeValue();(tst是字符串类型)问题是我做任何事情都没有任何文档,因为网络上没有这方面的示例/教程。如果有人知道如何解决这个问题,我将不胜感激。谢谢如果你在没有文档的情况下做事情,那么你遇到麻烦我一点也不奇怪。DOM API非常可怕,不直观,但它是c当然,文档编制得很好。抱歉,我不能给你更多帮助,我看不到你盯着代码出现错误,我没有足够的信息为你调试它,而且无论如何,我讨厌DOM。嗨,Michael,谢谢你的回复,我也试过了,但它仍然抛出相同的异常。我也试过:Node sundayNode=doc.getElementsByTagName(“sunday”)。项(0);Element sunElem=(Element)sundayNode;Attr typeAttribute=sunElem.getAttributeNode(“类型”);tst=typeAttribute.getLocalName():tst=typeAttribute.getNodeValue();(tst为字符串类型)问题是我做任何事情都没有任何文档,因为网络上没有这方面的示例/教程。如果有人知道如何解决这个问题,我将不胜感激。谢谢如果你在没有文档的情况下做事情,那么你遇到麻烦我一点也不奇怪。DOM API非常可怕,不直观,但它是c很抱歉,我不能给你更多的帮助,我看不出你的错误