我正在制作一个 android 应用程序女巫从 Web 服务获取信息。我需要一个解决方案来解析这个结果:
<Client xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://lmggroup.net/">
<ID>12805</ID>
<PersonalNumber>0</PersonalNumber>
<EntryDate>2013-01-28T14:39:01</EntryDate>
<FirstName>0</FirstName>
<LastName>0</LastName>
<Address>0</Address>
<Phone>0601231569</Phone>
<Email>aaa@aaa.com</Email>
<OrganizationalUnitID>02901</OrganizationalUnitID>
<Password>aaaaaa</Password>
<IsActive>true</IsActive>
</Client>
我已经尝试使用以下代码解决此问题
public static ArrayList<UserContent> getUserContentList(String response)
{
ArrayList<UserContent> result = new ArrayList<UserContent>();
if (response != null && response.equals("") == false)
{
KXmlParser xmlParser = new KXmlParser();
Document xmlDoc = new Document();
ByteArrayInputStream bin = new ByteArrayInputStream(response.getBytes());
InputStreamReader isr = new InputStreamReader( bin );
try
{
xmlParser.setInput(isr);
xmlDoc.parse(xmlParser);
Element xmlRoot = xmlDoc.getRootElement();
if(xmlRoot != null)
{
Element[] xmlChild = XmlParser.getChildren(xmlRoot);
for ( int index = 0; index < xmlChild.length; ++index )
{
UserContent item = new UserContent();
Element[] contentNodes = XmlParser.getChildren(xmlChild[index]);
for ( int i = 0; i < contentNodes.length; ++i )
{
if (contentNodes[i].getName().equals(StaticStrings.contentUserID))
{
item.id = contentNodes[i].getText(0);
}
else if (contentNodes[i].getName().equals(StaticStrings.contentUserPIB))
{
item.pib = contentNodes[i].getText(0);
}
else if (contentNodes[i].getName().equals(StaticStrings.contentUserPhone))
{
item.phone = contentNodes[i].getText(0);
}
else if (contentNodes[i].getName().equals(StaticStrings.contentUserMail))
{
item.email = contentNodes[i].getText(0);
}
}
result.add(item);
}
}
}
catch (IOException e)
{
Log.e(TAG, e.getMessage());
}
catch (XmlPullParserException e)
{
Log.e(TAG, e.getMessage());
}
try
{
isr.close();
}
catch (IOException e) {}
}
return result;
}
但是当我调用这个方法时,我得到了所有的 xml 标签,但它们的内容是空的。