用过dom4j的朋友请进来 dom4j的api中有这个方法,名称不记得了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 呵呵 原来是Terry兄啊dom4j验证xml 很多方法 推荐做法是用sun的 MSV 来验证 可以支持DTD,RELAX等发个官方的demo给你///////////////////import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.io.SAXReader;import org.dom4j.io.SAXWriter;import org.iso_relax.verifier.Schema;import org.iso_relax.verifier.Verifier;import org.iso_relax.verifier.VerifierFactory;import org.iso_relax.verifier.VerifierHandler;import org.xml.sax.ErrorHandler;import org.xml.sax.SAXParseException;import com.sun.msv.verifier.jarv.TheFactoryImpl;/** * A sample program which validates an already existing dom4j Document using the * JARV API using Sun's MSV library implementation. * * @author <a href="mailto:[email protected]">James Strachan </a> * @version $Revision: 1.4 $ */public class JARVDemo1 { public static void main(String[] args) { new JARVDemo1().run(args); } public void run(String[] args) { try { if (args.length < 2) { System.out.println("usage: <xmlDoc> <schemaDoc>"); System.out .println("Which validates the given XML document against the given schema document"); System.out .println("The schema can be XML Schema, RelaxNG, Relax or TREX"); return; } String xmlFile = args[0]; String schema = args[1]; SAXReader reader = new SAXReader(); Document document = reader.read(xmlFile); process(document, schema); } catch (DocumentException e) { System.out.println("Exception occurred: " + e); Throwable nestedException = e.getNestedException(); if (nestedException != null) { System.out.println("NestedException: " + nestedException); nestedException.printStackTrace(); } else { e.printStackTrace(); } } catch (Throwable t) { System.out.println("Exception occurred: " + t); t.printStackTrace(); } } /** Validate document using MSV */ protected void process(Document document, String schemaURI) throws Exception { System.out.println("Loaded schema document: " + schemaURI); // use autodetection of schemas VerifierFactory factory = new TheFactoryImpl(); Schema schema = factory.compileSchema(schemaURI); Verifier verifier = schema.newVerifier(); verifier.setErrorHandler(new ErrorHandler() { public void error(SAXParseException e) { System.out.println("ERROR: " + e); } public void fatalError(SAXParseException e) { System.out.println("FATAL: " + e); } public void warning(SAXParseException e) { System.out.println("WARNING: " + e); } }); System.out.println("Validating XML document"); VerifierHandler handler = verifier.getVerifierHandler(); SAXWriter writer = new SAXWriter(handler); writer.write(document); System.out.println(handler.isValid()); }}////////////////// 关于java swing james中的fetchmail问题 有正确答案马上结贴:一个关于jsp中图片处理的问题? Cannot create PoolableConnectionFactory错误 java 的问题 j2sdk1.4.2配置问题,问题解决马上结贴 求Eclipse+easy struts开发步骤 WebLogic不能编译JSP文件? resin 支持struts吗?如果是,jbuilder9+tomcat做的war文件直接拷到resin目录下,行吗 Java 怎样查看JSP引擎生成的Java代码? 安装---Jforum--中文提交出现问号“?”乱码。
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXWriter;
import org.iso_relax.verifier.Schema;
import org.iso_relax.verifier.Verifier;
import org.iso_relax.verifier.VerifierFactory;
import org.iso_relax.verifier.VerifierHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;import com.sun.msv.verifier.jarv.TheFactoryImpl;/**
* A sample program which validates an already existing dom4j Document using the
* JARV API using Sun's MSV library implementation.
*
* @author <a href="mailto:[email protected]">James Strachan </a>
* @version $Revision: 1.4 $
*/
public class JARVDemo1 { public static void main(String[] args) {
new JARVDemo1().run(args);
} public void run(String[] args) {
try {
if (args.length < 2) {
System.out.println("usage: <xmlDoc> <schemaDoc>");
System.out
.println("Which validates the given XML document against the given schema document");
System.out
.println("The schema can be XML Schema, RelaxNG, Relax or TREX");
return;
}
String xmlFile = args[0];
String schema = args[1]; SAXReader reader = new SAXReader();
Document document = reader.read(xmlFile);
process(document, schema);
} catch (DocumentException e) {
System.out.println("Exception occurred: " + e);
Throwable nestedException = e.getNestedException();
if (nestedException != null) {
System.out.println("NestedException: " + nestedException);
nestedException.printStackTrace();
} else {
e.printStackTrace();
}
} catch (Throwable t) {
System.out.println("Exception occurred: " + t);
t.printStackTrace();
}
} /** Validate document using MSV */
protected void process(Document document, String schemaURI)
throws Exception { System.out.println("Loaded schema document: " + schemaURI); // use autodetection of schemas
VerifierFactory factory = new TheFactoryImpl();
Schema schema = factory.compileSchema(schemaURI); Verifier verifier = schema.newVerifier();
verifier.setErrorHandler(new ErrorHandler() {
public void error(SAXParseException e) {
System.out.println("ERROR: " + e);
} public void fatalError(SAXParseException e) {
System.out.println("FATAL: " + e);
} public void warning(SAXParseException e) {
System.out.println("WARNING: " + e);
}
}); System.out.println("Validating XML document"); VerifierHandler handler = verifier.getVerifierHandler();
SAXWriter writer = new SAXWriter(handler);
writer.write(document);
System.out.println(handler.isValid());
}
}//////////////////