org.dom4j.DocumentException: www.apple.com Nested exception: www.apple.com


SAXReader offline


package com.akwolf.xml;

import java.io.IOException;
import java.io.Serializable;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * 在不能连接外网情况下对指定的文档实体进行解析
 * 
 * 文件名 : OfflineEntityResover.java
 * 
 * @version : 1.0
 * @author : zhangh
 * @email : zhangh@justsy.com
 * @create date : 2012-11-22
 */
public class OfflineEntityResover implements EntityResolver, Serializable {
	private static final long serialVersionUID = 1L;
	protected String uriPrefix;
	public OfflineEntityResover(String systemId) {
		this.uriPrefix = getUriPrefix(systemId);
	}
	protected String getUriPrefix(String systemId) {
		String prefix = null;
		if ((systemId != null) && (systemId.length() > 0)) {
			int idx = systemId.lastIndexOf('/');
			if (idx > 0) {
				prefix = systemId.substring(0, idx + 1);
			}
		}
		return prefix;
	}

	@Override
	public InputSource resolveEntity(String publicId, String systemId)
			throws SAXException, IOException {
		if ((systemId != null) && (systemId.length() > 0)) {
			if ((uriPrefix != null) && (systemId.indexOf(':') <= 0)) {
				systemId = uriPrefix + systemId;
			}
		}
		if (systemId.equals("http://www.apple.com/DTDs/PropertyList-1.0.dtd")) {
			return new InputSource(OfflineEntityResover.class.getClassLoader()
					.getResourceAsStream("dtd/PropertyList-1.0.dtd"));
		} else {
			return new InputSource(systemId);
		}
	}

}


package com.akwolf.xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;

/**
 * 
 * 
 * 文件名 : ValidateXml.java
 * 
 * @version : 1.0
 * @author : zhangh
 * @email : zhangh@justsy.com
 * @create date : 2012-11-22
 */
public class ValidateXml {

	public static void main(String[] args) throws DocumentException {
		 Document doc = new ValidateXml().loadXml() ;
		 System.out.println(doc.getRootElement().getName());
	}
	

	public Document loadXml() throws DocumentException {
		InputSource input = new InputSource(this.getClass().getClassLoader().getResourceAsStream("angryInfo.plist")) ;		
		SAXReader saxReader = new SAXReader();		
		saxReader.setEntityResolver(new OfflineEntityResover(input.getSystemId())) ;
		Document doc = saxReader.read(input);
		return doc;
	}
}


<!ENTITY % plistObject "(array | data | date | dict | real | integer | string | true | false )" >
<!ELEMENT plist %plistObject;>
<!ATTLIST plist version CDATA "1.0" >

<!-- Collections -->
<!ELEMENT array (%plistObject;)*>
<!ELEMENT dict (key, %plistObject;)*>
<!ELEMENT key (#PCDATA)>

<!--- Primitive types -->
<!ELEMENT string (#PCDATA)>
<!ELEMENT data (#PCDATA)> <!-- Contents interpreted as Base-64 encoded -->
<!ELEMENT date (#PCDATA)> <!-- Contents should conform to a subset of ISO 8601 (in particular, YYYY '-' MM '-' DD 'T' HH ':' MM ':' SS 'Z'.  Smaller units may be omitted with a loss of precision) -->

<!-- Numerical primitives -->
<!ELEMENT true EMPTY>  <!-- Boolean constant true -->
<!ELEMENT false EMPTY> <!-- Boolean constant false -->
<!ELEMENT real (#PCDATA)> <!-- Contents should represent a floating point number matching ("+" | "-")? d+ ("."d*)? ("E" ("+" | "-") d+)? where d is a digit 0-9.  -->
<!ELEMENT integer (#PCDATA)> <!-- Contents should represent a (possibly signed) integer number in base 10 -->




http://stackoverflow.com/questions/8831989/how-to-use-dom4j-saxreader-offline

http://www.cnblogs.com/deepnighttwo/archive/2011/06/12/2078829.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值