`
woshizn
  • 浏览: 207118 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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

    博客分类:
  • Java
阅读更多

今天因为项目需要,需要解析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;
	}
}
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics