String xml = "<?xml version='1.0' encoding='UTF-8'?><Body><Table Name='SetUpStatusProperty' Description='设置上报信息属性'><Item CallbackUrl='http://...' VerifyID='...' IntervalFig='xxxx' /></Table></Body>";//需要读取的字符串
public String SendToMethod(String VerifyID, String xml) {
//这个方法是我们验证的一个方法
if (VerifyIDAuthentication.AuthenVerifyId(VerifyID)) {
SAXReader xmlReader = new SAXReader();
try {
Document doc = DocumentHelper.parseText(xml);
} catch (DocumentException e) {
e.printStackTrace();
return "读取Xml文件错误"; } catch (Exception ex) {
ex.printStackTrace();
return "读取Xml文件错误";
} }
return "识别码不正确"; }
各位高手,我要取到里面所有的值,怎么取,?我后面就不会了,一写就报错.我是一个刚学java的,
Document doc = null;
try
{
doc = DocumentHelper.parseText(xml);
}catch(Exception ex)
{
throw new RuntimeException("转换失败!",ex);
}
//假如我要读取<Item>元素
List listItems = doc.selectNodes("/Body/Table/Item" );
Iterator it = listItems.iterator();
while(it.hasNext())
{
Element itemElement = (Element)it.next();
System.out.println("item_CallbackUrl="+itemElement.attribute("IntervalFig").getValue());
}
//假如我要读取<Item>中的IntervalFig属性值
List list = doc.selectNodes("/Body/Table/Item/@IntervalFig" );
it = list.iterator();
while(it.hasNext())
{
Attribute attribute = (Attribute)it.next();
System.out.println("@IntervalFig="+attribute.getValue());
}
{
Element ys=list.get(i);
System.out.println( ys.getText());
}
这只是个示例,节点自己取
List bodys = doc.selectNodes("Body");
for(Iterator itB = bodys.iterator();itB.hasNext();){
Element body = (Element) itB.next();
List tables = body.selectNodes("Table");
for(Iterator itT = tables.iterator();itT.hasNext();){
Element table = (Element) itT.next();
System.out.println(table.attributeValue("Name"));
System.out.println(table.attributeValue("Description"));
List items = table.selectNodes("Item");
for(Iterator its = items.iterator();its.hasNext();){
Element item = (Element) its.next();
System.out.println(item.attributeValue("CallbackUrl"));
System.out.println(item.attributeValue("VerifyID"));
System.out.println(item.attributeValue("IntervalFig"));
}
}
}关于标签我取的全是list(不清楚你的实际xml格式),你要确定全是单一标签可以具体改一下