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")));
}