当前位置:编程学习 > XML/UML >>

jibx进行xml数据绑定的binging.xml配置

1、给一个例子:
 
<binding xmlns:ns1="http://vteba.com/service/xml/jibx" name="bind" package="com.vteba.service.xml.jibx">
  <namespace uri="http://vteba.com/service/xml/jibx" default="elements"/>
  <mapping abstract="true" type-name="ns1:customer" class="com.vteba.service.xml.jibx.Customer">
    <structure map-as="ns1:person" field="person" usage="optional" name="person"/>
    <value style="element" name="street" field="street" usage="optional"/>
    <value style="element" name="city" field="city" usage="optional"/>
    <value style="element" name="state" field="state" usage="optional"/>
    <value style="attribute" name="zip" field="zip" usage="optional"/>
    <value style="element" name="phone" field="phone" usage="optional"/>
    <collection field="nameList" usage="optional" create-type="java.util.ArrayList">
      <value name="name" type="java.lang.String"/>
    </collection>
    <collection field="personList" usage="optional" create-type="java.util.ArrayList">
      <structure map-as="ns1:person" name="person"/>
    </collection>
  </mapping>
  <mapping class="com.vteba.service.xml.jibx.Customer" name="customer">
    <structure map-as="ns1:customer"/>
  </mapping>
  <mapping abstract="true" type-name="ns1:person" class="com.vteba.service.xml.jibx.Person">
    <value style="attribute" name="customerNumber" field="customerNumber"/>
    <value style="element" name="firstName" field="firstName" usage="optional"/>
    <value style="element" name="lastName" field="lastName" usage="optional"/>
  </mapping>
</binding>
2、这个文件是使用jibx提供的工具生成。不用在手工去写了。当然你可以参考jibx提供的20多个例子来写。
 
jibx工具类,org.jibx.binding.generator.BindGen或org.jibx.binding.BindingGenerator这两个类都可以,前者在jibx-tools.jar
 
中,后者在jibx-bind.jar中。
 
 
3、命令 
 
 
 
4、当然你可以把这些命令写在ant脚本中,或者使用java main方法来执行。此处直接使用命令行。
 
我使用的是maven,在命令行中切换到target文件夹下的classes目录。
 
上面的java 是运行某个程序 –cp是依赖的classpath路径的jar、zip等文件,-b 是输出文件名称,是BindGen类的参数。这样会在classes目录中生成bind.xml文件和jibx.xsd文件。bind.xml既是开头贴出来的文件。
 
 
5、然后你可以通过上述bind.xml文件,对jibx类进行字节码增强。
 
有两种方式可以使用,一是使用ant,官方提供了很多例子,可以自己参考写。
 
二是是jibx eclipse maven插件。会自动绑定,推荐使用这个。可以到网上去下载
 
 
 
上面还有一个jibx.xsd文件,可以根据他产生java类。
 
 
 
6、下面既是使用类
 
package com.vteba.service.xml.jibx;
 
import java.util.List;
 
public class Customer {
public Person person;
public String street;
public String city;
public String state;
public Integer zip;
public String phone;
public List<String> nameList;
public List<Person> personList;
 
public Person getPerson() {
return person;
}
 
public void setPerson(Person person) {
this.person = person;
}
 
public String getStreet() {
return street;
}
 
public void setStreet(String street) {
this.street = street;
}
 
public String getCity() {
return city;
}
 
public void setCity(String city) {
this.city = city;
}
 
public String getState() {
return state;
}
 
public void setState(String state) {
this.state = state;
}
 
public Integer getZip() {
return zip;
}
 
public void setZip(Integer zip) {
this.zip = zip;
}
 
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
 
public List<Person> getPersonList() {
return personList;
}
 
public void setPersonList(List<Person> personList) {
this.personList = personList;
}
 
public List<String> getNameList() {
return nameList;
}
 
public void setNameList(List<String> nameList) {
this.nameList = nameList;
}
 
}
 
public class Person {
public int customerNumber;
public String firstName;
public String lastName;
 
 
public int getCustomerNumber() {
return customerNumber;
}
 
 
public void setCustomerNumber(int customerNumber) {
this.customerNumber = customerNumber;
}
 
 
public String getFirstName() {
return firstName;
}
 
 
public void setFirstName(String firstName) {
this.firstName = firstName;
}
 
 
public String getLastName() {
return lastName;
}
 
 
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
 
7、这个例子已经包含了,一般类的映射,List<JavaBean> List<String>。
 
如果将 List<String> 换成String[], create-type属性就不需要指定了。
 
 
关于绑定文件的各个属性的意思,请参考官方文档。attribute是作为属性,element是作为子元素。
 
对于collection,如果不指定name属性,映射的xml将不会有最外层元素。
 
补充:web前端 , JavaScript ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,