AXIOM 读写 xml文件

来源:互联网 发布:同名同姓身份证号软件 编辑:程序博客网 时间:2024/05/15 07:48

 

  • 什么是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

 

原创粉丝点击