⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xppreader.java

📁 解决如何把XML应用到JAVA里问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
 *
 * This software is open source.
 * See the bottom of this file for the licence.
 */

package org.dom4j.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.xpp.ProxyXmlStartTag;

import org.gjt.xpp.XmlEndTag;
import org.gjt.xpp.XmlPullParser;
import org.gjt.xpp.XmlPullParserException;
import org.gjt.xpp.XmlPullParserFactory;

/**
 * <p>
 * <code>XPPReader</code> is a Reader of DOM4J documents that uses the fast <a
 * href="http://www.extreme.indiana.edu/soap/xpp/">XML Pull Parser 2.x </a>. It
 * does not currently support comments, CDATA or ProcessingInstructions or
 * validation but it is very fast for use in SOAP style environments.
 * </p>
 * 
 * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
 * @version $Revision: 1.7 $
 */
public class XPPReader {
    /** <code>DocumentFactory</code> used to create new document objects */
    private DocumentFactory factory;

    /** <code>XmlPullParser</code> used to parse XML */
    private XmlPullParser xppParser;

    /** <code>XmlPullParser</code> used to parse XML */
    private XmlPullParserFactory xppFactory;

    /** DispatchHandler to call when each <code>Element</code> is encountered */
    private DispatchHandler dispatchHandler;

    public XPPReader() {
    }

    public XPPReader(DocumentFactory factory) {
        this.factory = factory;
    }

    /**
     * <p>
     * Reads a Document from the given <code>File</code>
     * </p>
     * 
     * @param file
     *            is the <code>File</code> to read from.
     * 
     * @return the newly created Document instance
     * 
     * @throws DocumentException
     *             if an error occurs during parsing.
     * @throws IOException
     *             if a URL could not be made for the given File
     * @throws XmlPullParserException
     *             DOCUMENT ME!
     */
    public Document read(File file) throws DocumentException, IOException,
            XmlPullParserException {
        String systemID = file.getAbsolutePath();

        return read(new BufferedReader(new FileReader(file)), systemID);
    }

    /**
     * <p>
     * Reads a Document from the given <code>URL</code>
     * </p>
     * 
     * @param url
     *            <code>URL</code> to read from.
     * 
     * @return the newly created Document instance
     * 
     * @throws DocumentException
     *             if an error occurs during parsing.
     * @throws IOException
     *             DOCUMENT ME!
     * @throws XmlPullParserException
     *             DOCUMENT ME!
     */
    public Document read(URL url) throws DocumentException, IOException,
            XmlPullParserException {
        String systemID = url.toExternalForm();

        return read(createReader(url.openStream()), systemID);
    }

    /**
     * <p>
     * Reads a Document from the given URL or filename.
     * </p>
     * 
     * <p>
     * If the systemID contains a <code>':'</code> character then it is
     * assumed to be a URL otherwise its assumed to be a file name. If you want
     * finer grained control over this mechansim then please explicitly pass in
     * either a {@link URL}or a {@link File}instance instead of a {@link
     * String} to denote the source of the document.
     * </p>
     * 
     * @param systemID
     *            is a URL for a document or a file name.
     * 
     * @return the newly created Document instance
     * 
     * @throws DocumentException
     *             if an error occurs during parsing.
     * @throws IOException
     *             if a URL could not be made for the given File
     * @throws XmlPullParserException
     *             DOCUMENT ME!
     */
    public Document read(String systemID) throws DocumentException,
            IOException, XmlPullParserException {
        if (systemID.indexOf(':') >= 0) {
            // lets assume its a URL
            return read(new URL(systemID));
        } else {
            // lets assume that we are given a file name
            return read(new File(systemID));
        }
    }

    /**
     * <p>
     * Reads a Document from the given stream
     * </p>
     * 
     * @param in
     *            <code>InputStream</code> to read from.
     * 
     * @return the newly created Document instance
     * 
     * @throws DocumentException
     *             if an error occurs during parsing.
     * @throws IOException
     *             DOCUMENT ME!
     * @throws XmlPullParserException
     *             DOCUMENT ME!
     */
    public Document read(InputStream in) throws DocumentException, IOException,
            XmlPullParserException {
        return read(createReader(in));
    }

    /**
     * <p>
     * Reads a Document from the given <code>Reader</code>
     * </p>
     * 
     * @param reader
     *            is the reader for the input
     * 
     * @return the newly created Document instance
     * 
     * @throws DocumentException
     *             if an error occurs during parsing.
     * @throws IOException
     *             DOCUMENT ME!
     * @throws XmlPullParserException
     *             DOCUMENT ME!
     */
    public Document read(Reader reader) throws DocumentException, IOException,
            XmlPullParserException {
        getXPPParser().setInput(reader);

        return parseDocument();
    }

    /**
     * <p>
     * Reads a Document from the given array of characters
     * </p>
     * 
     * @param text
     *            is the text to parse
     * 
     * @return the newly created Document instance
     * 
     * @throws DocumentException
     *             if an error occurs during parsing.
     * @throws IOException
     *             DOCUMENT ME!
     * @throws XmlPullParserException
     *             DOCUMENT ME!
     */
    public Document read(char[] text) throws DocumentException, IOException,
            XmlPullParserException {
        getXPPParser().setInput(text);

        return parseDocument();
    }

    /**
     * <p>
     * Reads a Document from the given stream
     * </p>
     * 
     * @param in
     *            <code>InputStream</code> to read from.
     * @param systemID
     *            is the URI for the input
     * 
     * @return the newly created Document instance
     * 
     * @throws DocumentException
     *             if an error occurs during parsing.
     * @throws IOException
     *             DOCUMENT ME!
     * @throws XmlPullParserException
     *             DOCUMENT ME!
     */
    public Document read(InputStream in, String systemID)
            throws DocumentException, IOException, XmlPullParserException {
        return read(createReader(in), systemID);
    }

    /**
     * <p>
     * Reads a Document from the given <code>Reader</code>
     * </p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -