Axiom簡(jiǎn)述--Axis2 的XML處理器
Axis2用Axiom,也就是Axis Object Model,處理SOAP文檔。
Axiom采用pull解析方式,基于StAX(JSR173)。
Pull解析是最近處理XML的一種趨勢(shì)。而SAX和DOM都是基于push的解析方式,也就是說解析控制在parser本身。Push解析方式很容易使用,但在處理巨型XML文檔時(shí)效率并不好,(因?yàn)橐趦?nèi)存中生成完成的對(duì)象模型)。Pull解析方式顛倒了這種控制方式,增強(qiáng)了parser,只在用戶需要的時(shí)候菜進(jìn)行處理。用戶決定處理或者忽略parser生成的事件。
Axiom和StAX緊密相關(guān),要使用Axiom,StAX相關(guān)的jar包也必須在classpath下。
Axiom的一些特性: 1、Lightweight(輕量),更少的內(nèi)存需要。 2、Deferred building(延遲構(gòu)建),可以說是最重要的OM特性, 3、Pull based(pull模式),OM基于StAX--標(biāo)準(zhǔn)的pull parser API。
Axiom讀XML:
// 首先構(gòu)建parser, XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader( new FileInputStream("5.xml")); // 還需要builder對(duì)象, 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方法來獲得內(nèi)容。
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:
// 可以構(gòu)建writer做輸出器, XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter( new FileOutputStream("2.xml"));
// 通常通過OMFactory來構(gòu)造XML文檔中的element,下面是一些示例代碼。 OMFactory factory = OMAbstractFactory.getOMFactory(); OMDocument doc = factory.createOMDocument();
OMNamespace ns = factory.createOMNamespace("http://demo.axiom","x"); OMNamespace ns1 = factory.createOMNamespace("http://mo.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(),依然會(huì)輸出ele doc.serialize(System.out); // 如果detach(),就不會(huì)有ele到document里。
關(guān)于serialize和serializeAndConsume,前者會(huì)強(qiáng)制構(gòu)建OMTree,或者則不會(huì)。 關(guān)于detach,它只影響OMElement本身和OMTree的關(guān)系,并不影響OMElement本身。 與之對(duì)應(yīng)的還有一個(gè)build方法,build會(huì)強(qiáng)制build整個(gè)OMTree出來。 這兩個(gè)方法通常用在處理OMElement與OMTree的關(guān)系上。從輸入流構(gòu)建出OMElement(build)以及把OMElement從輸入流斷開(detach),以便放到輸出流。輸入流和輸出流是不同的OMTree。
測(cè)試用的XML文檔(5.xml),
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <x:root xmlns:x=" <y:fool xmlns:y="yyhttp://mo.axiom">YY</y:fool> <ns:ele xmlns:ns="elehttp://namespace">ELE</ns:ele> </x:root>
參考: AXIOM Tutorial : http://ws./commons/axiom/OMTutorial.html
|