当前位置:   article > 正文

schema验证xml_使用 schema 验证 xml 文档

使用 schema 验证 xml 文档

两步:

一、写xsd文件(xsd写法参照点我学习http://www.w3school.com.cn/schema/

二、调用方法校验

贴我的代码:

demo.xsd

  1. <?xml version="1.0" encoding="GBK" ?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  3. <xs:element name="B2CReq">
  4. <xs:complexType>
  5. <xs:all>
  6. <xs:element name="merId">
  7. <xs:simpleType>
  8. <xs:restriction base="xs:string">
  9. <xs:minLength value="1"/>
  10. <xs:maxLength value="20"/>
  11. </xs:restriction>
  12. </xs:simpleType>
  13. </xs:element>
  14. <xs:element name="orderNo">
  15. <xs:simpleType>
  16. <xs:restriction base="xs:string">
  17. <xs:minLength value="1"/>
  18. <xs:maxLength value="30"/>
  19. </xs:restriction>
  20. </xs:simpleType>
  21. </xs:element>
  22. <xs:element name="orderAmt">
  23. <xs:simpleType>
  24. <xs:restriction base="xs:decimal">
  25. <xs:pattern value="[\d]{0,16}\.[\d]{2}"/>
  26. </xs:restriction>
  27. </xs:simpleType>
  28. </xs:element>
  29. <xs:element name="point">
  30. <xs:simpleType>
  31. <xs:restriction base="xs:integer">
  32. <xs:minInclusive value="0"/>
  33. <xs:maxInclusive value="99999999999999999999999999999"/>
  34. </xs:restriction>
  35. </xs:simpleType>
  36. </xs:element>
  37. <xs:element name="account">
  38. <xs:simpleType>
  39. <xs:restriction base="xs:string">
  40. <xs:minLength value="1"/>
  41. <xs:maxLength value="18"/>
  42. </xs:restriction>
  43. </xs:simpleType>
  44. </xs:element>
  45. <xs:element name="custName">
  46. <xs:simpleType>
  47. <xs:restriction base="xs:string">
  48. <xs:minLength value="1"/>
  49. <xs:maxLength value="50"/>
  50. </xs:restriction>
  51. </xs:simpleType>
  52. </xs:element>
  53. <xs:element name="certNo">
  54. <xs:simpleType>
  55. <xs:restriction base="xs:string">
  56. <xs:minLength value="1"/>
  57. <xs:maxLength value="50"/>
  58. </xs:restriction>
  59. </xs:simpleType>
  60. </xs:element>
  61. <xs:element name="orderDate">
  62. <xs:simpleType>
  63. <xs:restriction base="xs:string">
  64. <xs:pattern value="[1-9][\d]{3}(1[012]|0[1-9])([012][0-9]|3[01])([01][\d]|2[0-4])([0-5][\d]|60)([0-5][\d]|60)"/>
  65. </xs:restriction>
  66. </xs:simpleType>
  67. </xs:element>
  68. <xs:element name="curType">
  69. <xs:simpleType>
  70. <xs:restriction base="xs:string">
  71. <xs:enumeration value="CNY"/>
  72. </xs:restriction>
  73. </xs:simpleType>
  74. </xs:element>
  75. <xs:element name="notifyURL">
  76. <xs:simpleType>
  77. <xs:restriction base="xs:anyURI">
  78. <xs:minLength value="1"/>
  79. <xs:maxLength value="300"/>
  80. </xs:restriction>
  81. </xs:simpleType>
  82. </xs:element>
  83. <xs:element name="remark">
  84. <xs:simpleType>
  85. <xs:restriction base="xs:string">
  86. <xs:minLength value="1"/>
  87. <xs:maxLength value="100"/>
  88. </xs:restriction>
  89. </xs:simpleType>
  90. </xs:element>
  91. </xs:all>
  92. </xs:complexType>
  93. </xs:element>
  94. </xs:schema>

b.工具类

  1. package org.zhangh.util;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.File;
  4. import java.io.InputStream;
  5. import javax.xml.transform.Source;
  6. import javax.xml.transform.stream.StreamSource;
  7. import javax.xml.validation.Schema;
  8. import javax.xml.validation.SchemaFactory;
  9. import javax.xml.validation.Validator;
  10. import org.apache.log4j.Logger;
  11. import org.xml.sax.SAXException;
  12. /**
  13. * @desc xml验证
  14. * @author zhangh
  15. * @date 2013-5-13 上午11:12:55
  16. *
  17. */
  18. public class XmlValidateUtil {
  19. private static final Logger logger = Logger.getLogger(XmlValidateUtil.class);
  20. private static final String SCHEMALANG = "http://www.w3.org/2001/XMLSchema";
  21. /**
  22. * Schema校验xml文件
  23. * @param xmlPath xml字符串
  24. * @param xsdPath xsd文件路径
  25. * @return xml文件是否符合xsd定义的规则
  26. */
  27. public static boolean xmlStringValidate(String xmlStr, String xsdPath) {
  28. boolean flag = false;
  29. try {
  30. SchemaFactory factory = SchemaFactory.newInstance(SCHEMALANG);
  31. File schemaLocation = new File(xsdPath);
  32. Schema schema = factory.newSchema(schemaLocation);
  33. Validator validator = schema.newValidator();
  34. InputStream is = new ByteArrayInputStream(xmlStr.getBytes());
  35. Source source = new StreamSource(is);
  36. try {
  37. validator.validate(source);
  38. flag = true;
  39. } catch (SAXException ex) {
  40. logger.info(ex.getMessage());
  41. }
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. return flag;
  46. }
  47. /**
  48. * Schema校验xml文件
  49. * @param xmlPath xml文件路径
  50. * @param xsdPath xsd文件路径
  51. * @return xml文件是否符合xsd定义的规则
  52. */
  53. public static boolean xmlFileValidate(String xmlPath, String xsdPath) {
  54. boolean flag = false;
  55. try {
  56. SchemaFactory factory = SchemaFactory.newInstance(SCHEMALANG);
  57. File schemaLocation = new File(xsdPath);
  58. Schema schema = factory.newSchema(schemaLocation);
  59. Validator validator = schema.newValidator();
  60. Source source = new StreamSource(xmlPath);
  61. try {
  62. validator.validate(source);
  63. flag = true;
  64. } catch (SAXException ex) {
  65. logger.info(ex.getMessage());
  66. }
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. }
  70. return flag;
  71. }
  72. }

c调用

XmlValidateUtil.xmlStringValidate(tranData, xsdPath)


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/286863
推荐阅读
相关标签
  

闽ICP备14008679号