Axiom簡述--Axis2 的XML處理器
Axis2用Axiom,也就是Axis Object Model,處理SOAP文檔。
Axiom采用pull解析方式,基于StAX(JSR173)。
Pull解析是最近處理XML的一種趨勢。而SAX和DOM都是基于push的解析方式,也就是說解析控制在parser本身。Push解析方式很容易使用,但在處理巨型XML文檔時效率并不好,(因為要在內(nèi)存中生成完成的對象模型)。Pull解析方式顛倒了這種控制方式,增強了parser,只在用戶需要的時候菜進行處理。用戶決定處理或者忽略parser生成的事件。
Axiom和StAX緊密相關,要使用Axiom,StAX相關的jar包也必須在classpath下。
Axiom的一些特性:
??? 1、Lightweight(輕量),更少的內(nèi)存需要。
??? 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方法來獲得內(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:
??? // 可以構建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("??? OMNamespace ns1 = factory.createOMNamespace("
??? 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),
<?xml version='1.0' encoding='utf-8'?>
<x:root xmlns:x="?<y:fool xmlns:y=">
?<ns:ele xmlns:ns="http://namespace">ELE</ns:ele>
</x:root>
參考:
AXIOM Tutorial : http://ws.apache.org/commons/axiom/OMTutorial.html