xml---sax操作

<?xml version="1.0" encoding="UTF-8"?>
<书架>
    <书>
        <书名>书名1</书名>
        <作者>作者1</作者>
        <售价>售价1</售价>
    </书>
    <书>
        <书名>书名2</书名>
        <作者>作者2</作者>
        <售价>售价2</售价>
    </书>
</书架>

创建Book类来封装解析出来的xml

package day04;

public class Book {
    private String name;
    private String author;
    private String  price;
    public String getName() {
        return name;
    }
    public Book() {
    }
    public Book(String name, String author, String price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }

}

sax--解析过程

package day04;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class Demo3 {

    //sax解析xml文档
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException{
        Demo3 d=new Demo3();
        //1.创建解析工厂
        SAXParserFactory factory = SAXParserFactory.newInstance();

        //2.得到解析器
        SAXParser sp=factory.newSAXParser();

        //3.得到读取器
        XMLReader reader=sp.getXMLReader();

        //4.设置内容处理器
        BeanListHander Hander=d.new BeanListHander();
        reader.setContentHandler(Hander);

        //5.读取xml文档内容
        reader.parse("src/Book.xml");

        List<Book> list=Hander.getBooks();
        System.out.println(list );

    }
    //把xml文档中每一本书封装到一个book对象,并把多个对象放到一个list集合中返回
    class BeanListHander extends DefaultHandler{
        private List<Book> list=new ArrayList<Book>();//存入list集合中
        private String currentTag;//记住当前解析到的是什么标签
        private Book book;
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {
            currentTag=qName;
            if("书".equals(currentTag)){
                 book=new Book();
            }

        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if("书名".equals(currentTag)){
                String name = new String(ch,start,length);
                book.setName(name);
            }
            if("作者".equals(currentTag)){
                String  author= new String(ch,start,length);
                book.setAuthor(author);
            }
            if("售价".equals(currentTag)){
                String price = new String(ch,start,length);
                book.setPrice(price);
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if(qName.equals("书")){
                list.add(book);
                book=null;
            }
            currentTag=null;
        }

        public List<Book> getBooks() {
            return list;
        }

    }

}

节点打到这一句:

List<Book> list=Hander.getBooks();

时间: 2024-12-28 11:44:44

xml---sax操作的相关文章

xml操作-Nested exception: org.xml.sax.SAXParseException: White spaces are required between publicId and systemId. 异常处理

异常如下: org.dom4j.DocumentException: Error on line 2 of document file:///D:/workspaces/struts2/lesson01/src/newfile.xml : White spaces are required between publicId and systemId. Nested exception: White spaces are required between publicId and systemId

Java 操作XML (org.xml.sax)

package hello; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.util.ArrayList; import java.util.List; pu

xml文件操作

一.xml文件操作1.了解xml文件的SAX解析方式 1.1SAX解析XML  //第一步:解析xml的对象  //工厂模式   SAXParser parser = SAXParserFactory       .newInstance()       .newSAXParser();  //第二步:获得xml文件   InputStream is = getResources().openRawResource(R.raw.shu);  //第三步: 处理器   DefaultHandler

xml的操作-解析篇

2014-05-04 11:16 吴文付 工作上,学习上,肯定会碰到xml的操作.这里提供详细的xml解析方法. 这里的规划如下:简单的布局3个按钮.3个按钮,对应3种解析xml的方法.分别为 sax解析 dom解析 pull解析 12:56 完成了dom的解析.比较简单. xml的操作-解析篇,布布扣,bubuko.com

org.xml.sax.SAXParseException: Content is not allowed in prolog

sax错误:org.xml.sax.SAXParseException: Content is not allowed in prolog解决  标签: org. xml. sax. saxparse exception content is not allowed in prolog 分类: 开发技术之java原因及其解决办法: 1.xml编码错误 该xml是UTF-8编码的,如果该文件通过UltraEdit编辑后,会在无BOM头的UTF-8文件中加入BOM,但是DOM4j不认这个BOM(do

转载:sax错误:org.xml.sax.SAXParseException: Content is not allowed in prolog解决

原因及其解决办法: 1.xml编码错误 该xml是UTF-8编码的,如果该文件通过UltraEdit编辑后,会在无BOM头的UTF-8文件中加入BOM,但是DOM4j不认这个BOM(dom4j1.3),解决的办法可以通过升级dom4j到1.6解决www.dom4j.org 什么是BOM?http://www.unicode.org/faq/utf_bom.html#22Unicode规范中有一个BOM的概念.BOM——Byte Order Mark,就是字节序标记.在这里找到一段关于BOM的说明

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允许有内容。

二月 25, 2016 9:24:24 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SpringMvc-1' did not find a matching property. 二月 25,

Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in

1.错误描述 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from file [E:\Ecl

Bug解决方案:org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 8; 不允许有匹配 &quot;[xX][mM][lL]&quot; 的处理指令目标

十月 17, 2016 10:14:30 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org[email protected]2e0fa5d3: startup date [Mon Oct 17 22:14:30 CST 2016]; root of context hierarchy十月 17, 2016 10:14:30 下午 org.spring

(Spring加载xml时)org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element &#39;beans&#39;.

ApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");报错 在启动Spring时,报以下错误,如图: 原因是在xml中spring的xsd的版本配置的不一致,我使用的是spring-2.5.6,但配置文件中配的是3.0.改成如下即可: [xhtml] view plain copy <?xml version="1.0" encoding="UTF-8"