复杂的xml转化为java实体

时间:2021-06-09 21:01:28

 一、样例一:

    以根据订单号向支付宝查询支付是否成功为例(成功信息)

    失败信息:

    <?xml version="1.0" encoding="utf-8"?>

      <alipay>
        <is_success>F</is_success>
        <error>TRADE_NOT_EXIST</error>
      </alipay>

    成功信息:

    <?xml version="1.0" encoding="utf-8"?>

      <alipay>
        <is_success>T</is_success>
          <request>
            <param name="_input_charset">utf-8</param>
            <param name="service">single_trade_query</param>
            <param name="partner">2088121424678319</param>
            <param name="out_trade_no">H161021114440198N</param>
          </request>
          <response>
            <trade>
              <body>订单:T16102111443568</body>
              <buyer_email>1176760525@qq.com</buyer_email>
              <buyer_id>2088402231747066</buyer_id>
              <discount>0.00</discount>
              <flag_trade_locked>0</flag_trade_locked>
              <gmt_create>2016-10-21 11:45:06</gmt_create>
              <gmt_last_modified_time>2016-10-21 11:45:16</gmt_last_modified_time>
              <gmt_payment>2016-10-21 11:45:17</gmt_payment>
              <is_total_fee_adjust>F</is_total_fee_adjust>
              <operator_role>B</operator_role>
              <out_trade_no>H161021114440198N</out_trade_no>
              <payment_type>1</payment_type>
              <price>0.01</price>
              <quantity>1</quantity>
              <seller_email>pay@gstarcad.com</seller_email>
              <seller_id>2088121424678319</seller_id>
              <subject>的生活费...</subject>
              <time_out>2017-01-20 11:45:17</time_out>
              <time_out_type>finishFPAction</time_out_type>
              <to_buyer_fee>0.00</to_buyer_fee>
              <to_seller_fee>0.01</to_seller_fee>
              <total_fee>0.01</total_fee>
              <trade_no>2016102121001004060245497067</trade_no>
              <trade_status>TRADE_SUCCESS</trade_status>
              <use_coupon>F</use_coupon>
            </trade>
           </response>
           <sign>25217c932f49868371a301e3151201a2</sign>
           <sign_type>MD5</sign_type>
        </alipay>

    首先解析is_success字段的值,看看结果是T(成功)还是F(失败),具体就不详细作解释,这里只分析为T时的数据,一定要判断,否则若为F再解析因XML格式不一会报错

    Map<Object, Object> map = new HashMap<Object, Object>();

    map = XMLUtil.doXMLParse(result);
    String is_success = map.get("is_success").toString();

  1、工具类XStreamHandle中定义两个方法:   

  /**

     * 实体转xml字符串

   * @param obj

      * @return

    */

  public static String toXml(Object obj) {

   XStream xstream = new XStream(new DomDriver("utf8"));

  xstream.processAnnotations(obj.getClass()); // 识别obj类中的注解

  return xstream.toXML(obj);

   }

   /**

   * 字符串转实体

   * @param xmlStr

   * @param cls

  * @return

   */

   public static <T> T toBean(String xmlStr, Class<T> cls) {

     XStream xstream = new XStream(new DomDriver());

    xstream.processAnnotations(cls);

     @SuppressWarnings("unchecked")

  T t = (T) xstream.fromXML(xmlStr);

   return t;

   }

  2、根据节点的层级关系定义实体(此为重点)

    由*节点alipay建的实体Alipay 类(Alipay -response-trade) request暂时不需要解析可用String接收

复杂的xml转化为java实体

省略get,set方法

  Alipay 的下级节点response建造类Response

  复杂的xml转化为java实体

省略get,set方法

    Response的下级节点trade建造类    

复杂的xml转化为java实体

xml中所有的标签都要解析,否则会报错

3、调用

  String xml="...";  //xml字符串

  Alipay alipay = XStreamHandle.toBean(xml, Alipay.class);

  Trade trade = alipay.getResponse().getTrade();

二、样例二

  books中有好多book数,有价格,书名,作者

  xml样式:  

      <?xml version="1.0" encoding="utf-8"?>

      <books>
        <book price="108">
          <name>Java编程思想</name>
           <author>Bruce Eckel</author>
        </book>
        <book price="52">
          <name>Effective Java</name>
          <author>Joshua Bloch</author>
        </book>
        <book price="118">
          <name>Java 7入门经典</name>
          <author>Ivor Horton</author>
        </book>
      </books>

  1、工具类还用样例一中的工具类,这里不做重复了

  2、根据节点的层级关系定义实体

    根据*元素books创建实体

    复杂的xml转化为java实体

  根据子级创建book类

复杂的xml转化为java实体

 3、调用

    String xmlString = "......";   //xml内容

    Books books = XStreamHandle.toBean(xmlString, Books.class);
    List<Book> booklist = books.getList();
    System.out.println(booklist.size());

复杂的xml转化为java实体