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