您的位置:首页 > 编程语言 > Java开发

Xsd文件验证xml的java实现

2009-04-09 09:28 381 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息