`
mengxianhua
  • 浏览: 31748 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

AXIOM 读写 xml文件

阅读更多
  • 什么是AXIOM?

Axiom ,也就是Axis Object Model
Axis2用Axiom处理soap文档和soap信息。

  • Axiom的一些特性:
  1. Lightweight(轻量),更少的内存需要。
  2. Deferred building(延迟构建)
  3. 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();

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读
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

转自:http://blog.sina.com.cn/s/blog_6151984a0100lq9p.html

分享到:
评论

相关推荐

    axiom API文档,javadoc格式排版

    axiom是axis下操作xml文件的重要工具,本文档是其api接口说明

    axiom-api-1.2.10.jar

    axiom-api-1.2.10.jar包,开发使用,Axiom ,也就是Axis Object Model,Apache下一款XML对象模型 Axis2用Axiom处理soap文档和soap信息。 Axiom的一些特性: Lightweight(轻量),更少的内存需要。 Deferred ...

    axis2 axiom api 文档

    AXIOM 还不是另一种对象模型。它有着明确的设计目标:大幅提升 Apache 下一代 SOAP 协议栈 Axis 2 的性能。结果造就了不同于其他对象模型的 AXIOM(也称为 OM),因为它突出了构造的轻型,并且 ... 这里是它的api 文档

    AXIOM 测试版硬件_HTML_代码_设计_文档_相关文件_下载

    此存储库包含与 AXIOM Beta 硬件相关的所有信息和文件。 AXIOM Beta是由apertus°制造的开源、开放硬件、专业级数码胶片相机。它被设计成模块化的,例如可互换的传感器前端等,并支持以 4K 分辨率录制。 相机结构...

    axiom教程完整版

    axiom教程---------------------------------------------

    axiom.jar包

    axiom.jar包,axiom.jar包,axiom.jar包,axiom.jar包

    c# Axiom WayPointNavigationSystem.zip

    c# Axiom WayPointNavigationSystem.zip c# Axiom WayPointNavigationSystem.zip

    axiom_reference.chm

    axiom_reference.chm别说我要的分数高,在网上哥这是第一份chm格式的aiom的api文档,绝对值

    axiom-impl.jar.zip

    标签:axiom-impl.jar.zip,axiom,impl,jar.zip包下载,依赖包

    xml的序列化与验证

    3、利用XSD文件的XML3种验证方法 1)Dom4j的SAXValidator (dom4j.jar, javax.xml.parsers 相关类) 2)Javax.xml.validation API(Java1.5及以上) 3)Jdom(jdom.jar,xerces.jar) 4、详细说明ppt文档

    axiom api

    apache axiom api,解压后为html格式,首页为index.html

    axiom-api-1.2.7.jar

    axiom-api-1.2.7.jar为的是更好的发扬优良的精要的代码!

    Axiom软件手册.pdf

    加拿大RBH门禁技术公司开发的AxiomV门禁控制与安全管理集成系统用户手册。官网注册特别繁琐,向代理厂家要的资料,供大家学习

    axiom-impl-1.2.11.jar.zip

    标签:axiom-impl-1.2.11.jar.zip,axiom,impl,1.2.11,jar.zip包下载,依赖包

    axiom-api-1.2.12.jar.zip

    标签:axiom-api-1.2.12.jar.zip,axiom,api,1.2.12,jar.zip包下载,依赖包

    axiom-dom-1.2.7.jar

    axiom-dom-1.2.7.jar-----------------------------

    axiom.jar.zip

    标签:axiom.jar.zip,axiom,jar.zip包下载,依赖包

    axiom-api-1.2.8.jar

    Bumped AXIOM version to 1.2.8 and added axiom-impl to the dependencies of d3_commons.

    axiom-c14n-1.2.8.jar

    axiom-c14n-1.2.8.jar axiom-c14n-1.2.8.jar axiom-c14n-1.2.8.jar

    axiom-all-1.2.12_1.jar

    标签:axiom-all-1.2.12_1.jar,axiom,all,1.2.12_1,jar包下载,依赖包

Global site tag (gtag.js) - Google Analytics