JAVA解析XML格式字符串

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

public class TestXML {
    public List xmlElements(String xmlDoc) {
        //创建一个新的字符串
        StringReader read = new StringReader(xmlDoc);
        //创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
        InputSource source = new InputSource(read);
        //创建一个新的SAXBuilder
        SAXBuilder sb = new SAXBuilder();
        try {
            //通过输入源构造一个Document
            Document doc = sb.build(source);
            //取的根元素
            Element root = doc.getRootElement();
            System.out.println(root.getName());//输出根元素的名称(测试)
            //得到根元素所有子元素的集合
            List jiedian = root.getChildren();
            //获得XML中的命名空间(XML中未定义可不写)
            Namespace ns = root.getNamespace();
            Element et = null;
            for(int i=0;i<jiedian.size();i++){
                et = (Element) jiedian.get(i);//循环依次得到子元素
                /**//*
                 * 无命名空间定义时
                 * et.getChild("users_id").getText();
                 * et.getChild("users_address",ns).getText()
                 */
                /*System.out.println(et.getChild("users_id",ns).getText());
                System.out.println(et.getChild("users_address",ns).getText());*/
                System.out.println(et.getChild("p_id",ns).getText());
                System.out.println(et.getChild("ctnno",ns).getText());
            }
            /**//*
             * 如要取<row>下的子元素的名称
             */
            et = (Element) jiedian.get(0);
            List zjiedian = et.getChildren();
            for(int j=0;j<zjiedian.size();j++){
                Element xet = (Element) zjiedian.get(j);
                System.out.println(xet.getName());
            }
        } catch (JDOMException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args){
     TestXML doc = new TestXML();
        String xml = "<?xml version=\"1.0\" encoding=\"gb2312\"?>"+
        "<Result xmlns=\"http://www.fiorano.com/fesb/activity/DBQueryOnInput2/Out\">"+
           "<row resultcount=\"1\">"+
              "<users_id>1001     </users_id>"+
              "<users_name>wangwei   </users_name>"+
              "<users_group>80        </users_group>"+
              "<users_address>1001号   </users_address>"+
           "</row>"+
           "<row resultcount=\"1\">"+
              "<users_id>1002     </users_id>"+
              "<users_name>wangwei   </users_name>"+
              "<users_group>80        </users_group>"+
              "<users_address>1002号   </users_address>"+
           "</row>"+
        "</Result>";
        String xml1 = "<?xml version=\"1.0\" encoding=\"UTF-16LE\" standalone=\"no\"?>" +
          "<d_fsgl_fee_count_for_xml>" +
          "  <d_fsgl_fee_count_for_xml_row>" +
          "  <p_id>JD1302130002</p_id>" +
          "  <ctnno>CXDU1499549</ctnno>" +
          "  <fee_type>单</fee_type>" +
          "  <start_time>2013-02-09 00:00:00</start_time>" +
          "  <end_time>2013-02-13 00:00:00</end_time>" +
          "  <fee>4</fee>" +
          "  <fee_count>16</fee_count>" +
          "  <cpid></cpid>" +
          "  <fee_name>堆存费</fee_name>" +
          "  <fee_rate_id></fee_rate_id>" +
          "  <jffs>1</jffs>" +
          "  <if_hand>0</if_hand>" +
          "  <sfid>FDZT1302180104</sfid>" +
          "  <wt_company>QT</wt_company>" +
          "  <opid>928</opid>" +
          "  <cy>D</cy>" +
          "  <if_bf></if_bf>" +
          "  <days_count>4</days_count>" +
          "  <if_collect>1</if_collect>" +
          "  <if_dd></if_dd>" +
          "  <dd_fee_name></dd_fee_name>" +
          "  <spec_sign>五洲代垫</spec_sign>" +
          " </d_fsgl_fee_count_for_xml_row>" +
          " <d_fsgl_fee_count_for_xml_row>" +
          "  <p_id>JD1302130002</p_id>" +
          "  <ctnno>CXDU1499549</ctnno>" +
          "  <fee_type>周</fee_type>" +
          "  <start_time>2013-02-13 00:00:00</start_time>" +
          "  <end_time>2013-02-20 00:00:00</end_time>" +
          "  <fee>4</fee>" +
          "  <fee_count>32</fee_count>" +
          "  <cpid></cpid>" +
          "  <fee_name>堆存费</fee_name>" +
          "  <fee_rate_id>67</fee_rate_id>" +
          "  <jffs>1</jffs>" +
          "  <if_hand>0</if_hand>" +
          "  <sfid>FDZT1302180104</sfid>" +
          "  <wt_company>QT</wt_company>" +
          "  <opid>928</opid>" +
          "  <cy>D</cy>" +
          "  <if_bf></if_bf>" +
          "  <days_count>8</days_count>" +
          "  <if_collect>1</if_collect>" +
          "  <if_dd></if_dd>" +
          "  <dd_fee_name></dd_fee_name>" +
          "  <spec_sign></spec_sign>" +
          " </d_fsgl_fee_count_for_xml_row>"  +
          "</d_fsgl_fee_count_for_xml>";
        doc.xmlElements(xml1);
    }
}

 

posted on 2013-04-08 17:20 youngturk 阅读(278) 评论(0)  编辑  收藏 所属分类: 几种文档解析web.xml解析


只有注册用户登录后才能发表评论。


网站导航:
 
<2013年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

公告

this year :
1 jQuery
2 freemarker
3 框架结构
4 口语英语

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

相册

EJB学习

Flex学习

learn English

oracle

spring MVC web service

SQL

Struts

生活保健

解析文件

搜索

最新评论

阅读排行榜

评论排行榜