无线&移动互联网技术研发

换位思考·····
posts - 19, comments - 53, trackbacks - 0, articles - 283
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

一下给的例子是本人公司常用的格式,现都给出实例型的解析。
在解析xml时,感谢网上很多朋友给我的帮助,
http://topic.csdn.net/u/20081202/23/67e0cc8b-2922-4f56-94e4-05811090c5a6.html(我参照的帖子)
http://topic.csdn.net/u/20090512/14/1627b473-22c1-4c2c-aa2a-ae492338390b.html(我发布的帖子)

package com.Gavin.io;

import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/** */
/**
 * @descripte parse XML,and these xml are stream
 * 
@author Gavin.lee
 * @date 09--5-6 pm
 * 
@version 1.0
 *
 
*/


public class XMLParse {

    
private static String xml = "<?xml version='1.0' encoding='GBK'?>"
            
+ "<alipay>" + "<is_success>T</is_success>" + "<request>"
            
+ "<param name='service'>single_trade_query</param>"
            
+ "<param name='_input_charset'>utf-8</param>"
            
+ "<param name='partner'>2088001513232645</param>"
            
+ "<param name='out_trade_no'>20090422577264</param>"
            
+ "</request>" + "<response><trade/></response>"
            
+ "<sign>39d55e300e7ffc159535b1f5a11d29fc</sign>"
            
+ "<sign_type>MD5</sign_type>" + "</alipay>";

    
private static String xml2 = "<?xml version='1.0' encoding='gb2312' ?>"
            
+ "<xml>"
            
+ "<version>v1.0</version>"
            
+ "<state>100</state>"
            
+ "<message>获取成功</message>"
            
+ "<list>"
            
+ "<row ui_password_old='' ui_carnumber='' ui_bankname='如广东省中国建设银行深圳市航空大厦支行营业部' "
            
+ "ui_carofzone='0' ui_carofbank='0' ui_retime='2008-09-02 11:15:20' ui_lastip='192.168.0.39' "
            
+ "ui_lasttime='2008-12-16 10:51:01' ui_truename='' ui_country='' ui_province='' ui_city='合肥' "
            
+ "ui_address='' ui_phone='' ui_mobile='' ui_work='' ui_edu='' ui_idcode='' ui_zipcode='' "
            
+ "ui_birthday='' ui_oicq='' ui_sex='1' ui_email='' ui_locktime='' ui_locknote='' "
            
+ "ui_regchanel='500wan' ui_expname=''/>"
            
+ "<row ui_password_old='' ui_carnumber='' ui_bankname='如广东省中国建设银行深圳市航空大厦支行营业部' "
            
+ "ui_carofzone='0' ui_carofbank='0' ui_retime='2008-09-02 11:15:20' ui_lastip='192.168.0.39' "
            
+ "ui_lasttime='2008-12-16 10:51:01' ui_truename='' ui_country='' ui_province='' ui_city='北京' "
            
+ "ui_address='' ui_phone='' ui_mobile='' ui_work='' ui_edu='' ui_idcode='' ui_zipcode='' "
            
+ "ui_birthday='' ui_oicq='' ui_sex='1' ui_email='' ui_locktime='' ui_locknote='' "
            
+ "ui_regchanel='500wan' ui_expname=''/>" + "</list>" + "</xml>";

    
private static String xml3 = "<?xml version='1.0' encoding='GBK'?>"
            
+ "<xml>" + "<message>获取成功</message>" + "<person>" +

            
"<teacher>" + "<name>Gavin</name>" + "<age>23</age>"
            
+ "<sex>男</sex>" + "</teacher>" +

            
"<teacher>" + "<name>kathy</name>" + "<age>21</age>"
            
+ "<sex>女</sex>" + "</teacher>" +

            
"</person>" + "</xml>";

    
/**
     * 只能解析标准的单个子标签xml文件,<root>root value</root>。
     
*/

    
public void xmlParse(String xml) {

        BufferedReader bufferXml 
= new BufferedReader(new CharArrayReader(xml
                .toCharArray()));
        
try {
            SAXReader reader 
= new SAXReader();
            Document doc 
= reader.read(bufferXml);
            Element root 
= doc.getRootElement();
            String isSuccess 
= root.elementText("is_success");
            
//   String param = root.elementText("param");      //错误,无法解析
            String signType = root.elementText("sign_type");

            System.out.println(isSuccess);
            
//   System.out.println(param);
            System.out.println(signType);

            Element elements 
= root.element("request");
            Element e;
            
for (Iterator i = elements.elementIterator("param"); i.hasNext();) {
                e 
= (Element) i.next();
                
if ("service".equals(e.attributeValue("name"))) {
                    System.out.println(e.getText());
                }

            }

        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }


    
public void xmlParse2(String xml) {
        BufferedReader bufferXml 
= new BufferedReader(new CharArrayReader(xml
                .toCharArray()));
        
try {
            SAXReader reader 
= new SAXReader();
            Document doc 
= reader.read(bufferXml);
            Element root 
= doc.getRootElement();
            String msg 
= root.elementText("message");

            System.out.println(msg);

            Element elements 
= root.element("list");
            Element e;
            
for (Iterator i = elements.elementIterator("row"); i.hasNext();) {
                e 
= (Element) i.next();
                System.out.println(
"city:" + e.attributeValue("ui_city"));
            }

        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }


    
public void xmlParse3(String xml) {
        BufferedReader bufferXml 
= new BufferedReader(new CharArrayReader(xml
                .toCharArray()));
        
try {
            
//   File f = new File("this.xml"); 
            SAXReader reader = new SAXReader();
            Document doc 
= reader.read(bufferXml);
            Element root 
= doc.getRootElement();
            Element foo;
            Element elements 
= root.element("person");
            
for (Iterator i = elements.elementIterator("teacher"); i.hasNext();) {
                foo 
= (Element) i.next();
                System.out.println(
"name:" + foo.elementText("name"));
                System.out.println(
"age:" + foo.elementText("age"));
            }

        }
 catch (Exception e) {
            e.printStackTrace();
        }


    }


    
public static void main(String[] args) {
        XMLParse parse 
= new XMLParse();
        
//  parse.xmlParse(xml);  
        
//  parse.xmlParse2(xml2);
        
//  parse.xmlParse3(xml3);
    }

}


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


网站导航: