AXIOM 读写 xml文件
- 什么是AXIOM?
Axiom ,也就是Axis Object Model
Axis2用Axiom处理soap文档和soap信息。
- Axiom的一些特性:
- Lightweight(轻量),更少的内存需要。
- Deferred building(延迟构建)
- Pull based(pull模式),OM基于StAX--标准的pull parser API。
- 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(); if(temp.getLocalName().equals("student")){ |
结果: ==================== student ---------------- ---------------- ==================== ---------------- ---------------- |
- 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")); |
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> 此文件缺陷: |
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()); } 结果: |
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"); |
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读 结果: |
OMElement 节点修改
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文件相关推荐
- dom4j读写xml文件
dom4j读写xml文件 首先我们给出一段示例程序: import java.io.File; import java.io.FileWriter; import java.util.Iterator ...
- [转]C#读写xml文件
c#读写xml文件 已知有一个XML文件(bookstore.xml)如下: <?xml version="1.0" encoding="gb2312"? ...
- (3)[wp7数据存储] WP7 IsolatedStorage系列篇——通过XmlSerializer读写XML文件 [复制链接]...
发表于 2012-5-17 15:51:07 |只看该作者 |倒序浏览 分享到: 本帖最后由 agameboy 于 2012-5-17 17:08 编辑 这一篇我们会通过XmlSerializer读写 ...
- xml文件c语言读取函数,读写xml文件的2个小函数
#region 读写xml文件的2个小函数,2005 4 2 by hyc public void SetXmlFileValue(string xmlPath,string AppKey,strin ...
- java dom读写xml文件_java通过dom读写xml文件
java通过dom读写xml文件 要读的xml文件 encoding="GB2312"?> "男"> 李华姓名> 14年龄> 学生> ...
- Unity(C#)读写xml文件详解
目录 什么是xml文件? xml文件的结构 如何读写xml文件 什么是xml文件? xml文件是一种可拓展标记语言,由于简单易用,常被应用程序用于保存数据,unity也是如此,在我们需要控制读写用户的 ...
- Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件
Fixflow引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素 Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件 Fixflow引擎解析(二)(模型) - BPMN ...
- [QT操作XML]QT读写XML文件,QT修改XML文件
[QT操作XML]QT读写XML文件 XML简介 QT操作XML,写入.读取.修改 XML效果演示 XML简介 概念:Extensible Markup Language 可扩展标记语言(可扩展:标签 ...
- 一次代码重构之旅-快速读写xml文件工具类封装
为了满足系统的灵活性,有些功能经常需要用到配置文件,一般是xml格式的居多.如何能快速读写配置文件呢? 以前都是用dom4j提供的api来读写xml文件,用dom4j读写配置文件总感觉像是在结构化的处 ...
最新文章
- linux下使用gcp拷贝数据的时候显示进度条
- 【已解决】清除linux系统的多余引导
- HTTP协商缓存与HTTP强缓存
- 息中间件kafka与activeMQ、rabbitMQ、zeroMQ、rocketMQ的比较
- 【java】java Integer 缓存 一定是 -128~127 吗
- 基于JAVA+SpringMVC+Mybatis+MYSQL的医院在线预约挂号系统
- Vue父组件访问子组件属性和方法、父子组件双向绑定(两种方法)
- 父窗口、子窗口和Silverlight之间的相互调用
- 4.5 Spark SQL 处理JSON数据
- Hadoop HA HDFS启动错误之org.apache.hadoop.ipc.Client: Retrying connect to server问题解决
- Error:java.lang.RuntimeException: Some file crunching failed
- 学校计算机教室解说词词,小学各专用教室解说词.doc
- 2000元平板电脑推荐2023 2000元左右平板电脑哪款好
- kindle书摘-围城-相爱勿相伤
- Linux蓝牙系统(3) Linux 命令
- tiptop自定义发送邮件
- java 变量类型_Java 变量类型
- 转载-FileZilla Server源码分析(1)
- HTML网页一键变黑白色调
- 研究生初试录取系统C语言,研究生初试录取管理系统