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>
本文介绍了一种使用Java代码结合XSD模式文件验证XML文档的方法。通过具体示例展示了如何创建Schema对象并利用它来验证XML的有效性。
1141

被折叠的 条评论
为什么被折叠?



