日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AXIOM学习

發布時間:2023/12/20 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AXIOM学习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

AXIOM的全稱為AXIs Object Model,最初是作為Apache Axis 2的XML對象模型開發的。但是后來變成了WS Commons Project的一部分,以便收益于Axis2外的其他項目。
Overview and Features
????? AXIOM是一個實現了延遲構造和拉解析(pull parsing)的輕量級XML解析器。延遲構造是AXIOM的最重要的特性之一,它可以實現對象在使用時才構造。而這個延遲構造的功能實現是基于標準的拉式解析器——StAX。
What is Pull Parsing?
??? 簡單介紹一下“拉式解析”的概念。一個XML文檔可以通過“拉式”或“推式”中任意一種方式來解析。“拉式”是目前流行的XML解析方式。傳統的XML解 析框架,例如SAX和DOM都是“推式”的,那意味著解析過程是由解析器本身來控制的。這樣的實現方式看似不錯,并且使用方便,但是在解析大型XML文檔 時效率就差了,因為整個文檔對象模型都要生成在內存里。而“拉式解析”剛好顛倒了解析過程中的控制關系,解析器只對用戶指定的部分進行解析(好比傳送帶上 的一盤盤壽司,我只取我感興趣的那盤,而不是把經過我面前的都拿下來,篩選后再把不感興趣的放上去,留下想吃的那部分)。這樣用戶可以決定是保存或是拋棄 解析器生成的事件。OM(對象模型)便是基于“拉式解析”的。
Working with AXIOM
要使用AXIOM需要下載它的API包。AXIOM開始是作為AXIS2的一部分出現的,但現在已經可以單獨下載了,當然在AXIS2的發布包里還是可以找到它的。
Creating an AXIOM
我們可以通過三種方式創建一個AXIOM:Pull Event stream,Push Event stream或者由程序自動創建。本文主要演示通過第一種和第三種方式來構建一個AXIOM,它們也是創建AXIOM最常用的方式。
Creating an AXIOM from an Input Stream
下面的代碼演示了如何從一個文件輸入流來創建一個AXIOM:

//首先需要創建一個parser或是reader,這里我們創建一個parser. XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(file)); //然后需要創建一個builder來使用剛才創建的parser,這里我們使用StAXOMBuilder。 StAXOMBuilder builder = new StAXOMBuilder(parser); //最后便可以通過builder獲取一個document element。 OMElement documentElement = builder.getDocumentElement(); ?

注意,當我們從builder獲取到document element時,builder僅僅是返回一個指向,我們將要操作的XML數據仍然在數據流里,沒有被取出來,對象樹也沒有被創建。對象樹只有到我們導航或是構建AXIOM時創建。
Creating an AXIOM Using a String
現在,讓我們通過一個字符串來創建AXIOM,這是一個非常簡單、方便的方式。

String xmlString = "<book>" + "<name>Quick-start Axis</name>" + "<isbn>978-1-84719-286-8</isbn>" + "</book>"; ByteArrayInputStream xmlStream = new ByteArrayInputStream(xmlString. getBytes()); //create a builder. Since we want the XML as a plain XML, we can just use the plain OMBuilder StAXBuilder builder = new StAXOMBuilder(xmlStream); //return the root element. OMElement documentElement = builder.getDocumentElement();

?
Creating an AXIOM Programmatically

//獲取一個 factory OMFactory factory = OMAbstractFactory.getOMFactory(); //使用這個factory來創建一個命名空間對象。 OMNamespace axis2 = factory.createOMNamespace("axis2","ns"); //使用這個factory來創建三個元素。 OMElement root = factory.createOMElement("book",axis2); OMElement name = factory.createOMElement("name",axis2); OMElement isbn = factory.createOMElement("isbn",axis2);


在上面的代碼里,我們可以看到一組factory.create*方法。這些方法可以用來創建不同的XML對象。在AXIOM里,推薦使用這樣的方法來創建AXIOM對象,因為這樣可以使得在AXIOM的其它不同實現中切換變得簡單。
Adding a Child Node and Attributes
到目前為止,我們已經學習了創建AXIOM和使用StAX的API,但這些對于使AXIOM工作起來還是不夠的。我們還需要學習向AXIOM添加子節點。
在OMElement 接口中已經定義了基本的添加和刪除方法:

public void addChild(OMNode omNode); public void addAttribute(OMAttribute omAttribute);

?
下面讓我們實現向root元素添加子節點:

root.addChild(name); root.addChild(isbn);


*add()方法總是將新添加的子節點作為最后一個子節點加入到父節點中。

?

Working with OM Namespaces
由于對命名空間的處理也是解析XML的關鍵部分,因此AXIOM也提供了一系列API來處理命名空間:
public OMNamespace declareNamespace(String uri, String prefix);
public OMNamespace declareNamespace(OMNamespace namespace);
public OMNamespace findNamespace(String uri, String prefix) throws OMException;
以上方法對于使用其它解析方法處理過XML的朋友來說應該不難理解,但需要注意的是,一個已經添加過一次的命名空間聲明不能被再次添加。
findNamespace方法是一個非常便捷的方法用來在整個對象樹中找到一個命名空間對象。
在序列化過程中,一個由工廠方法直接創建的命名空間不會被立即聲明,只有當序列化到這個命名空間被使用的地方(即遇到它的前綴)時才會被聲明。
如果我們序列化我們創建的元素,我們會得到:

Xml代碼
  • < ns:book ? xmlns:ns = "axis2" > ??
  • < ns:name > </ ns:name > ??
  • < ns:isbn > </ ns:isbn > ??
  • </ ns:book > ??
  • <ns:book xmlns:ns="axis2"> <ns:name></ns:name> <ns:isbn></ns:isbn> </ns:book> ?


    Working with Attributes
    下面讓我們來看一下如何給book元素(代碼中的root)添加一個屬性:

    Java代碼
  • OMAttribute?type?=?factory.createOMAttribute( "type" , null ,???????????????????????????????????????? "web-services" );??
  • root.addAttribute(type);??
  • OMAttribute type = factory.createOMAttribute("type",null, "web-services"); root.addAttribute(type); ?


    這時我們再序列化一下代碼,我們將得到:

    Xml代碼
  • < ns:book ? xmlns:ns = "axis2" ? type = "web-services" > ??
  • < ns:name > </ ns:name > ??
  • < ns:isbn > </ ns:isbn > ??
  • </ ns:book > ??
  • <ns:book xmlns:ns="axis2" type="web-services"> <ns:name></ns:name> <ns:isbn></ns:isbn> </ns:book> ?


    Traversing the AXIOM Tree
    在前面的章節,我們已經學習了如何創建一個元素,如何添加子節點,如何創建命名空間和屬性。現在,就讓我們來遍歷一下整個AXIOM樹。
    遍歷對象結構可以通過常用的獲取子節點列表的方法,但在AXIOM里,獲取子節點得到的是一個iterator。下面的代碼演示來如何通過給定的節點來訪問子節點。子節點的類型為OMNode,可以是OMText或OMElement中的任意一種。

    Java代碼
  • Iterator?children?=?root.getChildren();??
  • while (children.hasNext()){??
  • OMNode?node?=?(OMNode)children.next();??
  • }??
  • Iterator children = root.getChildren(); while(children.hasNext()){ OMNode node = (OMNode)children.next(); } ?


    需 要補充的是,每一個OMNode都和相鄰兄弟節點相連,所以在訪問相鄰節點時可以使用nextSibling() 或是 PreviousSibling()方法。還可以通過直接指定字節點的名稱來獲取子節點,方法getChildWithName (Qname)實現來這個功能,它會返回一個iterator.使用這些iterator的優點是,整個對象結構不會立馬構造,只有當需要的時候才會構 造。
    Serialization
    到目前為止,我們對創建和遍歷AXIOM樹已經有了一定的了解。現在就讓我們將AXIOM樹序列化或是寫入一個輸出流。
    AXIOM可以被序列化成一個純對象模型或是一個拉事件流。序列化過程使用一個XMLStreamWriter對象來寫輸出,因此相同的序列化機制也可以來寫不同類型的輸出,比如text,binary……
    AXIOM提供了個一緩存標志來控制內存中的AXIOM。OMNode提供了兩個方法serializeAndConsume和serialize。當 serializeAndConsume方法被調用時,緩存標志被重置,序列化器不會緩存這個流,因此如果緩存標志不被設置,這個對象模型就不會被創建。 在這種情況下,XML流不會創建對象模型,而直接被序列化到輸出流里。所以如果調用了serializeAndConsume方法,我們就只能序列化一次 AXIOM樹,因為內存中并沒有生成AXIOM對象樹。但是,我們可以多次調用serialize方法。文字可能不好理解,下面讓我看段代碼:

    Java代碼
  • String?xmlStream?=? "<ns:book?xmlns:ns=\"axis2\"?type=\" web-services\??
  • "><ns:name></ns:name><ns:isbn></ns:isbn></ns:book>" ;??
  • //從字符串創建一個輸入流 ??
  • ByteArrayInputStream?byteArrayInputStream?=?new ?ByteArrayInputStream??
  • ???????????????????????????????????????????????(xmlStream.getBytes());??
  • //創建一個builder. ??
  • StAXBuilder?builder?=?new ?StAXOMBuilder(byteArrayInputStream);??
  • //獲取root元素. ??
  • OMElement?root?=?builder.getDocumentElement();??
  • //在這里,我們調用兩次serialize ??
  • root.serialize(System.out);??
  • root.serialize(System.out);??
  • String xmlStream = "<ns:book xmlns:ns=\"axis2\" type=\"web-services\ "><ns:name></ns:name><ns:isbn></ns:isbn></ns:book>"; //從字符串創建一個輸入流 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlStream.getBytes()); //創建一個builder. StAXBuilder builder = new StAXOMBuilder(byteArrayInputStream); //獲取root元素. OMElement root = builder.getDocumentElement(); //在這里,我們調用兩次serialize root.serialize(System.out); root.serialize(System.out); ?


    如果我們執行上面的代碼,我們會在控制臺看到:

    Xml代碼
  • < ns:book ? xmlns:ns = "axis2" ? type = "web-services" > < ns:name > </ ns:name > < ns: ??
  • isbn> </ ns:isbn > </ ns:book > ??
  • < ns:book ? xmlns:ns = "axis2" ? type = "web-services" > < ns:name > </ ns:name > < ns: ??
  • isbn> </ ns:isbn > </ ns:book > ??
  • <ns:book xmlns:ns="axis2" type="web-services"><ns:name></ns:name><ns: isbn></ns:isbn></ns:book> <ns:book xmlns:ns="axis2" type="web-services"><ns:name></ns:name><ns: isbn></ns:isbn></ns:book> ?


    但 是如果我們先調用serializeAndConsume()方法,再調用serialize()方法,就會出現一個異常。這是因為一旦 serializeAndConsume()被調用,緩存標志就會被重置。所以緊接著調用serialize()方法時,我們沒有東西可以被序列化來,因 此就得到了一個異常。
    //將上面的代碼最后兩行做如下改動,便會出現異常。

    Java代碼
  • root.serializeAndConsume(System.out);??
  • root.serialize(System.out);??
  • root.serializeAndConsume(System.out); root.serialize(System.out); ?


    AXIOM and SOAP
    前面已經說過,AXIOM本來是作為Axis2的一部分——它的專屬XML解析器而出現的,而Axis2是一個SOAP框架,那解析SOAP消息自然也是 AXIOM的主要功能之一了。SOAP也是XML,只不過有它獨特的結構而已,所以我們可以很方便的從AXIOM獲取一個SOAP層的API。
    下面我們直接看代碼,用AXIOM分別創建一個SOAP1.1和SOAP1.2文檔。
    Creating a SOAP 1.1 Document:

    Java代碼
  • OMFactory?factory?=?OMAbstractFactory.getOMFactory();??
  • ???OMNamespace?axis2?=?factory.createOMNamespace("axis2" ,? "ns" );??
  • ???OMElement?root?=?factory.createOMElement("book" ,?axis2);??
  • ???OMAttribute?type?=?factory.createOMAttribute("type" , null ,??
  • ?????????????????????????????????????????????????????????"web-services" );??
  • ???root.addAttribute(type);??
  • ???OMElement?name?=?factory.createOMElement("name" ,?axis2);??
  • ???OMElement?isbn?=?factory.createOMElement("isbn" ,?axis2);??
  • ???root.addChild(name);??
  • ???root.addChild(isbn);??
  • ???SOAPFactory?soapFactory?=?OMAbstractFactory.getSOAP11Factory();??
  • ???//get?the?default?envelope ??
  • ???SOAPEnvelope?env?=?soapFactory.getDefaultEnvelope();??
  • ???//add?the?created?child ??
  • ???env.getBody().addChild(root);??
  • ???System.out.println(?env);??
  • OMFactory factory = OMAbstractFactory.getOMFactory();OMNamespace axis2 = factory.createOMNamespace("axis2", "ns");OMElement root = factory.createOMElement("book", axis2);OMAttribute type = factory.createOMAttribute("type",null,"web-services");root.addAttribute(type);OMElement name = factory.createOMElement("name", axis2);OMElement isbn = factory.createOMElement("isbn", axis2);root.addChild(name);root.addChild(isbn);SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();//get the default envelopeSOAPEnvelope env = soapFactory.getDefaultEnvelope();//add the created childenv.getBody().addChild(root);System.out.println( env); ?

    Creating a SOAP 1.2 Document:

    Java代碼
  • OMFactory?factory?=?OMAbstractFactory.getOMFactory();??
  • OMNamespace?axis2?=?factory.createOMNamespace("axis2" ,? "ns" );??
  • OMElement?root?=?factory.createOMElement("book" ,?axis2);??
  • OMAttribute?type?=?factory.createOMAttribute("type" , null ,"web-??
  • services");??
  • root.addAttribute(type);??
  • OMElement?name?=?factory.createOMElement("name" ,?axis2);??
  • OMElement?isbn?=?factory.createOMElement("isbn" ,?axis2);??
  • root.addChild(name);??
  • root.addChild(isbn);??
  • SOAPFactory?soapFactory?=?OMAbstractFactory.getSOAP12Factory();??
  • //get?the?default?envelope ??
  • SOAPEnvelope?env?=?soapFactory.getDefaultEnvelope();??
  • //add?the?created?child ??
  • env.getBody().addChild(root);??
  • System.out.println(?env);??
  • OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace axis2 = factory.createOMNamespace("axis2", "ns"); OMElement root = factory.createOMElement("book", axis2); OMAttribute type = factory.createOMAttribute("type",null,"web- services"); root.addAttribute(type); OMElement name = factory.createOMElement("name", axis2); OMElement isbn = factory.createOMElement("isbn", axis2); root.addChild(name); root.addChild(isbn); SOAPFactory soapFactory = OMAbstractFactory.getSOAP12Factory(); //get the default envelope SOAPEnvelope env = soapFactory.getDefaultEnvelope(); //add the created child env.getBody().addChild(root); System.out.println( env);

    ?
    兩段代碼唯一的區別是,使用的工廠不同而已。
    Summary
    通過以上的介紹,相信大家對AXIOM應該有了一個比較清晰的了解了,這樣上手Axis2應該更容易了。

    總結

    以上是生活随笔為你收集整理的AXIOM学习的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。