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;
    }
时间: 2024-07-28 17:31:17

Dom4j SAXReader Constructors的相关文章

excel导入导出优化

对于上一篇excel中出现的问题,在excel导入导出中都做了优化.还是eclipse+jdk1.8,但是这个项目是一个web项目,需要配合Tomcat服务器,并且使用了SSH框架, I/O操作过多 首先,对于I/O操作过多,那么就不像之前一样,一条一条的添加或者更新;而且凑齐一堆,也就是一个list集合,然后统一的批量保存. 使用SessionFactory获取当前的session,然后调用session的persist方法,保存实体.只是设置了一个批量的量值.每到30条数据,就将缓存同步到数

How to Validate XML using Java

Configure Java APIs (SAX, DOM, dom4j, XOM) using JAXP 1.3 to validate XML Documents with DTD and Schema(s). Many Java XML APIs provide mechanisms to validate XML documents, the JAXP API can be used for most of these XML APIs but subtle configuration

小毛驴解析

XML解析  解析XML的步骤: * 1.创建SAXReader * 2.创建File对象来描述文件 * 3.使用SAXReader读取文件(解析的过程) * 并返回Document对象,其封装了整棵树 * 4.通过Document获取根元素(根标签) * 5.根据XML的结构获取不同节点预计对应的信息 XML解析方式 1. SAX解析方式 SAX(simple API for XML)是一种XML解析的替代方法.相比于DOM,SAX是一种速度更快,更有效的方法.它逐行扫描文档,一边扫描一边解析

Spring源码试读--BeanFactory模拟实现

动机 现在Springboot越来越便捷,如果简单的Spring应用,已无需再配置xml文件,基本可以实现全注解,即使是SpringCloud的那套东西,也都可以通过yaml配置完成.最近一年一直在用Springboot+JPA或者Springboot+MyBatis,基本上不用Spring和SpringMVC了,心血来潮想着趁国庆假期试着一点点实现一下Spring的基本功能(当然是会对照源码的,毕竟很多细节想不到,变量命名也会按照源码来),基本思路就是先按照Spring的类图试着自己写,争取实

java解析XML saxReader.read(xml) 错误:org.dom4j.DocumentException: no protocol

完整错误信息: org.dom4j.DocumentException: no protocol: <?xml version="1.0" encoding="utf-8" ?><smil><subtitle visible="1" fontfamily="宋体" size="36" color="#FFFFFF" bgcolor=""

dom4j API使用简介

功能简介 dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它.在IBM developerWorks上面可以找到一篇文章,对主流的Java XML API进行的性能.功能和易用性的评测,dom4j无论在那个方面都是非常出色的.如今你可以看到越来越多的Java软件都在使用dom4j来读写XML,特别值得

【收藏用】--切勿转载JAVA 使用Dom4j 解析XML

原帖地址 : http://blog.csdn.NET/yyywyr/article/details/38359049 解析XML的方式有很多,本文介绍使用dom4j解析xml. 1.环境准备 (1)下载dom4j-1.6.1.jar (2)下载junit-4.10.jar 2.温馨提示 解析XML过程是通过获取Document对象,然后继续获取各个节点以及属性等操作,因此获取Document对象是第一步,大体说来,有三种方式: (1)自己创建Document对象   Document docu

javaweb学习总结十三(dom4j方式对XML文档进行解析)

一:dom4j方式介绍 对于xml的解析总共有三种 1:jaxp方式,是sun公司开发的,分为sax方式和dom方式 2:jdom方式,后来其中部分人员参与开发dom4j 3:dom4j方式,是现在企业使用比较多的一种方式 二:代码案例 1:读取xml文件中某个节点 1):获取第二个学生的名字 1 @Test 2 // 1:获取第二个学生的名字 3 public void test01() throws DocumentException { 4 // 1:获取解析器 5 SAXReader r

dom4j

Dom4j是什么DD? dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它.在IBM developerWorks上面可以找到一篇文章,对主流的Java XML API进行的性能.功能和易用性的评测,dom4j无论在那个方面都是非常出色的.如今你可以看到越来越多的Java软件都在使用dom4j来读写XML