use AXIOM in java xml data binding

AXis 对象模型(AXis Object Model,AXIOM)是 Apache Axis 2 的 XML 对象模型,其目标是提供强大的特性组合彻底改变 XML 处理技术。AXIOM 超越了现有的 XML 处理技术,它把延迟构建和一种快速、轻型的可定制对象模型结合了起来。本文中,软件架构师、AXIOM 的首创者 Eran Chinthaka 介绍了这种新的 XML 处理方法。

测试环境JDK1.4

需要的jar包

axis2/jars/axiom-api-1.2.7.jar
axis2/jars/axiom-dom-1.2.7.jar
axis2/jars/axiom-impl-1.2.7.jar
axis2/jars/axis2-adb-1.4.1.jar
axis2/jars/axis2-kernel-1.4.1.jar
stax/jars/stax-api-1.0.1.jar
axis2/jars/annogen-0.1.0.jar
commons-logging/jars/commons-logging-1.1.1.jar
activation/jars/activation-1.1.jar
wstx/jars/wstx-asl-3.2.3.jar

Student.java

public class Student implements java.io.Serializable {
private static final long serialVersionUID = 771652260758459933L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

JavaXmlDataBinding.java

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.databinding.utils.BeanUtil;
import org.apache.axis2.engine.DefaultObjectSupplier;
import org.apache.axis2.util.StreamWrapper;

public class JavaXmlDataBinding {
public static void main(String args[]) throws AxisFault {
Student student = new Student();
student.setName(“Helloworld”);
student.setAge(100);
javax.xml.stream.XMLStreamReader reader = BeanUtil.getPullParser(student);
StreamWrapper parser = new StreamWrapper(reader);
StAXOMBuilder stAXOMBuilder = OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), parser);
OMElement omElement = stAXOMBuilder.getDocumentElement();
System.out.println(omElement);
Student _student = (Student) BeanUtil.processObject(omElement,Student.class,null, true,new DefaultObjectSupplier());
System.out.println(_student.getName());
}
}

File file = new File(“line-item.xml”);
FileInputStream fis = new FileInputStream(file);
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader reader = xif.createXMLStreamReader(fis);
StAXOMBuilder builder = new StAXOMBuilder(reader);
OMElement lineItem = builder.getDocumentElement();

Iterator it = lineItem.getChildElements();
while(it.hasNext()){
System.out.println(it.next());
}

原创文章,转载请注明: 转载自海波无痕

本文链接地址: use AXIOM in java xml data binding

文章的脚注信息由WordPress的wp-posturl插件自动生成

此条目发表在javaee分类目录,贴了, , , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论链接可以 移除 nofollow.