Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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_Xmlpullparser - Fatal编程技术网

Android XML属性解析

Android XML属性解析,android,xml,xmlpullparser,Android,Xml,Xmlpullparser,我正在构建一个必须解析XML属性的应用程序。我的XML如下所示: <ad type="interstitial" animation="none"> <interstitial preload="0" autoclose="0" type="url" url="http://account.mobfox.com/activation_vad.php" orientation="portrait"> </interstitial> </ad&

我正在构建一个必须解析
XML
属性的应用程序。我的
XML
如下所示:

<ad type="interstitial" animation="none">
<interstitial preload="0" autoclose="0" type="url" url="http://account.mobfox.com/activation_vad.php" orientation="portrait">       
</interstitial>
</ad>
但是,我无法从属性打印
URL

我做错了什么?任何帮助都将不胜感激。

最好使用xpath概念。它很容易实现。这是。。。您还可以在线运行查询…

试试这个

Log.d("URL --> ", ""+parser1.getAttributeValue(null, "url"));
因为您可能正在
Log.d
中传递参数

编辑

URL url = new URL("Your URL");
                DocumentBuilderFactory dbf = DocumentBuilderFactory
                        .newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(new InputSource(url.openStream()));

                doc.getDocumentElement().normalize();

                NodeList nodeList = doc.getElementsByTagName("ad");

                for (int i = 0; i < nodeList.getLength(); i++) {

                    Node node = nodeList.item(i);

                    Element fstElmnt = (Element) node;
                    NodeList nameList = fstElmnt.getElementsByTagName("interstitial");
                    Element nameElement = (Element) nameList.item(0);
                    nameList = nameElement.getChildNodes();                       

                    System.out.println("Value : "
                            + (nameElement.getAttribute("url")));
                }
URL=newurl(“您的URL”);
DocumentBuilderFactory dbf=DocumentBuilderFactory
.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(新的输入源(url.openStream());
doc.getDocumentElement().normalize();
NodeList NodeList=doc.getElementsByTagName(“ad”);
for(int i=0;i
异常或正在输出的内容可能会有所帮助。得到的
异常如下:
11-29 16:08:54.124:W/System.err(30206):java.lang.NullPointerException:println需要一条消息11-29 16:08:54.134:W/System.err(30206):在android.util.Log.println_native(native方法)11-29 16:08:54.134:W/System.err(30206):在android.util.Log.d(Log.java:139)上,
尝试获取
null
输出。@Anupam然后您没有获取URL。url为空,因此出现错误。试试我的编辑
URL url = new URL("Your URL");
                DocumentBuilderFactory dbf = DocumentBuilderFactory
                        .newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(new InputSource(url.openStream()));

                doc.getDocumentElement().normalize();

                NodeList nodeList = doc.getElementsByTagName("ad");

                for (int i = 0; i < nodeList.getLength(); i++) {

                    Node node = nodeList.item(i);

                    Element fstElmnt = (Element) node;
                    NodeList nameList = fstElmnt.getElementsByTagName("interstitial");
                    Element nameElement = (Element) nameList.item(0);
                    nameList = nameElement.getChildNodes();                       

                    System.out.println("Value : "
                            + (nameElement.getAttribute("url")));
                }