我有以下xml结构:
<?xml version="1.0" encoding="utf-8"?>
<Packages SyncId="634651745071815748" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Package Id="1848f7cc-0ced-fbb7-140e-b485d28c207f" Title="Active Directory" AvailableContentType="ReportsOnly" Badge="0">
<Icon Id="960EE89A85EAD6244DAEC389A7103F3A5FBB4EC3"/>
</Package>
<Package Id="b923bf10-3a79-a7dc-64dc-9cd875233a3a" Title="Exchange 2007" AvailableContentType="ReportsOnly" Badge="0">
<Icon Id="4999D5496086D802FC261F953DDB50B167431E45"/>
</Package>
<Package Id="367f0d4b-4656-c628-b75f-26535dfa505c" Title="Exchange 2010" AvailableContentType="ReportsOnly" Badge="0">
<Icon Id="320E1D9563CA8FE09FA43D8325F7E9F02BC88737"/></Package>
<Package Id="2c088847-b1ca-a9f7-29a4-a3fbc8cd3445" Title="Local System" AvailableContentType="ReportsOnly" Badge="0">
<Icon Id="FA9BA4CA8B5E626304EDD5323DE252DC0D3F6087"/>
</Package>
<Package Id="9ac614f7-e67b-5f98-3197-49de0469fd3b" Title="Network" AvailableContentType="ReportsOnly" Badge="0">
<Icon Id="B9B35308E738346CA68970BAB2CEC31B4047983F"/>
</Package>
<Package Id="1bac78ad-7834-5549-b775-0ecc7f29b1ae" Title="Routing Table" AvailableContentType="ReportsOnly" Badge="0">
<Icon Id="ABA86D564A461201C0B39785D7A68A3C2CEA4CAD"/>
</Package>
</Packages
我想解析这个xml。我写了这段代码:
public static Packages Parse(String packages)
{
Packages packs = new Packages();
Document doc= new Document();
byte[] xmlByteArray = packages.getBytes();
ByteArrayInputStream xmlStream = new ByteArrayInputStream(xmlByteArray);
xmlStream.reset();
//xmlStream.reset();
InputStreamReader xmlReader = new InputStreamReader(xmlStream);
//packages = StringHelper.replace(packages, "<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
//packages = StringHelper.replace(packages, " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
KXmlParser parser = new KXmlParser();
try {
parser.setInput(xmlReader);
} catch (XmlPullParserException e) {
e.printStackTrace();
}
try {
doc.parse(parser);
xmlReader.reset();
String posDesc = parser.getPositionDescription();
**int token = parser.nextTag();**
parser.require(XmlPullParser.START_TAG, null, "Packages");
if (parser.getAttributeCount()>0)
{
if (parser.getAttributeName(0)=="SyncId") packs.SyncId = parser.getAttributeValue(0);
}
while(parser.nextTag()!=XmlPullParser.END_DOCUMENT)
{
parser.require(XmlPullParser.START_TAG, null, "Package");
Package pack = new Package();
int packAttrCount = parser.getAttributeCount();
if(packAttrCount>0)
{
for(int i=0; i<packAttrCount; i++)
{
if (parser.getAttributeName(i)=="Id") pack.Id=parser.getAttributeValue(i);
else
if (parser.getAttributeName(i)=="Title") pack.Title=parser.getAttributeValue(i);
else
if (parser.getAttributeName(i)=="AvailableContentType") pack.AvailableContentType=parser.getAttributeValue(i);
else
if (parser.getAttributeName(i)=="Badge") pack.Badge=parser.getAttributeValue(i);
}
}
parser.next();
parser.require(XmlPullParser.START_TAG, null, "Icon");
Icon icon = new Icon();
if (parser.getAttributeCount()>0)
{
if (parser.getAttributeName(0)=="Id") icon.Id=parser.getAttributeValue(0);
}
parser.nextTag();
if (parser.getName()=="Data") icon.Data=parser.getText();
pack.Icon = icon;
packs.Packs.addElement(pack);
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
首先调用parser.next()总是返回END_DOCUMENT,并且我在require方法中接收异常。
org.xmlpull.v1.XmlPullParserException:expected:START_TAG {null}包(位置:END_DOCUMENT null @ 1:1268 in java.io.InputStreamReader@bd3ab6b6)
我做错了什么? 提前谢谢!
答案 0 :(得分:0)
从您的代码中删除对doc.parse(parser);
的通话。
使用kXML,您可以使用
解析XMLDocument#parse()
方法你试过这两个。调用doc.parse(parser)
后,您自动前进到XML文档的末尾(并到达XmlPullParser.END_DOCUMENT tag
)。之后,您立即尝试使用第二种方法再次解析XML。
根据您的代码判断,您决定使用第二种方法,因此,您应该删除对doc.parser(parser)
的调用。