AXIOM 读写 xml文件
- 什么是AXIOM?
Axiom? ,也就是Axis Object Model
Axis2用Axiom處理soap文檔和soap信息。
- Axiom的一些特性:
?
- pull模式
Axiom采用pull解析方式,基于StAX(JSR173)。
SAX和DOM 都是基于push的解析方式,也就是說解析控制在parser本身。
Axiom和StAX緊密相關,要使用Axiom,StAX相關的jar包也必須在classpath下。
- Axiom讀XML:
1.需要的包
| axiom-api-1.2.8.jar | 包含javax.xml.*的 包 JDK中就有javax.xml.*的包,但一定不要使用他們,所以一定要把AXIS2下的此包加入buildPath,如果不加入,系統也不報錯,因為會從JDK中找到,但這樣使用JDK的包就會在運行的時候出異常 |
2.范例test1.xml 文件(用于下面的讀寫)
| <?xml version="1.0" encoding="UTF-8"?> <fool> ??? <student> ??? ??? <name>mac</name> ??? ??? <id>12</id> ??? ??? <age>33</age> ??? ??? <sex>male</sex> ??? </student> ??? <student> ??? ??? <name>silly</name> ??? ??? <id>5</id> ??? ??? <age>12</age> ??? ??? <sex>female</sex> ??? </student> ??? <teacher> ??? ??? <name>Mr. Jones</name> ??? ??? <id>2</id> ??? ??? <age>31</age> ??? ??? <sex>male</sex> ??? </teacher> ??? <student> ??? ??? <name>macy</name> ??? ??? <id>2</id> ??? ??? <age>40</age> ??? ??? <sex>female</sex> ??? </student> ??? <student> ??? ??? <name>tom</name> ??? ??? <id>32</id> ??? ??? <age>31</age> ??? ??? <sex>male</sex> ??? </student> ??? <message>hello world</message> </fool> |
3.簡單讀-----直接匹配name去讀
| ??? ??? // 首先對具體的xml文件構建parser ??? ??? FileInputStream xmlFile = new FileInputStream("test1.xml"); ??? ??? XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(xmlFile); ??? ??? ??? ??? // 還需要StAXOMBuilder對象 ??? ??? StAXOMBuilder builder = new StAXOMBuilder(parser); ??? ??? ??? ??? OMElement doc = builder.getDocumentElement();????? //??? 讀到<fool></fool>??? ??? ??? ??? ??????? OMElement cre = doc.getFirstChildWithName(new QName("student"));? //讀到<student> ??? ??? ??????? OMElement cre1 = cre.getFirstChildWithName(new QName("id"));? //??? 讀到<id></id> ??? ??? System.out.println(cre1.getLocalName()+":"+cre1.getText()); ??? ??? cre1 = cre.getFirstChildWithName(new QName("name"));?????? //??? 讀到<name></name> ??? ??? System.out.println(cre1.getLocalName()+":"+cre1.getText());? ? ??? ??? ??? cre1 = cre.getFirstChildWithName(new QName("age"));? ??? //??? 讀到<age></age> ??? ??? System.out.println(cre1.getLocalName()+":"+cre1.getText());??? ???? ??? ??? ??? cre1 = cre.getFirstChildWithName(new QName("sex"));?? ? //??? 讀到<sex></sex> ??? ??? System.out.println(cre1.getLocalName()+":"+cre1.getText()); |
| 結果: id:12 name:mac age:33 sex:male |
4.復雜讀-----getChild
| ??? ??? FileInputStream xmlFile = new FileInputStream("test1.xml"); ??? ??? XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(xmlFile); ??? ??? ??? ??? StAXOMBuilder builder = new StAXOMBuilder(parser); ??? ??? OMElement doc = builder.getDocumentElement(); ??? ??? ??? ????Iterator<OMElement> iter = doc.getChildElements(); ??? ??? while(iter.hasNext()){ ??? ??? ??? OMElement temp = iter.next(); ??? ??? ??? System.out.println("===================="); ??? ??? ??? System.out.println(temp.getLocalName()); ??? ??? ??? System.out.println(temp.getText()); ??? ??? ??? if(temp.getLocalName().equals("student")){ ??? ??? ??? ??? Iterator<OMElement> iter1 = temp.getChildElements(); ??? ??? ??? ??? System.out.println("----------------"); ??? ??? ??? ??? while(iter1.hasNext()){ ??? ??? ??? ??? ??? OMElement temp1 = iter1.next();??? ??? ??? ??? ??? ??? ??? ??? ??? ??? System.out.println(temp1.getLocalName()+":"+temp1.getText()); ??? ??? ??? ??? } ??? ??? ??? } ??? ??? } |
| 結果: ==================== student ---------------- name:mac id:12 age:33 sex:male ==================== student ---------------- name:silly id:5 age:12 sex:female ==================== teacher?? ==================== student ---------------- name:macy id:2 age:40 sex:female ==================== student ---------------- name:tom id:32 age:31 sex:male ==================== message hello world ? |
- Axiom寫XML:
簡單方式(基本方式,無ns,無attribute)
| 1.建立節點??? ??? ??? // 通常通過OMFactory來構造XML文檔中的element ??? ??? OMFactory factory = OMAbstractFactory.getOMFactory(); ??? ??? ??? ??? //建立doc節點,doc節點會和下面的root節點合并 ??? ??? OMDocument doc = factory.createOMDocument(); ??????? ??? ??? //建立root節點 ??? ??? OMElement root = factory.createOMElement(new QName("root")); ??? ??? ??? ??? //建立兩個普通節點 ??? ??? OMElement stu = factory.createOMElement(new QName("student")); ??? ??? stu.addChild(factory.createOMText("mac")); ??? ??? OMElement tea = factory.createOMElement(new QName("teacher")); ??? ??? tea.addChild(factory.createOMText("silly")); |
| 2.構建樹??? ??? ??? ??? //構建樹,將兩個普通節點連到root節點上 ??? ??? root.addChild(stu); ??? ??? root.addChild(tea); ??? ??? //構建樹,將root節點連到doc節點上 ??? ??? doc.addChild(root); |
| 3. 寫入文件 ??? ??? // 構建writer做輸出器 ??? ??? XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter( ??? ??????????? new FileOutputStream("2.xml")); ??? ??? root.serialize(writer); // cache on ??? ??? writer.flush(); |
| 4. 看結果 "2.xml" <root><student>mac</student><teacher>silly</teacher></root> 此文件缺陷: 1.沒有xml頁首的<?xml version="1.0" encoding="UTF-8"?> 2.沒有回車換行 |
| 5.試著用AXIOM讀 ??? ??? FileInputStream xmlFile = new FileInputStream("2.xml"); ??? ??? XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(xmlFile); ??? ??? ??? ??? StAXOMBuilder builder = new StAXOMBuilder(parser); ??? ??? OMElement doc = builder.getDocumentElement(); ??? ??? ??? ??? Iterator<OMElement> iter = doc.getChildElements(); ??? ??? while(iter.hasNext()){ ??? ??? ??? OMElement temp = iter.next(); ??? ??? ??? System.out.println("===================="); ??? ??? ??? System.out.println(temp.getLocalName()+":"+temp.getText()); ??? ??? } 結果: ==================== student:mac ==================== teacher:silly |
Axiom寫XML,復雜方式
| 1. 構建節點 ??? ??? // 通常通過OMFactory來構造XML文檔中的element ??? ??? OMFactory factory = OMAbstractFactory.getOMFactory(); ??? ??? ??? ??? // 建立 namespace ??? ??? OMNamespace ns = factory.createOMNamespace("http://demo.axiom","x"); ??? ??? OMNamespace ns1 = factory.createOMNamespace("http://ot.demo.axiom","y"); ??? ??? ??? ??? //建立doc節點,doc節點會和下面的root節點合并 ??? ??? OMDocument doc = factory.createOMDocument(); ??? ??? ??? ??? //建立root節點 ??? ??? OMElement root = factory.createOMElement("root",ns); ??? ??? ??? ??? //建立兩個普通節點 ??? ??? OMElement stu = factory.createOMElement("student",ns1); ??? ??? stu.addChild(factory.createOMText("mac")); ??? ??? OMElement tea = factory.createOMElement("teacher", "http://namespace", "ns"); ??? ??? tea.addChild(factory.createOMText("silly")); |
| 2.構建樹??? ??? ??? ??? ??? ??? //構建樹,將兩個普通節點連到root節點上 ??? ??? root.addAttribute(factory.createOMAttribute("attr", ns, "hello world")); ??? ??? root.addChild(stu); ??? ??? root.addChild(tea); ??? ??? //構建樹,將root節點連到doc節點上 ??? ??? doc.addChild(root); |
| 3. 輸出到文件或打印到屏幕??? ??? ??? ??? // 構建writer做輸出器 ??? ??? XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter( ??? ??????????? new FileOutputStream("2.xml")); ??? ??? root.serialize(writer); // cache on ??? ??? writer.flush(); |
| 4. 生成xml如下: <x:root xmlns:x="http://demo.axiom" x:attr="hello world"><y:student xmlns:y="http://ot.demo.axiom">mac</y:student><ns:teacher xmlns:ns="http://namespace">silly</ns:teacher></x:root> ? |
| 5.試著用AXIOM讀 |
?
public void update(OMElement element) throws XMLStreamException {//構建節點本身element.build();//從父節點刪除本節點element.detach();//獲取節點的第一個子節點——symbolOMElement symbolElement = element.getFirstElement();//獲取這個子節點的文本值String symbol = symbolElement.getText();//獲取節點的第二個字節點——priceOMElement priceElement = (OMElement) symbolElement.getNextOMSibling();//獲取price的值String price = priceElement.getText();//把值放置到緩存中map.put(symbol, new Double(price));}string 轉成OMElement
OMElement omBody = AXIOMUtil.stringToOM("<m:GetPrice xmlns:m=\"http://www.w3schools.com/prices\"><m:Item>Apples</m:Item></m:GetPrice>"); 用string 生成soap MessageContext public static MessageContext newMessageContext(String xmlpayload)throws AxisFault, XMLStreamException {MessageContext messageContext = new MessageContext();SOAPEnvelope envelope = factory.createSOAPEnvelope();factory.createSOAPBody(envelope);envelope.getBody().addChild(AXIOMUtil.stringToOM(xmlpayload));messageContext.setEnvelope(envelope);return messageContext; }
總結
以上是生活随笔為你收集整理的AXIOM 读写 xml文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【JZOJ4939】平均值 题解
- 下一篇: 微信开发者工具网络连接失败问题解决方法