Java中对XML⽂件的校验Java中使⽤dtd校验xml⽂件:
1.定义⼀个校验类CheckXML类:
import org.w3c.dom.Document;
l.sax.EntityResolver;
l.sax.ErrorHandler;
l.sax.InputSource;
l.sax.SAXException;
l.sax.SAXParseException;
import java.io.IOException;
import java.io.InputStream;
import java.URL;
l.parsers.DocumentBuilder;
l.parsers.DocumentBuilderFactory;
l.parsers.ParserConfigurationException;java xml是什么
public class CheckXML(){
public Doucment getCheckXML(){
List<studentUAO> result = new ArrayList<>();
// 使⽤Java的类加载器,查类路径下的资源⽂件。
// 1.获取任意的Class类型的对象,类名.class就可以返回⼀个Class对象。
Class<?> clazz = Teststudent.class;
// 2.调⽤getResourceAsStream(arg0)得到输⼊流。
// 从类路径的根⽬录查名为l的⽂件。
/
/ ⾃动在所有的类路径下⾯查名为l的⽂件,到第⼀个就马上返回,不会继续。URL url = Resource("./l");
//下⾯是输出l⽂件的根⽬录
System.out.println(url);
InputStream in = url.openStream();
// 1.创建DocumentBuilderFactory
DocumentBuilderFactory builderFactory = wInstance();
// 开启验证!
builderFactory.setValidating(true);
// 2.利⽤DocumentBuilderFactory创建DocumentBuilder
DocumentBuilder builder = wDocumentBuilder();
// 3.使⽤EntityResolver告诉XML解析器DTD⽂件在哪⾥!
// 同样在当前类的类路径下⾯,查student.dtd⽂件
builder.setEntityResolver(new EntityResolver(){
@Override
public InputSource resolveEntity(String arg0, String arg1) throws SAXException, IOException { InputStream dtdStream = ResourceAsStream("./student.dtd");
InputSource source = new InputSource(dtdStream);
return source;
}
});
// 4.为了能够得到准确的解析验证信息,必须提供⼀个ErrorHandler MyErrorHandler eh = new MyErrorHandler();
builder.setErrorHandler(eh);
if(eh.isSucceed()){
System.out.println("解析成功!");
// 5.执⾏解析
// 底层使⽤SAX⽅式读取XML内容,这样读取的性能⽐较好
Document doc = builder.parse(in);
return doc;
}else{
System.out.println("解析失败!");
return null;
}
}
static class MyErrorHandler implements ErrorHandler {
private Integer Errorcount = 0;
@Override
public void error(SAXParseException arg0) throws SAXException {
System.out.println("出现错误!");
arg0.printStackTrace(System.out);
Errorcount++; }
@Override
public void fatalError(SAXParseException arg0) throws SAXException { System.out.println("出现致命的错误!");
arg0.printStackTrace(System.out);
Errorcount++; }
@Override
public void warning(SAXParseException arg0) throws SAXException { System.out.println("出现警告!");
arg0.printStackTrace(System.out); }
//判断是否解析成功!
public boolean isSucceed(){ if(Errorcount == 0){ return true; } return false; } } }
1.2dom4j使⽤dtd校验xml:
// 1.创建SAXReader SAXReader reader = new SAXReader();
// 2.打开验证 reader.setValidation(true);
// 3.设置EntityResolver,在DOM4j⾥⾯,默认能够到相同⽬录下的dtd⽂件 /
/ 提供setEntityResolver的原因,主要是解决:如果XML⾥⾯声明的dtd⽂件和实际的dtd⽂件的名字不同的时候,能够正确到dtd reader.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
URL dtdURL = Resource("./students.dtd");
InputStream dtdStream = dtdURL.openStream();
InputSource source = new InputSource(dtdStream);
return source; } });
// 4.读取XML,⽣成Document对象
Document document = ad(url);
==============================================
Java使⽤schame校验xml⽂件:
2.1创建⼀个ValidationXM类:
import java.io.IOException;
import java.io.InputStream;
import java.URL;
l.XMLConstants;
l.transform.stream.StreamSource;
l.validation.Schema; l.validation.SchemaFactory;
l.validation.Validator;
l.sax.SAXException;
public class ValidationXML {
public static void getValidationXML() throws SAXException, IOException {
URL url = Resource("book.xsd");
/
/ 读取Schema⽂件
Schema s = wInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(url);
// 创建验证器
Validator v = s.newValidator();
// 对输⼊流进⾏验证
InputStream in = ResourceAsStream("/l");
StreamSource source = new StreamSource(in);
v.validate(source);
// 验证没有报错再去读取
//........................parse();
} }
3.使⽤dom4j:需要导两个包:dom4j.jar,jaxen-1.1-beta-6.jar。
import java.io.IOException;
import java.io.InputStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
l.sax.EntityResolver;
l.sax.InputSource;
l.sax.SAXException;
public class ValidatingDom4j {
public static void main(String[] args) throws DocumentException,
SAXException {
SAXReader reader = new SAXReader();
// 打开验证
reader.setValidation(true);
//验证指定的输⼊。
reader.setFeature("/xml/features/validation/schema", true);
// 到xsd⽂件的位置 reader.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { InputStream in = ValidatingDom4j.class .getResourceAsStream("book.xsd");
InputSource source = new InputSource(in);
return source; }
});
InputStream in = ResourceAsStream("l");
Document doc = ad(in); } }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论