加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

schema验证xml

发布时间:2020-12-16 06:02:59 所属栏目:百科 来源:网络整理
导读:两步: 一、写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:comple

两步:

一、写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>

b.工具类
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;
	}
	
}

c调用
XmlValidateUtil.xmlStringValidate(tranData,xsdPath)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读