xml文件SCHAMA校验


在工作中用到接收别的系统发送的XML到DRP中生成销售单出库时.要校验对方发过来的XML格式是否正确.
在工作中用到接收别的系统发送的XML到DRP中生成销售单出库时.要校验对方发过来的XML格式是否正确.

1.定义SCHAMA文件buildSalebill-validator.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:simpleType name="saledatetype">
  <xs:restriction base="xs:string">
   <xs:pattern value="(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})"/>
  </xs:restriction>
 </xs:simpleType>
 <xs:simpleType name="notnulltype">
  <xs:restriction base="xs:string">
   <xs:pattern value=".{1,50}"/>
  </xs:restriction>
 </xs:simpleType>
 <xs:simpleType name="ntype">
  <xs:restriction base="xs:string">
   <xs:pattern value="[\S]{1,200}"/>
  </xs:restriction>
 </xs:simpleType>
 <xs:simpleType name="remarktype">
  <xs:restriction base="xs:string">
   <xs:pattern value=".{0,200}"/>
  </xs:restriction>
 </xs:simpleType>
 <xs:simpleType name="groupNametype">
  <xs:restriction base="xs:string">
   <xs:pattern value="[\S]{0,200}"/>
  </xs:restriction>
 </xs:simpleType>
 <xs:simpleType name="groupIdtype">
  <xs:restriction base="xs:string">
   <xs:pattern value="[A-Za-z0-9]{0,50}"/>
  </xs:restriction>
 </xs:simpleType>
 <xs:simpleType name="ordertype">
  <xs:restriction base="xs:string">
   <xs:pattern value="[A-Za-z0-9]{10,30}"/>
  </xs:restriction>
 </xs:simpleType>
 <xs:simpleType name="phonetype">
  <xs:restriction base="xs:string">
   <xs:pattern value="[1][0-9]{10}"/>
  </xs:restriction>
 </xs:simpleType>
 <xs:simpleType name="imeitype">
  <xs:restriction base="xs:string">
   <xs:pattern value="[0-9/,/]{0,200}"/>
  </xs:restriction>
 </xs:simpleType>
 <xs:simpleType name="haspgtype">
  <xs:restriction base="xs:int">
   <xs:enumeration value="1"/>
   <xs:enumeration value="0"/>
  </xs:restriction>
 </xs:simpleType>
 <xs:element name="Order">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="SmSaleBill">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="OrderNo" type="ordertype"/>
       <xs:element name="SaleDate" type="saledatetype"/>
       <xs:element name="DeptNo" type="ntype"/>
       <xs:element name="SalesHandlerNum" type="ntype"/>
       <xs:element name="SaleClientName" type="notnulltype"/>
       <xs:element name="MobilePhone" type="phonetype"/>
       <xs:element name="HasCombine" type="haspgtype"/>
       <xs:element name="Remark" type="remarktype"/>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
    <xs:element name="SmSaleItemDetails">
     <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
       <xs:element name="row">
        <xs:complexType>
         <xs:sequence>
          <xs:element name="Imei" type="imeitype"/>
          <xs:element name="CrmItemName" type="notnulltype"/>
          <xs:element name="ItemNo" type="ntype"/>
          <xs:element name="Price" type="xs:decimal"/>
          <xs:element name="GroupPrice" type="xs:decimal"/>
          <xs:element name="MiniRetailPrice" type="xs:decimal"/>
          <xs:element name="SalePrice" type="xs:decimal"/>
          <xs:element name="num" type="xs:positiveInteger"/>
         </xs:sequence>
         <xs:attribute name="groupId" type="groupIdtype" use="required"/>
         <xs:attribute name="groupNo" type="xs:integer" use="required"/>
         <xs:attribute name="groupName" type="groupNametype" use="required"/>
        </xs:complexType>
       </xs:element>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>

2.与接收的XML校验:

String xmlfile = xml.substring(s, e + 8); // 取格式完整的XML文件  
  ByteArrayInputStream sb = new ByteArrayInputStream(xmlfile.getBytes());
  ByteArrayInputStream validatsb = new ByteArrayInputStream(xmlfile.getBytes());
 
  try {
   String s_xmlpath = "com/daditech/webservice/util/buildSalebill-validator.xsd";
   ClassLoader classLoader = CrmWebServiceClient.class.getClassLoader();
   java.net.URL urlxsd = classLoader.getResource(s_xmlpath);
   SchemaFactory schemafactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
   Schema schemadoc = schemafactory.newSchema(urlxsd);
   Validator v = schemadoc.newValidator();
   v.validate(new StreamSource(validatsb));
  } catch (Exception ex) {
   returnStr.append("<status>0</status>");
   returnStr.append("<message>xml文件格式不合法:"+ex.getMessage().replaceAll("cvc-pattern-valid:", "")+"</message></Return>");
   updatLog("失败","文件格式不合法:"+ex.getMessage().replaceAll("cvc-pattern-valid:", ""),tobillno,xml);
   return returnStr.toString();
  }
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值