Xsd文件验证xml的java实现

xsd文件验证xml的java实现

今天在修改数据交换平台时候,看见其中使用此技术


import java.io.File; import java.io.IOException;

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.xml.sax.SAXException;

public class ValidateXML {

/**
 * 
 */
public ValidateXML(){
 
}

public boolean Validatexml(String xsdpath,String xmlpath) throws SAXException,IOException{
   //建立schema工厂
   SchemaFactory schemaFactory=SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
   //建立验证文档文件对象,利用此文件对象所封装的文件进行schema验证
   File schemaFile=new File(xsdpath);
   //利用schema工厂,接收验证文档文件对象生成Schema对象
   Schema schema=schemaFactory.newSchema(schemaFile);
   //通过Schema产生针对于此Schema的验证器,利用schenaFile进行验证
   Validator validator=schema.newValidator();
   //得到验证的数据源
   Source source=new StreamSource(xmlpath);
   //开始验证,成功输出success!!!,失败输出fail
   try{
    
    validator.validate(source); 
    
   }catch(Exception ex){
    
    ex.printStackTrace(); 
    
   }
         return true;
 }

}


XML文件

<?xml version="1.0" encoding="gbk"?> <Test id="F2009045002100N03030100"> <NewData > <deviceTypeId>16135</deviceTypeId> <iconName>变电站.gif</iconName> </NewData> </Test>

验证XSD文件

<?xml version="1.0" encoding="gbk"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:element name="Test">
       <xs:complexType>
           <xs:choice maxOccurs="unbounded" minOccurs="0">
               <xs:element name="NewData">
                   <xs:complexType>
                       <xs:complexContent>
                           <xs:extension base="dataElementType">
                               <xs:attributeGroup ref="dataRequiredAttributeType"/>
                           </xs:extension>
                       </xs:complexContent>
                   </xs:complexType>
               </xs:element>
               <xs:element name="ModifyData">
                   <xs:complexType>
                       <xs:complexContent>
                           <xs:extension base="dataElementType">
                               <xs:attributeGroup ref="dataRequiredAttributeType"/>
                           </xs:extension>
                       </xs:complexContent>
                   </xs:complexType>
               </xs:element>
               <xs:element name="DeleteData">
                   <xs:complexType>
                       <xs:complexContent>
                           <xs:extension base="dataElementType">
                               <xs:attributeGroup ref="dataRequiredAttributeType"/>
                           </xs:extension>
                       </xs:complexContent>
                   </xs:complexType>
               </xs:element>             
           </xs:choice>
           <xs:attribute name="id" type="stringRequireType24" use="required"/>
       </xs:complexType>
   </xs:element>
   
   
   <xs:attributeGroup name="dataRequiredAttributeType">
   </xs:attributeGroup>
   
   <xs:complexType name="dataElementType">
       <xs:all>
          <xs:element name="deviceTypeId" type="stringType10" minOccurs="0"/>
          <xs:element name="iconName" type="stringType100" minOccurs="0"/>  
       </xs:all>
   </xs:complexType>
   
   
   
   <xs:simpleType name="stringType10">
       <xs:restriction base="xs:string">
           <xs:pattern value="([0-9])*"/>
           <xs:minLength value="0"/>
           <xs:maxLength value="10"/>
       </xs:restriction>
   </xs:simpleType>
   
   <xs:simpleType name="stringType100">
       <xs:restriction base="xs:string">
           <xs:minLength value="0"/>
           <xs:maxLength value="100"/>
       </xs:restriction>
   </xs:simpleType>
   
   <xs:simpleType name="stringRequireType24">
       <xs:restriction base="xs:string">
           <xs:minLength value="1"/>
           <xs:maxLength value="24"/>
       </xs:restriction>
   </xs:simpleType>
   
   <xs:simpleType name="stringRequireType50">
       <xs:restriction base="xs:string">
           <xs:minLength value="1"/>
           <xs:maxLength value="50"/>
       </xs:restriction>
   </xs:simpleType>
       
   

</xs:schema>

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值