xml验证二----java根据xsd验证xml_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > xml验证二----java根据xsd验证xml

xml验证二----java根据xsd验证xml

 2013/11/7 12:11:03  no_bao  程序员俱乐部  我要评论(0)
  • 摘要:xml转换xsd参考http://51876.iteye.com/blog/1972405下面是根据转换后的xsd使用java代码验证xml的合法性importjava.io.BufferedReader;importjava.io.File;importjava.io.FileReader;importjava.io.IOException;importjava.io.Reader;importjava.io.StringReader;importjavax.xml.transform
  • 标签:Java XML

?

xml 转换 xsd 参考

http://51876.iteye.com/blog/1972405

?

下面是根据转换后的xsd 使用java代码验证xml的合法性

import java.io.BufferedReader;?
import java.io.File;?
import java.io.FileReader;?
import java.io.IOException;?
import java.io.Reader;?
import java.io.StringReader;?
?
import javax.xml.transform.Source;?
import javax.xml.transform.stream.StreamSource;?
import javax.xml.validation.Schema;?
import javax.xml.validation.SchemaFactory;?
import javax.xml.validation.Validator;?
?
import org.xml.sax.SAXException;?

?

?

public class XmlValidator {?
?
??? public final static String SCHEMA_LANGUAGE = "http://www.w3.org/2001/XMLSchema";?
?
??? /**
???? * @param xmlStr
???? * @param xsdStr
???? * @return XmlValidateResult 通过Schema验证指定的xml字符串是否符合结构
???? */?
??? public static ReturnBean validateByXsd(String xmlStr, String xsdStr) {?
??????? // 查找支持指定模式语言的 SchemaFactory 的实现并返回它?
??????? SchemaFactory factory = SchemaFactory?
??????????????? .newInstance(XmlValidator.SCHEMA_LANGUAGE);?
??????? // 包装待验证的xml字符串为Reader?
??????? Reader xmlReader = new BufferedReader(new StringReader(xmlStr));?
??????? // 保障Schema xsd字符串为Reader?
??????? Reader xsdReader = new BufferedReader(new StringReader(xsdStr));?
??????? // 创建返回值类,默认为失败?
??????? ReturnBean vs = new ReturnBean();?
?
??????? try {?
??????????? // 构造Schema Source?
??????????? Source xsdSource = new StreamSource(xsdReader);?
??????????? // 解析作为模式的指定源并以模式形式返回它?
??????????? Schema schema = factory.newSchema(xsdSource);?
?
??????????? // 根据Schema检查xml文档的处理器,创建此 Schema 的新 Validator?
??????????? Validator validator = schema.newValidator();?
?
??????????? // 构造待验证xml Source?
??????????? Source xmlSource = new StreamSource(xmlReader);?
?
??????????? // 执行验证?
??????????? validator.validate(xmlSource);?
??????????? // 设置验证通过?
??????????? vs.setResult(ReturnUtil.ReturnTrue);?
??????????? return vs;?
??????? } catch (SAXException ex) {?
??????????? // 设置验证失败?
??????????? vs.setResult(ReturnUtil.ReturnFalse);?
??????????? // 设置验证失败信息?
??????????? vs.setDescription(ex.getMessage());?
??????????? return vs;?
??????? } catch (IOException e) {?
??????????? // 设置验证失败?
??????????? vs.setResult(ReturnUtil.ReturnFalse);?
??????????? // 设置验证失败信息?
??????????? vs.setDescription(e.getMessage());?
??????????? return vs;?
??????? }?
??? }?
?
??? /**
???? * @param args0
???? * @throws IOException
???? */?
??? public static void main(String[] args0) throws IOException {?
??????? File xml = new File("F:/mail.xml");?
??????? File xsd = new File("F:/mail.xsd");?
?
??????? BufferedReader xmlBuff = new BufferedReader(new FileReader(xml));?
??????? BufferedReader xsdBuff = new BufferedReader(new FileReader(xsd));?
?
??????? StringBuffer xmlStr = new StringBuffer();?
??????? String line = null;?
??????? while ((line = xmlBuff.readLine()) != null) {?
??????????? xmlStr.append(line);?
??????????? xmlStr.append("\n");?
??????? }?
??????? xmlBuff.close();?
?
??????? StringBuffer xsdStr = new StringBuffer();?
??????? line = null;?
??????? while ((line = xsdBuff.readLine()) != null) {?
??????????? xsdStr.append(line);?
??????? }?
??????? xsdBuff.close();?
?
??????? ReturnBean vs = XmlValidator.validateByXsd(xmlStr.toString(),?
??????????????? xsdStr.toString());?
??????? if (ReturnUtil.ReturnTrue.equals(vs.getResult())) {?
??????????? System.out.println("验证成功!");?
??????? } else {?
??????????? System.out.println("验证失败!");?
??????????? System.out.println("失败原因:" + vs.getDescription());?
//??????????? System.out.println("验证xml:\n" + xmlStr.toString());?
??????? }?
?
??? }?
}

发表评论
用户名: 匿名