Java 如何从dom解析器解析空标记以使用php webservice
在我的xml中,可以有像Java 如何从dom解析器解析空标记以使用php webservice,java,android,xml,xml-parsing,Java,Android,Xml,Xml Parsing,在我的xml中,可以有像这样的空标记 问题是,当我解析xml时,当我到达xml中的这一行时,会出现空指针异常 我应该如何在解析文件中检查这样的标记 ArrayList<String>textcomment = new ArrayList<String>(); for(int i = 0;i<nl.getLength();i++) { Element e = (Element)nl.item(i); // crash on t
这样的空标记
问题是,当我解析xml时,当我到达xml中的这一行时,会出现空指针异常
我应该如何在解析文件中检查这样的标记
ArrayList<String>textcomment = new ArrayList<String>();
for(int i = 0;i<nl.getLength();i++)
{
Element e = (Element)nl.item(i);
// crash on this string find first time a value and second time find </no_of_comments>
String noOfcomment = e.getElementsByTagName("no_of_comments").item(0).getFirstChild ().getNodeValue();
aryNoOfcomment.add(i, noOfcomment);
}
ArrayListtextcomment=new ArrayList();
对于(inti=0;i的解析,您可以使用此
FileInputStream fi = new FileInputStream(dir);
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
doc = builder.parse(fi);
NodeList nlorgid = doc.getElementsByTagName("Organization");
String orgidfromxml = ((Element)nlorgid.item(0)).getAttribute("id");
System.out.println(" organization id from xml is "+orgidfromxml);
有问题的线路是什么
是这条线吗
String noOfcomment = e.getElementsByTagName ("no_of_comments"). item (0). getFirstChild (). getNodeValue ();
所有内容都在同一行中,因此不检查其中一个是否为null
您可以分离并检查结果是否为空,如下所示:
List<String> yourList = new ArrayList<String>();
NodeList nl = e.getElementsByTagName ("no_of_comments");
if(nl != null){
for(int i = 0;i<nl.getLength();i++)
{
Node n = nl.item(i).getFirstChild();
if(n!=null){
String value = n.getNodeValue();
yourList.add(value);
}
}
}
List yourList=new ArrayList();
NodeList nl=e.getElementsByTagName(“无注释”);
如果(nl!=null){
对于(int i=0;i如何在ArrayListk中添加值字符串…但是sir breakpoint在这个位置arraylist上不起作用,我可以将这个arraylist添加到数组中吗..请帮助我,先生…看看现在是否有效。但是我的朋友,我不喜欢使用元素来读取XML…你可以在Internet上找到更好的解决方案。你可以找一个lib XStream,不是吗阅读XML很痛苦,但可能比XMLStreamWriter慢一点,然后用谷歌搜索:XMLStreamWriterok…您为空标记值提供了更好的解决方案…我很高兴您的回复…感谢您的成功回复。。。