kXML (XmlPullParser) 未命中 END_TAG
我正在尝试找出一种方法来重写我的一些 XML 解析代码。我目前正在使用 kXML2,这是我的代码 -
byte[] xmlByteArray;
try {
xmlByteArray = inputByteArray;
ByteArrayInputStream xmlStream = new ByteArrayInputStream(xmlByteArray);
InputStreamReader xmlReader = new InputStreamReader(xmlStream);
KXmlParser parser = new KXmlParser();
parser.setInput(xmlReader);
parser.nextTag();
while(true)
{
int eventType = parser.next();
String tag = parser.getName();
if(eventType == XmlPullParser.START_TAG)
{
System.out.println("****************** STARTING TAG "+tag+"******************");
if(tag == null || tag.equalsIgnoreCase(""))
{
continue;
}
else if(tag.equalsIgnoreCase("Category"))
{
// Gets the name of the category.
String attribValue = parser.getAttributeValue(0);
}
}
if(eventType == XmlPullParser.END_TAG)
{
System.out.println("****************** ENDING TAG "+tag+"******************");
}
else if(eventType == XmlPullParser.END_DOCUMENT)
{
break;
}
}
catch(Exception ex)
{
}
我的输入 XML 如下 -
<root xmlns:sql="urn:schemas-microsoft-com:xml-sql" xmlns="">
<Category name="xyz">
<elmt1>value1</elmt1>
<elmt2>value2</elmt2>
</Category>
<Category name="abc">
<elmt1>value1</elmt1>
<elmt2>value2</elmt2>
</Category>
<Category name="def">
<elmt1>value1</elmt1>
<elmt2>value2</elmt2>
</Category>
我的问题是,我希望它在遇到结束 xml 标记时命中 XmlPullParser.END_TAG
。它确实命中了 XmlPullParser.START_TAG
,但它似乎只是跳过/忽略了所有 END_TAG
。
这是它应该如何工作的吗?或者我错过了什么?
非常感谢任何帮助,
特贾。
I'm trying to figure out a way to rewrite some of my XML parsing code. I'm currently working with kXML2 and here's my code -
byte[] xmlByteArray;
try {
xmlByteArray = inputByteArray;
ByteArrayInputStream xmlStream = new ByteArrayInputStream(xmlByteArray);
InputStreamReader xmlReader = new InputStreamReader(xmlStream);
KXmlParser parser = new KXmlParser();
parser.setInput(xmlReader);
parser.nextTag();
while(true)
{
int eventType = parser.next();
String tag = parser.getName();
if(eventType == XmlPullParser.START_TAG)
{
System.out.println("****************** STARTING TAG "+tag+"******************");
if(tag == null || tag.equalsIgnoreCase(""))
{
continue;
}
else if(tag.equalsIgnoreCase("Category"))
{
// Gets the name of the category.
String attribValue = parser.getAttributeValue(0);
}
}
if(eventType == XmlPullParser.END_TAG)
{
System.out.println("****************** ENDING TAG "+tag+"******************");
}
else if(eventType == XmlPullParser.END_DOCUMENT)
{
break;
}
}
catch(Exception ex)
{
}
My input XML is as follows -
<root xmlns:sql="urn:schemas-microsoft-com:xml-sql" xmlns="">
<Category name="xyz">
<elmt1>value1</elmt1>
<elmt2>value2</elmt2>
</Category>
<Category name="abc">
<elmt1>value1</elmt1>
<elmt2>value2</elmt2>
</Category>
<Category name="def">
<elmt1>value1</elmt1>
<elmt2>value2</elmt2>
</Category>
My problem briefly is, I'm expecting it to hit XmlPullParser.END_TAG
when it encounters a closing xml tag. It does hit the XmlPullParser.START_TAG
but it just seems to skip / ignore all the END_TAG
s.
Is this how is it's supposed to work? Or am I missing something?
Any help is much appreciated,
Teja.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,不确定这是否是您的确切问题,但您错过了
您是否尝试过在开始标记和内容之后打印出什么 eventType 类型?
Well not sure if this is your exact problem but you are missing a
Have you tried printing out what eventType type is after start tag and after content?
哎呀,我很抱歉,一如既往,事实证明编写一个包罗万象的块是一个坏主意。我没有注意到我没有在此处发布的一段代码存在异常,该代码应该进入 XmlPullParser。END_TAG。删除它并像魅力一样工作:)
Oops, I'm sorry, as always, it turned out to be a bad idea to write a catch-all block. I didn't notice that there were exceptions in a piece of code that I didn't post here, which is supposed to go into XmlPullParser.END_TAG. Removed it and works like charm :)