Jibx 处理XML

黄舟
Lepaskan: 2017-02-18 15:18:23
asal
1857 orang telah melayarinya

前面有介绍过json-lib这个框架,在线博文:http://www.php.cn/

以及Jackson这个框架,在线博文:http://www.php.cn/

它们都可以完成Java对象到XML的转换,但是还不是那么的完善。

还有XStream对JSON及XML的支持,它可以对JSON或XML的完美转换。在线博文:

http://www.php.cn/

以及介绍Castor来完成Java对象到xml的相互转换。在线博文:http://www.php.cn/

Jaxb2完成xml的转换,在线博文:http://www.php.cn/

Jibx对Java对象的转换相对要负责些,它不仅需要配置xml还且还要生成相应的jar文件,已经xsd文件。下面我们就来慢慢看看Jibx转换Java到XML是如何完成的。

一、 准备工作

1、 准备资源

a) 官方示例:http://www.php.cn/

http://www.php.cn/

b) Jar下载:http://www.php.cn/

c) 依赖jar包如下:

clip_image002

2、 程序准备代码



package com.hoo.test;
Salin selepas log masuk

   

 <br>
Salin selepas log masuk
Salin selepas log masuk

   

import java.io.IOException;
Salin selepas log masuk

   

import java.io.StringReader;
Salin selepas log masuk

   

import java.io.StringWriter;
Salin selepas log masuk

   

import java.util.ArrayList;
Salin selepas log masuk

   

import java.util.HashMap;
Salin selepas log masuk

   

import java.util.List;
Salin selepas log masuk

   

import org.jibx.runtime.BindingDirectory;
Salin selepas log masuk

   

import org.jibx.runtime.IBindingFactory;
Salin selepas log masuk

   

import org.jibx.runtime.IMarshallingContext;
Salin selepas log masuk

   

import org.jibx.runtime.IUnmarshallingContext;
Salin selepas log masuk

   

import org.jibx.runtime.JiBXException;
Salin selepas log masuk

   

import org.junit.After;
Salin selepas log masuk

   

import org.junit.Before;
Salin selepas log masuk

   

import org.junit.Test;
Salin selepas log masuk

   

import com.hoo.entity.Account;
Salin selepas log masuk

   

import com.hoo.entity.AccountArray;
Salin selepas log masuk

   

import com.hoo.entity.Birthday;
Salin selepas log masuk

   

import com.hoo.entity.ListBean;
Salin selepas log masuk

   

import com.hoo.entity.MapBean;
Salin selepas log masuk

   

 <br>
Salin selepas log masuk
Salin selepas log masuk

   

/**
Salin selepas log masuk

   

 * function: Jibx转换Java到XML
Salin selepas log masuk

   

 * @author hoojo
Salin selepas log masuk

   

 * @createDate 2011-4-25 下午06:47:33
Salin selepas log masuk

   

 * @file JibxTest.java
Salin selepas log masuk

   

 * @package com.hoo.test
Salin selepas log masuk

   

 * @project WebHttpUtils
Salin selepas log masuk

   

 * @blog http://www.php.cn/
Salin selepas log masuk

   

 * @email hoojo_@126.com
Salin selepas log masuk

   

 * @version 1.0
Salin selepas log masuk

   

 */
Salin selepas log masuk

   

public class JibxTest {
Salin selepas log masuk

   

    private IBindingFactory factory = null;
Salin selepas log masuk

   

    <br>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

   

    private StringWriter writer = null;
Salin selepas log masuk

   

    private StringReader reader = null;
Salin selepas log masuk

   

    <br>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

   

    private Account bean = null;
Salin selepas log masuk

   

    <br>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

   

    @Before
Salin selepas log masuk

   

    public void init() {
Salin selepas log masuk

   

        bean = new Account();
Salin selepas log masuk

   

        bean.setAddress("北京");
Salin selepas log masuk

   

        bean.setEmail("email");
Salin selepas log masuk

   

        bean.setId(1);
Salin selepas log masuk

   

        bean.setName("jack");
Salin selepas log masuk

   

        Birthday day = new Birthday();
Salin selepas log masuk

   

        day.setBirthday("2010-11-22");
Salin selepas log masuk

   

        bean.setBirthday(day);
Salin selepas log masuk

   

        <br>
Salin selepas log masuk

   

        try {
Salin selepas log masuk
Salin selepas log masuk

   

            factory = BindingDirectory.getFactory(Account.class);
Salin selepas log masuk

   

        } catch (JiBXException e) {
Salin selepas log masuk

   

            e.printStackTrace();
Salin selepas log masuk
Salin selepas log masuk

   

        }
Salin selepas log masuk
Salin selepas log masuk

   

    }
Salin selepas log masuk

   

    <br>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

   

    @After
Salin selepas log masuk

   

    public void destory() {
Salin selepas log masuk

   

        bean = null;
Salin selepas log masuk

   

        try {
Salin selepas log masuk
Salin selepas log masuk

   

            if (writer != null) {
Salin selepas log masuk

   

                writer.flush();
Salin selepas log masuk

   

                writer.close();
Salin selepas log masuk

   

            }
Salin selepas log masuk

   

            if (reader != null) {
Salin selepas log masuk

   

                reader.close();
Salin selepas log masuk

   

            }
Salin selepas log masuk

   

        } catch (IOException e) {
Salin selepas log masuk

   

            e.printStackTrace();
Salin selepas log masuk
Salin selepas log masuk

   

        }
Salin selepas log masuk
Salin selepas log masuk

   

        System.gc();
Salin selepas log masuk

   

    }
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

   

    <br>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

   

    public void fail(Object o) {
Salin selepas log masuk

   

        System.out.println(o);
Salin selepas log masuk

   

    }
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

   

    <br>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

   

    public void failRed(Object o) {
Salin selepas log masuk

   

        System.err.println(o);
Salin selepas log masuk

   

    }
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

   

}
Salin selepas log masuk


IBindingFactory是一个工厂接口,通过BindingDirectory的getFactory工厂方法可以获得某个对象。然后通过这个工程可以获得转换xml文档的上下文。

二、 转换JavaXML、转换XMLJava

1、 转换JavaEntity对象

a) 首先看看Account、Birthday的代码

package com.hoo.entity;
   
 
   
public class Account {
   
    private int id;
   
    private String name;
   
    private String email;
   
    private String address;
   
    private Birthday birthday;
   
    //getter、setter
   
    
   
    @Override
   
    public String toString() {
   
        return this.id + "#" + this.name + "#" + this.email + "#" + this.address + "#" + this.birthday;
   
    }
   
}
Salin selepas log masuk


Birthday

package com.hoo.entity;
   
 
   
public class Birthday {
   
    private String birthday;
   
    
   
    public Birthday(String birthday) {
   
        super();
   
        this.birthday = birthday;
   
    }
   
    //getter、setter
   
    public Birthday() {}
   
    
   
    @Override
   
    public String toString() {
   
        return this.birthday;
   
    }
   
}
Salin selepas log masuk


b) 程序代码



@Test
Salin selepas log masuk
Salin selepas log masuk

public void bean2XML() {
Salin selepas log masuk

    try {
Salin selepas log masuk
Salin selepas log masuk

        writer = new StringWriter();
Salin selepas log masuk

        // marshal 编组
Salin selepas log masuk
Salin selepas log masuk

        IMarshallingContext mctx = factory.createMarshallingContext();
Salin selepas log masuk
Salin selepas log masuk

        mctx.setIndent(2);
Salin selepas log masuk
Salin selepas log masuk

        mctx.marshalDocument(bean, "UTF-8", null, writer);
Salin selepas log masuk

        fail(writer);
Salin selepas log masuk
Salin selepas log masuk

        <br/>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

        reader = new StringReader(writer.toString());
Salin selepas log masuk
Salin selepas log masuk

        //unmarshal 解组
Salin selepas log masuk
Salin selepas log masuk

        IUnmarshallingContext uctx = factory.createUnmarshallingContext();
Salin selepas log masuk
Salin selepas log masuk

        Account acc = (Account) uctx.unmarshalDocument(reader, null);
Salin selepas log masuk

        fail(acc);
Salin selepas log masuk

    } catch (Exception e) {
Salin selepas log masuk
Salin selepas log masuk

        e.printStackTrace();
Salin selepas log masuk
Salin selepas log masuk

    }
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

}
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk


这样还不够,复杂的东西还在后面。Jibx转换XML文档还要经过一系列复杂的程序。

c) 首先,要写bind.xml和schema。不过还好,官方有提高工具类可以用。

org.jibx.binding.generator.BindGen或org.jibx.binding.BindingGenerator这两个类都可以,用法如下:

首先用dos进入当前工程目录,然后执行命令:E:/Study/WebHttpUtils>java -cp bin;lib/jibx-tools.jar;lib/log4j-1.2.16.jar org.jibx.binding.generator.BindGen -b bind.xml com.hoo.entity.Account

上面的java 是运行某个程序 –cp是依赖的classpath路径的jar、zip等文件,-b 是输出文件名称,是BindGen类的参数。这样会在当前工程目录中生成bind.xml和entity.xsd文件。先看看这2个文件

bind.xml



?xml version="1.0" encoding="UTF-8"?>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

binding value-style="attribute">
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

  mapping class="com.hoo.entity.Account" name="account">
Salin selepas log masuk

    value name="id" field="id"/>
Salin selepas log masuk

    value style="element" name="name" field="name" usage="optional"/>
Salin selepas log masuk

    value style="element" name="email" field="email" usage="optional"/>
Salin selepas log masuk

    value style="element" name="address" field="address" usage="optional"/>
Salin selepas log masuk

    structure field="birthday" usage="optional" name="birthday">
Salin selepas log masuk

      value style="element" name="birthday" field="birthday" usage="optional"/>
Salin selepas log masuk

    /structure>
Salin selepas log masuk

  /mapping>
Salin selepas log masuk

/binding>
Salin selepas log masuk


entity.xsd文件

xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://hoo.com/entity"
   
elementFormDefault="qualified" targetNamespace="http://hoo.com/entity">
   
  xs:element type="tns:account" name="account"/>
   
  xs:complexType name="account">
   
    xs:sequence>
   
      xs:element type="xs:string" name="name" minOccurs="0"/>
   
      xs:element type="xs:string" name="email" minOccurs="0"/>
   
      xs:element type="xs:string" name="address" minOccurs="0"/>
   
      xs:element name="birthday" minOccurs="0">
   
        xs:complexType>
   
          xs:sequence>
   
            xs:element type="xs:string" name="birthday" minOccurs="0"/>
   
          /xs:sequence>
   
        /xs:complexType>
   
      /xs:element>
   
    /xs:sequence>
   
    xs:attribute type="xs:int" use="required" name="id"/>
   
  /xs:complexType>
   
/xs:schema>
Salin selepas log masuk


上面最重要的就是bind.xml文件了,下面编译的时候需要这个文件。Xsd文件可以根据这个文件的内容生成Java的Entity类代码。

执行完命令后,没有错误就可以运行下面一段命令了。运行命令:

E:/Study/WebHttpUtils>java -cp bin;lib/jibx-bind.jar org.jibx.binding.Compile -v bind.xml

-v是绑定文件的名称

运行后,有如下结果:

clip_image004

d) 然后你就可以运行上面的Java的Junit测试程序了,运行后结果如下:

?xml version="1.0" encoding="UTF-8"?>
   
account xmlns="http://hoo.com/entity" id="1">
   
  name>jack/name>
   
  email>email/email>
   
  address>北京/address>
   
  birthday>
   
    birthday>2010-11-22/birthday>
   
  /birthday>
   
/account>
   
1#jack#email#北京#2010-11-22
Salin selepas log masuk


你还可以用命令来查看某个已经生成bind、schema文件的信息,如:

java -cp bin;lib/jibx-run.jar org.jibx.runtime.PrintInfo -c com.hoo.entity.Account

结果如下:

clip_image006

e) 注意,有时候会出现异常信息,如:java.lang.NoSuchFieldException: JiBX_bindingXXXX就要重复下面的命令就可以了。

java -cp bin;lib/jibx-bind.jar org.jibx.binding.Compile -v bind.xml

2、 转换带List集合属性的JavaBean

a) 程序代码



@Test
Salin selepas log masuk
Salin selepas log masuk

public void listBean2XML() {
Salin selepas log masuk

    try {
Salin selepas log masuk
Salin selepas log masuk

        ListBean listBean = new ListBean();
Salin selepas log masuk

        Listlist = new ArrayList();
Salin selepas log masuk

        list.add(bean);
Salin selepas log masuk

        bean = new Account();
Salin selepas log masuk

        bean.setAddress("china");
Salin selepas log masuk

        bean.setEmail("tom@125.com");
Salin selepas log masuk

        bean.setId(2);
Salin selepas log masuk

        bean.setName("tom");
Salin selepas log masuk

        Birthday day = new Birthday("2010-11-22");
Salin selepas log masuk

        bean.setBirthday(day);
Salin selepas log masuk

        <br/>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

        list.add(bean);
Salin selepas log masuk

        listBean.setList(list);
Salin selepas log masuk

        <br/>
Salin selepas log masuk
Salin selepas log masuk

        <br/>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

        writer = new StringWriter();
Salin selepas log masuk

        factory = BindingDirectory.getFactory(ListBean.class);
Salin selepas log masuk

        // marshal 编组
Salin selepas log masuk
Salin selepas log masuk

        IMarshallingContext mctx = factory.createMarshallingContext();
Salin selepas log masuk
Salin selepas log masuk

        mctx.setIndent(2);
Salin selepas log masuk
Salin selepas log masuk

        mctx.marshalDocument(listBean, "UTF-8", null, writer);
Salin selepas log masuk

        fail(writer);
Salin selepas log masuk
Salin selepas log masuk

        <br/>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

        reader = new StringReader(writer.toString());
Salin selepas log masuk
Salin selepas log masuk

        //unmarshal 解组
Salin selepas log masuk
Salin selepas log masuk

        IUnmarshallingContext uctx = factory.createUnmarshallingContext();
Salin selepas log masuk
Salin selepas log masuk

        listBean = (ListBean) uctx.unmarshalDocument(reader, null);
Salin selepas log masuk

        <br/>
Salin selepas log masuk
Salin selepas log masuk

        fail(listBean.getList().get(0));
Salin selepas log masuk

        fail(listBean.getList().get(1));
Salin selepas log masuk

    } catch (Exception e) {
Salin selepas log masuk
Salin selepas log masuk

        e.printStackTrace();
Salin selepas log masuk
Salin selepas log masuk

    }
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

}
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk


b) ListBean代码



package com.hoo.entity;
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

 <br/>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

import java.util.List;
Salin selepas log masuk

 <br/>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

public class ListBean {
Salin selepas log masuk

    private String name;
Salin selepas log masuk

    private List list;
Salin selepas log masuk

}
Salin selepas log masuk
Salin selepas log masuk


c) 生成bind.xml

执行dos命令:

java -cp bin;lib/jibx-tools.jar;lib/log4j-1.2.16.jar org.jibx.binding.BindingGenerator -f bind.xml com.hoo.entity.ListBean

输出:

clip_image008

d) 执行完后会生产bind.xml

Bind文件



?xml version="1.0" encoding="UTF-8"?>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

binding value-style="attribute">
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

  mapping class="com.hoo.entity.ListBean" name="list-bean">
Salin selepas log masuk

    value style="element" name="name" field="name" usage="optional"/>
Salin selepas log masuk

    collection field="list" usage="optional" factory="org.jibx.runtime.Utility.arrayListFactory"/>
Salin selepas log masuk

  /mapping>
Salin selepas log masuk

/binding>
Salin selepas log masuk
Salin selepas log masuk


e) 运行Compile工具类

在运行前,一定要将最先前运行的Account那个类的bind.xml文件的内容加入到现在这个bind.xml中,因为ListBean依赖了Account这个类。

命令如下:

java -cp bin;lib/jibx-bind.jar org.jibx.binding.Compile -v bind.xml

运行后你可以看到最后出现这个

clip_image010

f) 运行Test程序,结果如下:

?xml version="1.0" encoding="UTF-8"?>
   
list-bean>
   
  account id="1">
   
    name>jack/name>
   
    email>email/email>
   
    address>北京/address>
   
    birthday>
   
      birthday>2010-11-22/birthday>
   
    /birthday>
   
  /account>
   
  account id="2">
   
    name>tom/name>
   
    email>tom@125.com/email>
   
    address>china/address>
   
    birthday>
   
      birthday>2010-11-22/birthday>
   
    /birthday>
   
  /account>
   
/list-bean>
   
1#jack#email#北京#2010-11-22
   
2#tom#tom@125.com#china#2010-11-22
Salin selepas log masuk

3、 转换Java对象数组

a) Test程序

/**
   
 * function:转换对象数组
   
 * @author hoojo
   
 * @createDate 2011-4-26 下午05:32:03
   
 */
   
@Test
   
public void arrayBean2XML() {
   
    try {
   
        Account[] acc = new Account[2];
   
        acc[0] = bean;
   
        bean = new Account();
   
        bean.setName("tom");
   
        bean.setId(223);
   
        acc[1] = bean;
   
        AccountArray array = new AccountArray();
   
        array.setAccounts(acc);
   
        
   
        
   
        writer = new StringWriter();
   
        factory = BindingDirectory.getFactory(AccountArray.class);
   
        // marshal 编组
   
        IMarshallingContext mctx = factory.createMarshallingContext();
   
        mctx.setIndent(2);
   
        mctx.marshalDocument(array, "UTF-8", null, writer);
   
        fail(writer);
   
        
   
        reader = new StringReader(writer.toString());
   
        //unmarshal 解组
   
        IUnmarshallingContext uctx = factory.createUnmarshallingContext();
   
        array = (AccountArray) uctx.unmarshalDocument(reader, null);
   
        
   
        fail(array.getAccounts()[0]);
   
        fail(array.getAccounts()[1]);
   
    } catch (Exception e) {
   
        e.printStackTrace();
   
    }
   
}
Salin selepas log masuk


b) AccountArray代码



package com.hoo.entity;
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

 <br/>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

public class AccountArray {
Salin selepas log masuk

    private Account[] accounts;
Salin selepas log masuk

    private int size;
Salin selepas log masuk

    public int getSize() {
Salin selepas log masuk

        size = accounts.length;
Salin selepas log masuk

        return size;
Salin selepas log masuk

    }
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

    public void setSize(int size) {
Salin selepas log masuk

        this.size = size;
Salin selepas log masuk

    }
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

    public Account[] getAccounts() {
Salin selepas log masuk

        return accounts;
Salin selepas log masuk

    }
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

    public void setAccounts(Account[] accounts) {
Salin selepas log masuk

        this.accounts = accounts;
Salin selepas log masuk

    }
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

}
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk


c) 运行命令生成bind.xml文件

命令如下:

java -cp bin;lib/jibx-tools.jar;lib/log4j-1.2.16.jar org.jibx.binding.BindingGenerator -f bind.xml com.hoo.entity.Account com.hoo.entity.AccountArray

因为AccountArray依赖Account,所以后面带2个类

clip_image012

d) 运行Compile命令

java -cp bin;lib/jibx-bind.jar org.jibx.binding.Compile -v bind.xml

e) 执行完后,就可以运行Test程序了,结果如下



?xml version="1.0" encoding="UTF-8"?>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

account-array size="0">
Salin selepas log masuk

  account id="1">
Salin selepas log masuk

    name>jack/name>
Salin selepas log masuk

    email>email/email>
Salin selepas log masuk

    address>北京/address>
Salin selepas log masuk

    birthday>
Salin selepas log masuk

      birthday>2010-11-22/birthday>
Salin selepas log masuk

    /birthday>
Salin selepas log masuk

  /account>
Salin selepas log masuk

  account id="223">
Salin selepas log masuk

    name>tom/name>
Salin selepas log masuk

  /account>
Salin selepas log masuk

/account-array>
Salin selepas log masuk

1#jack#email#北京#2010-11-22
Salin selepas log masuk

223#tom#null#null#null
Salin selepas log masuk


4、 转换带Map结合的JavaEntity对象

a) Test代码

/**
   
 * function:转换Map集合
   
 * @author hoojo
   
 * @createDate 2011-4-26 下午05:40:34
   
 */
   
@Test
   
public void mapBean2XML() {
   
    try {
   
        MapBean mapBean = new MapBean();
   
        HashMapmap = new HashMap();
   
        map.put("No1", bean);
   
        
   
        bean = new Account();
   
        bean.setAddress("china");
   
        bean.setEmail("tom@125.com");
   
        bean.setId(2);
   
        bean.setName("tom");
   
        Birthday day = new Birthday("2010-11-22");
   
        bean.setBirthday(day);
   
        
   
        map.put("No2", bean);
   
        mapBean.setMap(map);
   
        
   
        factory = BindingDirectory.getFactory(MapBean.class);
   
        writer = new StringWriter();
   
        // marshal 编组
   
        IMarshallingContext mctx = factory.createMarshallingContext();
   
        mctx.setIndent(2);
   
        mctx.marshalDocument(mapBean, "UTF-8", null, writer);
   
        fail(writer);
   
        
   
        reader = new StringReader(writer.toString());
   
        //unmarshal 解组
   
        IUnmarshallingContext uctx = factory.createUnmarshallingContext();
   
        mapBean = (MapBean) uctx.unmarshalDocument(reader, null);
   
        
   
        fail(mapBean.getMap());
   
        fail(mapBean.getMap().get("No1"));
   
        fail(mapBean.getMap().get("No2"));
   
    } catch (Exception e) {
   
        e.printStackTrace();
   
    }
   
}
Salin selepas log masuk


b) MapBean代码



package com.hoo.entity;
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

 <br/>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

import java.util.HashMap;
Salin selepas log masuk

 <br/>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

public class MapBean {
Salin selepas log masuk

    private HashMapmap;
Salin selepas log masuk

    <br/>
Salin selepas log masuk

    public HashMapgetMap() {
Salin selepas log masuk

        return map;
Salin selepas log masuk

    }
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

    public void setMap(HashMapmap) {
Salin selepas log masuk

        this.map = map;
Salin selepas log masuk

    }
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

}
Salin selepas log masuk
Salin selepas log masuk


c) 生成bind.xml,命令如下

E:/Study/WebHttpUtils>java -cp bin;lib/jibx-tools.jar;lib/log4j-1.2.16.jar org.jibx.binding.BindingGenerator -f bind.xml com.hoo.entity.Account com.hoo.entity.MapBean

运行后,会生产bind.xml;修改bind.xml内容如下:



?xml version="1.0" encoding="UTF-8"?>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

binding value-style="attribute">
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

    mapping class="com.hoo.entity.Account" name="account">
Salin selepas log masuk

        value name="id" field="id" />
Salin selepas log masuk

        value style="element" name="name" field="name" usage="optional" />
Salin selepas log masuk

        value style="element" name="email" field="email" usage="optional" />
Salin selepas log masuk

        value style="element" name="address" field="address" usage="optional" />
Salin selepas log masuk

        structure field="birthday" usage="optional" name="birthday">
Salin selepas log masuk

            value style="element" name="birthday" field="birthday" usage="optional" />
Salin selepas log masuk

        /structure>
Salin selepas log masuk

    /mapping>
Salin selepas log masuk

    mapping class="com.hoo.entity.MapBean" name="map-bean">
Salin selepas log masuk

        structure field="map" usage="optional" name="map"
Salin selepas log masuk

            marshaller="com.hoo.util.HashMapper" unmarshaller="com.hoo.util.HashMapper">
Salin selepas log masuk

        /structure>
Salin selepas log masuk

    /mapping>
Salin selepas log masuk

/binding>
Salin selepas log masuk
Salin selepas log masuk


注意上面的MapBean的structure元素的内容是经过修改的。一定要带上marshaller或unmarshaller,不然无法转换HashMap的。

d) HashMapper代码

package com.hoo.util;
   
 
   
import java.util.HashMap;
   
import java.util.Iterator;
   
import java.util.Map;
   
import org.jibx.runtime.IAliasable;
   
import org.jibx.runtime.IMarshallable;
   
import org.jibx.runtime.IMarshaller;
   
import org.jibx.runtime.IMarshallingContext;
   
import org.jibx.runtime.IUnmarshaller;
   
import org.jibx.runtime.IUnmarshallingContext;
   
import org.jibx.runtime.JiBXException;
   
import org.jibx.runtime.impl.MarshallingContext;
   
import org.jibx.runtime.impl.UnmarshallingContext;
   
 
   
/**
   
 * function:http://www.php.cn/ 
 * @file HashMapper.java
 * @package com.hoo.util   
 * @project WebHttpUtils   
 * @blog http://www.php.cn/   
 * @email hoojo_@126.com   
 * @version 1.0   
 */   
public class HashMapper implements IMarshaller, IUnmarshaller, IAliasable   
{
       private static final String SIZE_ATTRIBUTE_NAME = "size";   
    private static final String ENTRY_ELEMENT_NAME = "entry";   
    private static final String KEY_ATTRIBUTE_NAME = "key";   
    private static final int DEFAULT_SIZE = 10;   
       
    private String m_uri;
   
    private int m_index;
   
    private String m_name;
   
    
   
    public HashMapper() {
   
        m_uri = null;
   
        m_index = 0;
   
        m_name = "hashmap";
   
    }
   
    
   
    public HashMapper(String uri, int index, String name) {
   
        m_uri = uri;
   
        m_index = index;
   
        m_name = name;
   
    }
   
    
   
    /* (non-Javadoc)
   
     * @see org.jibx.runtime.IMarshaller#isExtension(int)
   
     */
   
    
   
    public boolean isExtension(int index) {
   
        return false;
   
    }
   
 
   
    /* (non-Javadoc)
   
     * @see org.jibx.runtime.IMarshaller#marshal(java.lang.Object,
   
     *  org.jibx.runtime.IMarshallingContext)
   
     */
   
    
   
    public void marshal(Object obj, IMarshallingContext ictx)
   
        throws JiBXException {
   
        
   
        // make sure the parameters are as expected
   
        if (!(obj instanceof HashMap)) {
   
            throw new JiBXException("Invalid object type for marshaller");
   
        } else if (!(ictx instanceof MarshallingContext)) {
   
            throw new JiBXException("Invalid object type for marshaller");
   
        } else {
   
            
   
            // start by generating start tag for container
   
            MarshallingContext ctx = (MarshallingContext)ictx;
   
            HashMap map = (HashMap)obj;
   
            ctx.startTagAttributes(m_index, m_name).
   
                attribute(m_index, SIZE_ATTRIBUTE_NAME, map.size()).
   
                closeStartContent();
   
            
   
            // loop through all entries in hashmap
   
            Iterator iter = map.entrySet().iterator();
   
            while (iter.hasNext()) {
   
                Map.Entry entry = (Map.Entry)iter.next();
   
                ctx.startTagAttributes(m_index, ENTRY_ELEMENT_NAME);
   
                if (entry.getKey() != null) {
   
                    ctx.attribute(m_index, KEY_ATTRIBUTE_NAME,
   
                        entry.getKey().toString());
   
                }
   
                ctx.closeStartContent();
   
                if (entry.getValue() instanceof IMarshallable) {
   
                    ((IMarshallable)entry.getValue()).marshal(ctx);
   
                    ctx.endTag(m_index, ENTRY_ELEMENT_NAME);
   
                } else {
   
                    throw new JiBXException("Mapped value is not marshallable");
   
                }
   
            }
   
            
   
            // finish with end tag for container element
   
            ctx.endTag(m_index, m_name);
   
        }
   
    }
   
 
   
    /* (non-Javadoc)
   
     * @see org.jibx.runtime.IUnmarshaller#isPresent(org.jibx.runtime.IUnmarshallingContext)
   
     */
   
     
   
    public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException {
   
        return ctx.isAt(m_uri, m_name);
   
    }
   
 
   
    /* (non-Javadoc)
   
     * @see org.jibx.runtime.IUnmarshaller#unmarshal(java.lang.Object,
   
     *  org.jibx.runtime.IUnmarshallingContext)
   
     */
   
     
   
    public Object unmarshal(Object obj, IUnmarshallingContext ictx)
   
        throws JiBXException {
   
        
   
        // make sure we&#39;re at the appropriate start tag
   
        UnmarshallingContext ctx = (UnmarshallingContext)ictx;
   
        if (!ctx.isAt(m_uri, m_name)) {
   
            ctx.throwStartTagNameError(m_uri, m_name);
   
        }
   
        
   
        // create new hashmap if needed
   
        int size = ctx.attributeInt(m_uri, SIZE_ATTRIBUTE_NAME, DEFAULT_SIZE);
   
        HashMap map = (HashMap)obj;
   
        if (map == null) {
   
            map = new HashMap(size);
   
        }
   
        
   
        // process all entries present in document
   
        ctx.parsePastStartTag(m_uri, m_name);
   
        while (ctx.isAt(m_uri, ENTRY_ELEMENT_NAME)) {
   
            Object key = ctx.attributeText(m_uri, KEY_ATTRIBUTE_NAME, null);
   
            ctx.parsePastStartTag(m_uri, ENTRY_ELEMENT_NAME);
   
            Object value = ctx.unmarshalElement();
   
            map.put(key, value);
   
            ctx.parsePastEndTag(m_uri, ENTRY_ELEMENT_NAME);
   
        }
   
        ctx.parsePastEndTag(m_uri, m_name);
   
        return map;
   
    }
   
 
   
    public boolean isExtension(String arg0) {
   
        return false;
   
    }
   
}
Salin selepas log masuk


e) 然后运行Compile命令

E:/Study/WebHttpUtils>java -cp bin;lib/jibx-bind.jar org.jibx.binding.Compile -v bind.xml

f) 结果如下


  
    
      
        tom
        tom@125.com
        
china
2010-11-22
jack email
北京
2010-11-22
{No2=2#tom#tom@125.com#china#2010-11-22, No1=1#jack#email#北京#2010-11-22} 1#jack#email#北京#2010-11-22 2#tom#tom@125.com#china#2010-11-22 顶
Salin selepas log masuk

 以上就是Jibx 处理XML的内容,更多相关内容请关注PHP中文网(www.php.cn)!












Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan