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