有关于读取XML文档内容的java代码的书写

来源:互联网 发布:人工智能的运用领域 编辑:程序博客网 时间:2024/06/10 07:51

最近通过学习,想分享给大家一些关于读取XML文档的内容的简单代码,很是好用.废话不多说,那就开始了:

这是读取信息的核心代码:

package com.iflytek.data;

import java.util.ArrayList;
import java.util.List;

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

import com.iflytek.entity.Book;

public class BookHandler extends DefaultHandler {

    private List<Book> books = null;
    private Book book = null;
    private String currentTag = null;

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

    @Override
    public void startDocument() throws SAXException {
        
        books = new ArrayList<Book>();
    }

    @Override
    public void endDocument() throws SAXException {
        
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        
        if (qName.equals("book")) {
            book = new Book();
            // 直接获取id的值
            String id = attributes.getValue(0);
            book.setUuid(id);

        }
        currentTag = qName;
        for (int i = 0; i < attributes.getLength(); i++) {
            // 属性名
            String attName = attributes.getQName(i);
            // 属性值
            String attValue = attributes.getValue(i);
            
        }

    }

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

    // 文本几点 包括空白
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        String value = new String(ch, start, length);
        
        if (currentTag != null) {
            if (currentTag.equals("name")) {
                book.setBookname(value);
            } else if (currentTag.equals("publish")) {
                book.setBookpublish(value);
            }else if (currentTag.equals("author")) {
                book.setAuthor(value);
            }else if (currentTag.equals("state")) {
                book.setImp(value);
            }
        }
    }

}
这些代码是对Book对象进行操作的,你需要新建一个Book对象属性可以自己定义,然后在核心代码里面对对应的String进行修改.

还有是main函数的操作和调用:

package com.iflytek.xmltest;

import java.io.File;
import java.io.IOException;

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

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

public class XmlSAXTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        readbySax(new File("e:\\book.xml"));
    }
    
    
    public static void readbySax(File file)
    {
        SAXParserFactory saxFacoty=SAXParserFactory.newInstance();
        try {
            SAXParser  parser=saxFacoty.newSAXParser();
            BookHandler bHandler=new BookHandler();
            parser.parse(file,bHandler);
            
            System.out.println("****************************************");
            for(Book book:bHandler.getBooks())
            {
                System.out.println(book);
            }
            
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

}


0 0
原创粉丝点击