xml解析 StAX lterator读取

StAX:

  The Streaming API for XML基于流的XML编程接口。StAX即可读文档也可以写文档(读取功能强大)

StAX的API编程接口:

lStAX编程接口都位于javax.xml.stream包中。StAX提供了两种方式的编程接口,它们是:
     lIterator API
        •它的特点是:方便易用、实现简单。
         •主要类是:XMLEventReader和XMLEventWriter。
     lCrusor API
        •它的特点是:运行速度快,底层编程。
        主要类是:XMLStreamReader和XMLStreamWriter 

lterator编程接口:(将所有的事件组织成一个迭代器,通过遍历事件处理)

    XMLEvent
            提供一系列的属性方法,判断文件是开始、结束
              StartDocument文档的开始
            StartElement、EndElement(元素的开始与结束)、Characters(字符串节点元素)
              EntityReference 实体引用
            Comment注释、EndDocument文档结束,DTD约束
            Attribute属性,Namespace命名空间
   XMLEventReader
           提供遍历XML文档的能力。它的源代码如下:
          public interface XMLEventReader extends Iterator {
         可见,它就是一个遍历器
  XMLEventWriter
        XMLEventWriter提供向写XML的功能。

解析步骤:

  

cursor API接口解析步骤:

下例对xml的lterator遍历:

public void readOfSTAX(){
		XMLEventReader er=null;
		try {
			er=XMLInputFactory.newFactory().createXMLEventReader(new FileInputStream("./XML/Two.xml"), "utf-8");
            while(er.hasNext()){
            	XMLEvent e=er.nextEvent();
            	if(e.isStartElement()){
            		StartElement se=e.asStartElement();
            		if(se.getName().getLocalPart().equals("user")){
            			String name=se.getAttributeByName(new QName("name")).getValue();
            			String pwd=se.getAttributeByName(new QName("pwd")).getValue();
            			System.out.println("user:"+name+","+pwd);
            		}else if(se.getName().getLocalPart().equals("contact")){
            			String id=se.getAttributeByName(new QName("id")).getValue();
            			System.out.print("   |---contact:"+id+",");
            			while(true){
            				e=er.nextEvent();
            				if(e.isEndElement()){
            					EndElement ee=e.asEndElement();
            					if(ee.getName().getLocalPart().equals("contact")){
            						System.out.println("/contact");
            						break;
            					}
            				}else if(e.isStartElement()){
            					e=er.nextEvent();
            					if(e.isCharacters()){
            						System.out.print(e.asCharacters()+",");
            					}
            				}
            			}
            		}
            	}else if(e.isEndElement()){
            		EndElement ue=e.asEndElement();
            		if(ue.getName().getLocalPart().equals("user"))
            			System.out.println("/user");
            		
            	}
            }
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (XMLStreamException e) {
			e.printStackTrace();
		} catch (FactoryConfigurationError e) {
			e.printStackTrace();
		}
	}

解析的xml文档:

<?xml version="1.0" encoding="utf-8"?>

<contacts xmlns="http://www.example.org/Two2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/Two2 Two2.xsd">  
  <user name="jack" pwd="309fee4e541e51de2e41f21bebb342aa"> 
    <contact id="bfee73a0c39fd13e3f6c5b65eae93a24"> 
      <name>张三</name>  
      <sex>男</sex>  
      <age>24</age>  
      <address>湖南益阳</address>  
      <tel>15576256943</tel> 
    </contact>  
    <contact id="c1517f3393e3ff2a3419dba754fbdc13"> 
      <name>李四</name>  
      <sex>男</sex>  
      <age>25</age>  
      <address>湖南长沙</address>  
      <tel>15576259835</tel> 
    </contact>  
    <contact id="ce646fc8ccb569ab3359b848feb71c00">
      <name>Rose</name>
      <sex>女</sex>
      <age>22</age>
      <address>湖南常德</address>
      <tel>15543269865</tel>
    </contact>
  </user> 
</contacts>

解析结果:

user:jack,309fee4e541e51de2e41f21bebb342aa
   |---contact:bfee73a0c39fd13e3f6c5b65eae93a24,张三,男,24,湖南益阳,15576256943,/contact
   |---contact:c1517f3393e3ff2a3419dba754fbdc13,李四,男,25,湖南长沙,15576259835,/contact
   |---contact:ce646fc8ccb569ab3359b848feb71c00,Rose,女,22,湖南常德,15543269865,/contact
/user

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值