日期:2014-05-19  浏览次数:20689 次

用Dom4j读取一段Xml字符串的问题(加急,在线等)
Java code

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的,

------解决方案--------------------
你先百度下SAXReader 这个怎么用,先用别人的例子,再改成自己的例子。
------解决方案--------------------
你可以通过XPATH查找,希望对你有帮助
Java code

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

}