zoukankan      html  css  js  c++  java
  • stax(和dom4j功能一样,这个是基于流的一种处理方式)

    package com.yinhai.liyang.stax;
    
    import java.io.IOException;
    import java.io.InputStream;
    import javax.xml.stream.XMLInputFactory;
    import javax.xml.stream.XMLStreamException;
    import javax.xml.stream.XMLStreamReader;
    import org.junit.Test;
    /***
     * 基于光标的模型
     * */
    public class TestStax {
        /**
         * 心得:可以通过不同的值得到不同的节点,然后节点怎么操作随你
         * */
        @Test
        public void TestStax() {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            InputStream in = null;
            try {
                in = TestStax.class.getClassLoader().getSystemResourceAsStream(
                        "books.xml");
                XMLStreamReader reader = factory.createXMLStreamReader(in);
                while (reader.hasNext()) {
                    int note = reader.next();
                    if (note == 1) { // 1代表的是开始节点,具体的代表什么XMLStreamConstants在这个类中去找
                        System.out.println(reader.getName());
                    } else if (note == 4) {// 4代表文本节点
                        System.out.println(reader.getText().trim());
                    } else if (note == 2) {// 2代表结束节点
                        System.out.println("/" + reader.getName());
                    }
                }
            } catch (XMLStreamException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        /**
         * 心得:可以通过开始节点得到文本属性
         * */
        @Test
        public void TestStax01() {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            InputStream in = null;
            try {
                in = TestStax.class.getClassLoader().getSystemResourceAsStream(
                        "books.xml");
                XMLStreamReader reader = factory.createXMLStreamReader(in);
                while (reader.hasNext()) {
                    int note = reader.next();
                    if (note == 1) { // 1代表的是开始节点,具体的代表什么XMLStreamConstants在这个类中去找
                        String name = reader.getName().toString();
                        if (name.equals("book")) {
                        System.out.println(reader.getAttributeName(0)+":"+reader.getAttributeValue(0));
                        }
                    }
                }
            } catch (XMLStreamException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
        /**
         * 心得:可以通过开始节点得到文本值
         * */
        @Test
        public void TestStax02() {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            InputStream in = null;
            try {
                in = TestStax.class.getClassLoader().getSystemResourceAsStream(
                        "books.xml");
    // in = new ByteArrayInputStream(xml.getBytes());//当xml是一个字符串的时候,将字符串装换成流 XMLStreamReader reader
    = factory.createXMLStreamReader(in); while (reader.hasNext()) { int note = reader.next(); if (note == 1) { // 1代表的是开始节点,具体的代表什么XMLStreamConstants在这个类中去找 String name = reader.getName().toString(); if (name.equals("title")) { System.out.println(name+":"+reader.getElementText()); }else if(name.equals("price")){ System.out.println(name+":"+reader.getElementText()); } } } } catch (XMLStreamException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
    package com.yinhai.liyang.stax;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.xml.stream.EventFilter;
    import javax.xml.stream.XMLEventReader;
    import javax.xml.stream.XMLInputFactory;
    import javax.xml.stream.XMLStreamException;
    import javax.xml.stream.events.XMLEvent;
    import org.junit.Test;
    /***
     * 基于迭代的模型
     * */
    public class TestStax02 {
        @Test
        public void TestStax() {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            InputStream in = null;
            try {
                in = TestStax.class.getClassLoader().getSystemResourceAsStream(
                        "books.xml");
                XMLEventReader reader = factory.createXMLEventReader(in);
                while (reader.hasNext()) {
                    XMLEvent event = reader.nextEvent();
                    if (event.isStartElement())
                        System.out.println(event.asStartElement().getName()
                                .toString());
                }
            } catch (XMLStreamException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void TestStaxfilter() {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            InputStream in = null;
            try {
                in = TestStax.class.getClassLoader().getSystemResourceAsStream(
                        "books.xml");
                XMLEventReader reader = factory.createFilteredReader(
                        factory.createXMLEventReader(in), new EventFilter() {
                            @Override
                            public boolean accept(XMLEvent event) {
                                if (event.isStartElement()) {   //过滤,只遍历开始节点
                                    return true;
                                }
                                return false;
                            }
                        });
               int num=0;
                while (reader.hasNext()) {
                  num++;
                    XMLEvent event = reader.nextEvent();
                    if (event.isStartElement())
                        System.out.println(event.asStartElement().getName()
                                .toString());
                }
                System.out.println(num);
            } catch (XMLStreamException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

     基于迭代模型的要获得文本值(重点是先要获得名字,才能获得文本值)为毛要先判断开始节点

    public void TestStax() {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            InputStream in = null;
            try {
                in = TestStax.class.getClassLoader().getSystemResourceAsStream(
                        "books.xml");
                XMLEventReader reader = factory.createXMLEventReader(in);
                while (reader.hasNext()) {
                    XMLEvent event = reader.nextEvent();
                       if(event.isStartElement()) {  
                            String name = event.asStartElement().getName().toString();  
                            if(name.equals("title")) {  //必须先获得名字才获得文本值,有了这个语句才能获得文本值
                                System.out.print(reader.getElementText()+":");  
                            }  
                            if(name.equals("price")) {  //必须先获得名字才能获得文本值,有了这个语句才能获得文本值
                                System.out.print(reader.getElementText()+"
    ");  
                            }  
                        }  
                }
            } catch (XMLStreamException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    1.这里只记录一些学习笔记 2.这里只记录一些学习心得,如果心得方向有错,请留言 2.这里只记录一些日记(只为提升英语,暂时有点忙,等转行了开始写)
  • 相关阅读:
    Qtranslate是Win10系统的一款非常小众的翻译神器,真的是办公人员利器
    大文件查找软件(WizTree) v3.37 ---非常快
    DirPrintOK --- 将文件以树的形式列出、可导出到excel、html文件,用于整理电脑的文件,非常好用
    Linux五大类常用命令
    Android pm list 命令查看手机安装的apk信息
    Android adb命令列出当前设备所有apk安装的路径和包名
    Win10 Python2.7.6 如何使用pip命令?如何离线安装第三方模块?PyCharm设置Python2.7.6,、wxPython安装
    证件照在线免费移除背景或更换背景
    Ubuntu wps办公软件快捷键
    根据书籍的ISBN号查询书籍信息
  • 原文地址:https://www.cnblogs.com/liyang31tg/p/3532462.html
Copyright © 2011-2022 走看看