java axis2 xml参数_Axiom简述--Axis2 的XML处理器

Axiom简述--Axis2 的XML处理器

Axis2用Axiom,也就是Axis Object Model,处理SOAP文档。

Axiom采用pull解析方式,基于StAX(JSR173)。

Pull解析是最近处理XML的一种趋势。而SAX和DOM都是基于push的解析方式,也就是说解析控制在parser本身。Push解析方式很容易使用,但在处理巨型XML文档时效率并不好,(因为要在内存中生成完成的对象模型)。Pull解析方式颠倒了这种控制方式,增强了parser,只在用户需要的时候菜进行处理。用户决定处理或者忽略parser生成的事件。

Axiom和StAX紧密相关,要使用Axiom,StAX相关的jar包也必须在classpath下。

Axiom的一些特性:

1、Lightweight(轻量),更少的内存需要。

2、Deferred building(延迟构建),可以说是最重要的OM特性,

3、Pull based(pull模式),OM基于StAX--标准的pull parser API。

Axiom读XML:

// 首先构建parser,

XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(

new FileInputStream("5.xml"));

// 还需要builder对象,

StAXOMBuilder builder = new StAXOMBuilder(parser);

// get the root element

// OMElement documentElement = builder.getDocumentElement();

OMDocument doc = builder.getDocument();

OMElement cre = doc.getOMDocumentElement().getFirstChildWithName(new QName("fool"));

// OMElement有一系列的get方法来获得内容。

cre.serialize(System.out); // cache on

cre.serializeAndConsume(System.out); // cache off

// will NOT build the OMTree in the memory.

// So you are at your own risk of losing information.

String creStr = cre.toStringWithConsume();

// call toString, will build the OMTree in the memory.

System.out.println(cre);

Axiom写XML:

// 可以构建writer做输出器,

XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(

new FileOutputStream("2.xml"));

// 通常通过OMFactory来构造XML文档中的element,下面是一些示例代码。

OMFactory factory = OMAbstractFactory.getOMFactory();

OMDocument doc = factory.createOMDocument();

OMNamespace ns = factory.createOMNamespace("http://demo.axiom","x");

OMNamespace ns1 = factory.createOMNamespace("http://ot.demo.axiom","y");

OMElement root = factory.createOMElement("root",ns);

OMElement elt11 = factory.createOMElement("fool",ns1);

elt11.addChild(factory.createOMText("YY"));

OMElement ele = factory.createOMElement("ele", "http://namespace", "ns");

ele.addChild(factory.createOMText("ELE"));

root.addAttribute(factory.createOMAttribute("attr", ns, "test attr"));

root.addChild(elt11);

root.addChild(ele);

doc.addChild(root);

root.serialize(writer); // cache on

writer.flush();

doc.serializeAndConsume(new FileOutputStream("3.xml"));

OMOutputFormat oof = new OMOutputFormat();

doc.serializeAndConsume(new FileOutputStream("5.xml"), oof); // cache off

//      ele.detach();

ele.serialize(System.out); // 即使detach(),依然会输出ele

doc.serialize(System.out); // 如果detach(),就不会有ele到document里。

关于serialize和serializeAndConsume,前者会强制构建OMTree,或者则不会。

关于detach,它只影响OMElement本身和OMTree的关系,并不影响OMElement本身。

与之对应的还有一个build方法,build会强制build整个OMTree出来。

这两个方法通常用在处理OMElement与OMTree的关系上。从输入流构建出OMElement(build)以及把OMElement从输入流断开(detach),以便放到输出流。输入流和输出流是不同的OMTree。

测试用的XML文档(5.xml),

YY

ELE

posted on 2006-12-08 21:47 竹十一 阅读(3926) 评论(0)  编辑  收藏 所属分类: SOA

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值