“ java.net.UnknownHostException”读取mybatis xml文件时发生 [英] "java.net.UnknownHostException" occur when read mybatis xml file

查看:689
本文介绍了“ java.net.UnknownHostException”读取mybatis xml文件时发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢通过Java代码从以下mybatis xml文件读取数据。 (不是mybatis映射,只想从应用程序外部读取数据形式。)

I like to read data from following mybatis xml file by java code. (Not mybatis mapping, just want to read data form outside application.)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.raistudies.persistence.UserService">

<resultMap id="result" type="user">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="standard" column="standard"/>
    <result property="age" column="age"/>
    <result property="sex" column="sex"/>
</resultMap>

<select id="getAllUser" resultMap="result">  
    SELECT id,name,standard,age,sex 
    FROM user; 
</select>
</mapper>

所以我尝试使用xml阅读器,如下所示:

So i trying with xml reader as follow:

        File fXmlFile = new File("c:\\mybatis.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("mapper");
                    .
                    .
                    .

但是当我运行此代码时,出现以下错误

But when i run this code, i got following error

java.net.UnknownHostException: mybatis.org
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDTDEntity(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(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.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at xmlReader.main(xmlReader.java:37)

我发现了

     <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

当我删除此语句时,我的所有代码均正常工作。
让我知道,我该如何解决这个问题。

My all code are work properly when i remove this statement. Let me know, how can i overcome this problem.

推荐答案

通过在DocumentBuilderFactory上设置以下功能可以:

It's OK by setting following features on the DocumentBuilderFactory:

    dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);






例如:


eg:

    File fXmlFile = new File("c:\\mybatis.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory
            .newInstance();

    dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();

这篇关于“ java.net.UnknownHostException”读取mybatis xml文件时发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆