日期:2014-05-19 浏览次数:20689 次
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 "识别码不正确"; }
String xml = "<?xml version='1.0' encoding='UTF-8'?><Body><Table Name='SetUpStatusProperty' Description='设置上报信息属性'><Item CallbackUrl='http://...' VerifyID='...' IntervalFig='xxxx' /></Table></Body>";//需要读取的字符串 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()); }
------解决方案--------------------
List<Element> list= doc.selectNodes("ck/gongJuTiao/niu");
for (int i = 0; i < list.size(); i++)
{
Element ys=list.get(i);
System.out.println( ys.getText());
}
这只是个示例,节点自己取
------解决方案--------------------
Document doc = DocumentHelper.parseText(xml);
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"));
}
}
}