xml 文件SCHAMA 校验

来源:互联网 发布:财税知识网络答题竞赛 编辑:程序博客网 时间:2024/05/14 04:58

 在工作中用到接收别的系统发送的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();
  }