Dom4j SAXReader Constructors

 

Dom4j读取xml:
eg1:

package xml;

import java.io.File;

import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

public class XmlReader_Dom4j {
    public static void main(String[] args)  {
        String path = "D:\\test\\中文文件夹名\\namespaces.xml";
        readXml(path);//will throw exception
        File xmlFile=new File(path);
        readXml(xmlFile);
        path = "D:\\test\\path withWhiteSpace\\namespaces.xml";
        readXml(path);

        path = "D:\\test\\normal\\namespaces.xml";
        readXml(path);
    }

    private static void readXml(String path) {
        SAXReader saxReader=new SAXReader();
        try {
            saxReader.read(path);
            System.out.println("success");
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    private static void readXml(File xmlFile) {
        SAXReader saxReader=new SAXReader();
        try {
            saxReader.read(xmlFile);
            System.out.println("success");
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

}

Output:

org.dom4j.DocumentException: unknown protocol: d Nested exception: unknown protocol: d
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.dom4j.io.SAXReader.read(SAXReader.java:321)
    at xml.XmlReader_Dom4j.readXml(XmlReader_Dom4j.java:24)
    at xml.XmlReader_Dom4j.main(XmlReader_Dom4j.java:11)
Nested exception:
java.net.MalformedURLException: unknown protocol: d
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.dom4j.io.SAXReader.read(SAXReader.java:321)
    at xml.XmlReader_Dom4j.readXml(XmlReader_Dom4j.java:24)
    at xml.XmlReader_Dom4j.main(XmlReader_Dom4j.java:11)
Nested exception: java.net.MalformedURLException: unknown protocol: d
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.dom4j.io.SAXReader.read(SAXReader.java:321)
    at xml.XmlReader_Dom4j.readXml(XmlReader_Dom4j.java:24)
    at xml.XmlReader_Dom4j.main(XmlReader_Dom4j.java:11)
success
success
success

 

Source code:

    /**
     * <p>
     * Reads a Document from the given URL or filename using SAX.
     * </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.
     */
    public Document read(String systemId) throws DocumentException {
        InputSource source = new InputSource(systemId);
        if (this.encoding != null) {
            source.setEncoding(this.encoding);
        }

        return read(source);
    }

 

eg2:

    private static void testWithUrl() throws MalformedURLException {
        System.out.println("=============testWithUrlBegin=============");

        String path = "file:///D:\\test\\中文文件夹名\\namespaces.xml";
        newUrl(path);
        readXml(path);

        path = "D:\\test\\中文文件夹名\\namespaces.xml";
        newUrl(path);

        System.out.println("=============testWithUrlEnd=============");
    }

    private static void newUrl(String path) throws MalformedURLException {
        try {
            new URL(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void readXml(String path) {
        SAXReader saxReader=new SAXReader();
        try {
            Document document=saxReader.read(path);
            System.out.println("document.hasContent():"+document.hasContent());
            System.out.println("success");
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

 

Output:

=============testWithUrlBegin=============
document.hasContent():true
success
java.net.MalformedURLException: unknown protocol: d
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at xml.XmlReader_Dom4j.newUrl(XmlReader_Dom4j.java:50)
    at xml.XmlReader_Dom4j.testWithUrl(XmlReader_Dom4j.java:43)
    at xml.XmlReader_Dom4j.main(XmlReader_Dom4j.java:13)
=============testWithUrlEnd=============

 

saxReader.read(xmlFile)不报错的原因:

    /**
     * <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.
     */
    public Document read(File file) throws DocumentException {
        try {
            /*
             * We cannot convert the file to an URL because if the filename
             * contains '#' characters, there will be problems with the URL in
             * the InputSource (because a URL like
             * http://myhost.com/index#anchor is treated the same as
             * http://myhost.com/index) Thanks to Christian Oetterli
             */
            InputSource source = new InputSource(new FileInputStream(file));
            if (this.encoding != null) {
                source.setEncoding(this.encoding);
            }
            String path = file.getAbsolutePath();

            if (path != null) {
                // Code taken from Ant FileUtils
                StringBuffer sb = new StringBuffer("file://");

                // add an extra slash for filesystems with drive-specifiers
                if (!path.startsWith(File.separator)) {
                    sb.append("/");
                }

                path = path.replace('\\', '/');
                sb.append(path);

                source.setSystemId(sb.toString());
            }

            return read(source);
        } catch (FileNotFoundException e) {
            throw new DocumentException(e.getMessage(), e);
        }
    }

 

java.net.URL.java中抛异常的位置:

    /**
     * Creates a <code>URL</code> object from the specified
     * <code>protocol</code>, <code>host</code>, <code>port</code>
     * number, <code>file</code>, and <code>handler</code>. Specifying
     * a <code>port</code> number of <code>-1</code> indicates that
     * the URL should use the default port for the protocol. Specifying
     * a <code>handler</code> of <code>null</code> indicates that the URL
     * should use a default stream handler for the protocol, as outlined
     * for:
     *     java.net.URL#URL(java.lang.String, java.lang.String, int,
     *                      java.lang.String)
     *
     * <p>If the handler is not null and there is a security manager,
     * the security manager's <code>checkPermission</code>
     * method is called with a
     * <code>NetPermission("specifyStreamHandler")</code> permission.
     * This may result in a SecurityException.
     *
     * No validation of the inputs is performed by this constructor.
     *
     * @param      protocol   the name of the protocol to use.
     * @param      host       the name of the host.
     * @param      port       the port number on the host.
     * @param      file       the file on the host
     * @param       handler    the stream handler for the URL.
     * @exception  MalformedURLException  if an unknown protocol is specified.
     * @exception  SecurityException
     *        if a security manager exists and its
     *        <code>checkPermission</code> method doesn't allow
     *        specifying a stream handler explicitly.
     * @see        java.lang.System#getProperty(java.lang.String)
     * @see        java.net.URL#setURLStreamHandlerFactory(
     *            java.net.URLStreamHandlerFactory)
     * @see        java.net.URLStreamHandler
     * @see        java.net.URLStreamHandlerFactory#createURLStreamHandler(
     *            java.lang.String)
     * @see        SecurityManager#checkPermission
     * @see        java.net.NetPermission
     */
    public URL(String protocol, String host, int port, String file,
           URLStreamHandler handler) throws MalformedURLException {
    if (handler != null) {
            SecurityManager sm = System.getSecurityManager();
            if (sm != null) {
                // check for permission to specify a handler
                checkSpecifyHandler(sm);
            }
        }

    protocol = protocol.toLowerCase();
        this.protocol = protocol;
     if (host != null) {

            /**
         * if host is a literal IPv6 address,
             * we will make it conform to RFC 2732
         */
            if (host != null && host.indexOf(':') >= 0
                    && !host.startsWith("[")) {
                host = "["+host+"]";
            }
            this.host = host;

        if (port < -1) {
        throw new MalformedURLException("Invalid port number :" +
                                                    port);
        }
            this.port = port;
        authority = (port == -1) ? host : host + ":" + port;
    }

    Parts parts = new Parts(file);
        path = parts.getPath();
        query = parts.getQuery();

        if (query != null) {
            this.file = path + "?" + query;
        } else {
            this.file = path;
        }
    ref = parts.getRef();    

    // Note: we don't do validation of the URL here. Too risky to change
        // right now, but worth considering for future reference. -br
        if (handler == null &&
            (handler = getURLStreamHandler(protocol)) == null) {
            throw new MalformedURLException("unknown protocol: " + protocol);
        }
        this.handler = handler;
    }

 

时间: 2025-01-30 07:02:49

Dom4j SAXReader Constructors的相关文章

java-XmlReader读取xml文件太慢是为什么,我用的是dom4j

问题描述 XmlReader读取xml文件太慢是为什么,我用的是dom4j SAXReader sax = new SAXReader(); Document xmlDoc = sax.read(new File("c://abc.xml")); 解决方案 跟你主机有关系的,问下你的空间商 解决方案二: DOM4J读取XML文件dom4j读取xml文件DOM4J读取XML文件 解决方案三: dom4j读取xml文件时,是读取所有树及节点的信息到内存中,再执行下一步的代码,所以会慢. 解

eclipse导入dom4j jar包,为什么不显示saxreader()方法?thanks in advance..

问题描述 eclipse导入dom4jjar包,为什么不显示saxreader()方法? 解决方案

JDOM真的比DOM4j要慢么?

dom  有一部分xml解析器使用者认为 JDOM 很慢,至少比起Dom4j来说效率不快.其实JDOM和DOM4J一样,同属优秀的开源XML解析器, 完全不必这样担心. 现在就实际拿一些实际使用的例子,作为简单的测试用例,对JDOM以及DOM4J最基本的文档解析功能来说明这个问题. JDOM测试用例如下:      public Document getDoc(String filename) throws IOException, JDOMException {        SAXBuild

Android创建与解析XML(五) Dom4j方式

1.Dom4j概述 dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the Java Collections Framework and with full support for DOM, SAX and JAXP. dom4j官方 网址:dom4j dom4j源码下载:dom4j download 本示例中,需要导入dom4

关于weblogic中使用Dom4j、Xerces导致执行线程挂起的问题

这两天有客户跟我说了个问题,说他们发现weblogic不停的load class,最后线程都挂在了Zip Entry操作上.让他们做了thread dump, 开始以为跟JDK的IO性能有关系,因为我曾经在HP\AIX上都碰到过线程挂起在zip操作上的问题,最终客户通过调整OS参数后,问题得到解决.但在拿到thread dump后, 发现问题不是他们说的那样,thread trace如下: "ExecuteThread: '6' for queue: 'Out.Thread Pool'"

浅谈Java开源XML工具包dom4j

dom4j为一个XML文档在内存中创建了一个树对象模型. 它提供了一组强大易用的API,通过XPath和XSLT来处理.操纵或者遍历XML文件,此外其中还集成了SAX.JAXP和DOM. 为了提供高度可配置的实现策略,dom4j基于接口设计.只需要提供一个DocumentFactory实现,您就可以创建您自己的XML树实现.这种设计,使得在扩展dom4j以定制您需要的特性时,能非常简单地重用dom4j的代码. 本文档将通过代码实例的方式为您提供一个dom4j的实践指南.在实验室项目中,这个开源工

使用Java解析XML文件(二) DOM4J篇

Dom4j是一个易用的.开源的库,用于XML,XPath和XSLT解析.它应用于Java平台,采用了Java集合框架并完全支持DOM,SAX和JAXP.官方网址:http://www.dom4j.org/. Dom4J在很多方面拥有更强大的功能,鼎鼎有名的Hibernate也使用它来做XML配置文件的解析. import java.io.FileWriter; import java.io.IOException; import org.dom4j.Document; import org.do

dom4j(Java code)

import java.io.File; import java.io.FileWriter; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute;import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat;import

在java中使用dom4j解析xml(示例代码)_java

虽然Java中已经有了Dom和Sax这两种标准解析方式 但其操作起来并不轻松,对于我这么一个初学者来说,其中部分代码是活生生的恶心 为此,伟大的第三方开发组开发出了Jdom和Dom4j等工具 鉴于目前的趋势,我们这里来讲讲Dom4j的基本用法,不涉及递归等复杂操作 Dom4j的用法很多,官网上的示例有那么点儿晦涩,这里就不写了 首先我们需要出创建一个xml文档,然后才能对其解析 xml文档: 复制代码 代码如下: <?xml version="1.0" encoding=&quo