在断网的情况下dom解析xml报错

今天因为项目需要,需要解析jetty配置文件jetty.xml。

因为是在无网的情况下做的程序,结果一来就报个错,百思不得其解。

错误信息如下:

java.net.UnknownHostException: jetty.mortbay.org
    at java.net.PlainSocketImpl.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$DTDDispatcher.dispatch(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 com.dir.MyDomXmlReader.<init>(MyDomXmlReader.java:46)
    at com.dir.SystemFileMove.moveSystemFile(SystemFileMove.java:18)
    at com.dir.run.main(run.java:8)
 

 

后来上google搜索,却没有找到什么解决办法。

后来几经折磨,终于判断为找不到dtd文件导致。

 

再次上网找寻解决办法如下,在方法中加入注释部分,即可。

其中的

String dtd_uri = "configure_1_2.xml";

为从xml描述里dtd uri 下载到的文件,存放在本地与类同一个目录下。

这样在断网的情况下运行,也不会报错了。

 

package com.dir;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class MyDomXmlReader {

	private List<List> _systemPath = new ArrayList<List>();

	public MyDomXmlReader() {

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			// 通过文档构建器工厂获取一个文档构建器
			DocumentBuilder db = dbf.newDocumentBuilder();

//			db.setEntityResolver(new EntityResolver() {
//				public InputSource resolveEntity(String publicId,
//						String systemId) throws SAXException, IOException {
//
//					String dtd_uri = "configure_1_2.xml";
//					InputStream dtd_stream = this.getClass()
//							.getResourceAsStream(dtd_uri);
//					return new InputSource(dtd_stream);
//
//				}
//			});

			File f = new File("D:/work/cqjyj/develope/etc/jetty.xml");

			// 通过文档通过文档构建器构建一个文档实例
			Document doc = db.parse(f);

			NodeList configureNodeList = doc.getElementsByTagName("Configure");
			Node configureNode;

			if (configureNodeList.getLength() == 1) {
				configureNode = configureNodeList.item(0);
			} else {
				throw new Exception();
			}

			NodeList nodeCallList = configureNode.getChildNodes();

			int nodeCallSize = nodeCallList.getLength();

			Node nodeCall;
			NodeList childsOfNodeCallList;
			Node childsOfNodeCall;
			int childsOfNodeCallSize;
			int sum = 0;
			List<String> twoPath;

			for (int i = 0; i < nodeCallSize; i++) {

				twoPath = new ArrayList<String>(2);
				nodeCall = nodeCallList.item(i);

				if ("Call".equals(nodeCall.getNodeName())) {
					if ("addWebApplication".equals(nodeCall.getAttributes()
							.getNamedItem("name").getNodeValue())) {

						childsOfNodeCallList = nodeCall.getChildNodes();
						childsOfNodeCallSize = childsOfNodeCallList.getLength();

						for (int j = 0; j < childsOfNodeCallSize; j++) {

							childsOfNodeCall = childsOfNodeCallList.item(j);

							if ("Arg".equals(childsOfNodeCall.getNodeName())) {
								twoPath.add(childsOfNodeCall.getTextContent());
							}
						}
						_systemPath.add(twoPath);
					}
				}

			}

		} catch (ParserConfigurationException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (SAXException ex) {
			ex.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<List> get_systemPath() {
		return _systemPath;
	}

	public void set_systemPath(List<List> systemPath) {
		_systemPath = systemPath;
	}
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值