赞
踩
两步:
一、写xsd文件(xsd写法参照点我学习http://www.w3school.com.cn/schema/)
二、调用方法校验
贴我的代码:
demo.xsd
- <?xml version="1.0" encoding="GBK" ?>
- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
-
- <xs:element name="B2CReq">
- <xs:complexType>
- <xs:all>
-
- <xs:element name="merId">
- <xs:simpleType>
- <xs:restriction base="xs:string">
- <xs:minLength value="1"/>
- <xs:maxLength value="20"/>
- </xs:restriction>
- </xs:simpleType>
- </xs:element>
-
- <xs:element name="orderNo">
- <xs:simpleType>
- <xs:restriction base="xs:string">
- <xs:minLength value="1"/>
- <xs:maxLength value="30"/>
- </xs:restriction>
- </xs:simpleType>
- </xs:element>
-
- <xs:element name="orderAmt">
- <xs:simpleType>
- <xs:restriction base="xs:decimal">
- <xs:pattern value="[\d]{0,16}\.[\d]{2}"/>
- </xs:restriction>
- </xs:simpleType>
- </xs:element>
-
- <xs:element name="point">
- <xs:simpleType>
- <xs:restriction base="xs:integer">
- <xs:minInclusive value="0"/>
- <xs:maxInclusive value="99999999999999999999999999999"/>
- </xs:restriction>
- </xs:simpleType>
- </xs:element>
-
- <xs:element name="account">
- <xs:simpleType>
- <xs:restriction base="xs:string">
- <xs:minLength value="1"/>
- <xs:maxLength value="18"/>
- </xs:restriction>
- </xs:simpleType>
- </xs:element>
-
- <xs:element name="custName">
- <xs:simpleType>
- <xs:restriction base="xs:string">
- <xs:minLength value="1"/>
- <xs:maxLength value="50"/>
- </xs:restriction>
- </xs:simpleType>
- </xs:element>
-
- <xs:element name="certNo">
- <xs:simpleType>
- <xs:restriction base="xs:string">
- <xs:minLength value="1"/>
- <xs:maxLength value="50"/>
- </xs:restriction>
- </xs:simpleType>
- </xs:element>
-
- <xs:element name="orderDate">
- <xs:simpleType>
- <xs:restriction base="xs:string">
- <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)"/>
- </xs:restriction>
- </xs:simpleType>
- </xs:element>
-
- <xs:element name="curType">
- <xs:simpleType>
- <xs:restriction base="xs:string">
- <xs:enumeration value="CNY"/>
- </xs:restriction>
- </xs:simpleType>
- </xs:element>
-
- <xs:element name="notifyURL">
- <xs:simpleType>
- <xs:restriction base="xs:anyURI">
- <xs:minLength value="1"/>
- <xs:maxLength value="300"/>
- </xs:restriction>
- </xs:simpleType>
- </xs:element>
-
- <xs:element name="remark">
- <xs:simpleType>
- <xs:restriction base="xs:string">
- <xs:minLength value="1"/>
- <xs:maxLength value="100"/>
- </xs:restriction>
- </xs:simpleType>
- </xs:element>
-
- </xs:all>
- </xs:complexType>
- </xs:element>
-
- </xs:schema>
-
- package org.zhangh.util;
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.InputStream;
-
- 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.apache.log4j.Logger;
- import org.xml.sax.SAXException;
-
- /**
- * @desc xml验证
- * @author zhangh
- * @date 2013-5-13 上午11:12:55
- *
- */
- public class XmlValidateUtil {
-
- private static final Logger logger = Logger.getLogger(XmlValidateUtil.class);
-
- private static final String SCHEMALANG = "http://www.w3.org/2001/XMLSchema";
-
- /**
- * Schema校验xml文件
- * @param xmlPath xml字符串
- * @param xsdPath xsd文件路径
- * @return xml文件是否符合xsd定义的规则
- */
- public static boolean xmlStringValidate(String xmlStr, String xsdPath) {
- boolean flag = false;
- try {
- SchemaFactory factory = SchemaFactory.newInstance(SCHEMALANG);
- File schemaLocation = new File(xsdPath);
- Schema schema = factory.newSchema(schemaLocation);
- Validator validator = schema.newValidator();
- InputStream is = new ByteArrayInputStream(xmlStr.getBytes());
- Source source = new StreamSource(is);
- try {
- validator.validate(source);
- flag = true;
- } catch (SAXException ex) {
- logger.info(ex.getMessage());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return flag;
- }
-
- /**
- * Schema校验xml文件
- * @param xmlPath xml文件路径
- * @param xsdPath xsd文件路径
- * @return xml文件是否符合xsd定义的规则
- */
- public static boolean xmlFileValidate(String xmlPath, String xsdPath) {
- boolean flag = false;
- try {
- SchemaFactory factory = SchemaFactory.newInstance(SCHEMALANG);
- File schemaLocation = new File(xsdPath);
- Schema schema = factory.newSchema(schemaLocation);
- Validator validator = schema.newValidator();
- Source source = new StreamSource(xmlPath);
-
- try {
- validator.validate(source);
- flag = true;
- } catch (SAXException ex) {
- logger.info(ex.getMessage());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return flag;
- }
-
- }
XmlValidateUtil.xmlStringValidate(tranData, xsdPath)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。