首页 > 其他 > 详细

用Stax方式处理xml

时间:2014-02-13 21:12:45      阅读:350      评论:0      收藏:0      [点我收藏+]

1.读取xml文件,首先用类加载器加载项目目录下的xml文件,从XMLInputFactory创建我所需要的XMLStreamReader,即得到了xml文件。根据XMLStreamConstant

属性值,就可以操作所得到的xml文件内容,详情看以下代码。

bubuko.com,布布扣
public class TestStax {
    public static void main(String[] args) {
        //基于光标式的
//        new TestStax().baseStax();
//        new TestStax().baseStax_01();
        //基于迭代模型的
//       new TestStax().baseStax_02();
     //基于过滤器的 
     new TestStax().baseStax_03();  }
public void baseStax(){ XMLInputFactory xif = XMLInputFactory.newInstance(); InputStream is = null; XMLStreamReader xsr = null; try { //第一种加载文件方式,此文件在根目录下 is = TestStax.class.getClassLoader().getResourceAsStream("menu.xml"); xsr = xif.createXMLStreamReader(is); while(xsr.hasNext()){ int type = xsr.next(); System.out.println(type); if(type == XMLStreamConstants.START_ELEMENT){ System.out.println(xsr.getName()); }else if(type == XMLStreamConstants.CHARACTERS){ System.out.println(xsr.getText().trim()); }else if(type == XMLStreamConstants.END_ELEMENT){ System.out.println("/" + xsr.getName()); } } } catch (Exception e) { e.printStackTrace(); }finally{ if(is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void baseStax_01(){ XMLInputFactory xif = XMLInputFactory.newInstance(); InputStream is = null; XMLStreamReader xmlsr = null; try { //另一种加载文件方式 is = new FileInputStream(System.getProperty("java.class.path") + File.separator + "menu.xml"); xmlsr = xif.createXMLStreamReader(is); while(xmlsr.hasNext()){ int type = xmlsr.next(); //XMLStreamConstants.START_ELEMENT=1 if(type == 1){ if("name".equals(xmlsr.getName().toString())){ System.out.print(xmlsr.getElementText() + ":"); }else if("price".equals(xmlsr.getName().toString())){ System.out.println(xmlsr.getElementText()); } } } } catch (Exception e) { e.printStackTrace(); }finally{ if(is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void baseStax_02(){ XMLInputFactory xif = XMLInputFactory.newInstance(); InputStream is = null;
is
= TestStax.class.getClassLoader().getResourceAsStream("menu.xml"); try { XMLEventReader xmler = xif.createXMLEventReader(is); while(xmler.hasNext()){ XMLEvent xmle = xmler.nextEvent(); if(xmle.isStartElement()){ String name = xmle.asStartElement().getName().toString(); if("name".equals(name)){ System.out.print(xmler.getElementText() + ":"); }else if("price".equals(name)){ System.out.println(xmler.getElementText()); } } } } catch (XMLStreamException e) { // TODO Auto-generated catch block e.printStackTrace(); } }   public void baseStax_03(){
  XMLInputFactory xif = XMLInputFactory.newFactory();
  InputStream is = TestStax.class.getClassLoader().getResourceAsStream("menu.xml");
  XMLEventReader xmler = null;
  try {
   xmler = xif.createFilteredReader(xif.createXMLEventReader(is), new EventFilter() {
    @Override
    public boolean accept(XMLEvent event) {
     if(event.isStartElement()){
      String name = event.asStartElement().getName().toString();
      if("name".equals(name) || "price".equals(name)){
       return true;
      }
     }
     return false;
    }
   } );
  } catch (XMLStreamException e) {
   e.printStackTrace();
  }
  while(xmler.hasNext()){ 
   try {
    XMLEvent xmle = xmler.nextEvent();
    if(xmle.isStartElement()){
     String nm = xmle.asStartElement().getName().toString();
     if("name".equals(nm)){
      System.out.print(xmler.getElementText() + ":");
     }else if("price".equals(nm)){
      System.out.println(xmler.getElementText());
     }
    }
   } catch (XMLStreamException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

}
bubuko.com,布布扣

 

 下面是我说是用的xml,在w3cSchool里弄的xml文件,也可以自己建一个xml文件

bubuko.com,布布扣
<?xml version="1.0" encoding="UTF-8"?>

 <breakfast_menu>
 <food>
  <name>Belgian Waffles</name> 
  <price>$5.95</price> 
  <description>two of our famous Belgian Waffles with plenty of real maple syrup</description> 
  <calories>650</calories> 
  </food>
 <food>
  <name>Strawberry Belgian Waffles</name> 
  <price>$7.95</price> 
  <description>light Belgian waffles covered with strawberries and whipped cream</description> 
  <calories>900</calories> 
  </food>
 <food>
  <name>Berry-Berry Belgian Waffles</name> 
  <price>$8.95</price> 
  <description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description> 
  <calories>900</calories> 
  </food>
 <food>
  <name>French Toast</name> 
  <price>$4.50</price> 
  <description>thick slices made from our homemade sourdough bread</description> 
  <calories>600</calories> 
  </food>
 <food>
  <name>Homestyle Breakfast</name> 
  <price>$6.95</price> 
  <description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description> 
  <calories>950</calories> 
  </food>
  </breakfast_menu>
bubuko.com,布布扣

用Stax方式处理xml

原文:http://www.cnblogs.com/lvzhanhui/p/xiaoqiaolv_stax.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!