kxml2 解析简单 XML
我正在尝试在 j2me 应用程序中解析一个简单的 XML 文件。 但解析失败:
XML 文件
<companies>
<company CompanyId="6">
<CompanyName>Test Company 1</CompanyName>
<SapNumber>0</SapNumber>
<RootCompanyId>1</RootCompanyId>
<ParentCompanyId /> </company>
</companies>
解析器片段
KXmlParser parser = new KXmlParser();
parser.setInput(new InputStreamReader(new ByteArrayInputStream(input.getBytes())));
parser.nextTag();
parser.require(XmlPullParser.START_TAG, null, "companies");
while(parser.nextTag() == XmlPullParser.START_TAG)
{
Company temp_company = new Company();
parser.require(XmlPullParser.START_TAG, null, "company");
String CompanyID = parser.getAttributeValue(0);
temp_company.putValue("CompanyId", CompanyID);
while(parser.nextTag() == XmlPullParser.START_TAG)
{
if(parser.getName() == "CompanyName")
{
temp_company.putValue("CompanyName", parser.nextText());
}
}
parser.require(XmlPullParser.END_TAG, null, "company");
listCompany.put(CompanyID, temp_company);
}
parser.require(XmlPullParser.END_TAG, null, "elements");
I'm trying to parse a simple XML file in my j2me application. But the parsing fails:
XML File
<companies>
<company CompanyId="6">
<CompanyName>Test Company 1</CompanyName>
<SapNumber>0</SapNumber>
<RootCompanyId>1</RootCompanyId>
<ParentCompanyId /> </company>
</companies>
Parser Snippet
KXmlParser parser = new KXmlParser();
parser.setInput(new InputStreamReader(new ByteArrayInputStream(input.getBytes())));
parser.nextTag();
parser.require(XmlPullParser.START_TAG, null, "companies");
while(parser.nextTag() == XmlPullParser.START_TAG)
{
Company temp_company = new Company();
parser.require(XmlPullParser.START_TAG, null, "company");
String CompanyID = parser.getAttributeValue(0);
temp_company.putValue("CompanyId", CompanyID);
while(parser.nextTag() == XmlPullParser.START_TAG)
{
if(parser.getName() == "CompanyName")
{
temp_company.putValue("CompanyName", parser.nextText());
}
}
parser.require(XmlPullParser.END_TAG, null, "company");
listCompany.put(CompanyID, temp_company);
}
parser.require(XmlPullParser.END_TAG, null, "elements");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我可以看到这里出了什么问题。 匹配
标记并获取 CompanyId 属性的值后,您将进入 while 循环。 但观察此时会发生什么:
开始标记,因此 if 条件将是true,您将获得标签内的文本。
内部)标记)或结束标记(即)。 无论哪种方式,您的 while 条件都会失败,因为您不在开始标记处。
的结束标记,但是您的状态仍然没有改变,这将无法满足。我最好的猜测是内部指针指向
内的文本节点,这就是为什么您得到" 意外类型 (position: Text :测试 Company1...”消息。
I guess I can see what is going wrong here. After you have matched the
<company>
tag and obtained the value of the CompanyId attribute, you enter a while loop. But observe what will happen at this point:<CompanyName>
start tag, thus the if condition will be true and you will obtain the text inside the tag.<CompanyName>
tag) or at the end tag (ie.</CompanyName>
). Either way, you while condition will fail because you are not at a start tag.<company>
, however, your state has still not changed and this will not be satisfied.My best guess is that the internal pointer is pointing at the text node inside
<CompanyName>
and that is why you get the"unexpected type (position: Text: Test Company1..."
message.