Schema 校验

Java
package com.steven.hp;

/**
 * 用schema来验证
 * 1, 保证 xml的schemaLocation值 和 xsd中的xmlns一致
 * 2, 继承 DefaultHandler, 但不需要实现其startElement等方法
 * 
 * 在eclipse中 配置spring 自定义的schema文件
 * http://jimichan.iteye.com/blog/363742
 * XML Catalog
 */
public class TypeLister extends DefaultHandler {

	public static void main(String[] args) throws SAXException, IOException {
		TypeLister lister = new TypeLister();
		
		String fileName = "hp/hp.xml";
		String refXSD = "hp/hp.xsd";
		
		lister.doParsing(fileName, refXSD);
	}
	
	private TypeInfoProvider provider;
	
	// set provider.
	private void setProvider(TypeInfoProvider provider) {
		this.provider = provider;
	}
	
	// method for file validation and parsing.
	public void doParsing(String fileName, String refXSD) throws SAXException,
			IOException {

		SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
		File schemaLocation = new File(refXSD);
		Schema schema = factory.newSchema(schemaLocation);

		ValidatorHandler vHandler = schema.newValidatorHandler();
		TypeInfoProvider provider = vHandler.getTypeInfoProvider();
		this.setProvider(provider);
		ContentHandler cHandler = this;

		vHandler.setContentHandler(cHandler);

		XMLReader parser = XMLReaderFactory.createXMLReader();
		parser.setContentHandler(vHandler);

		FileInputStream input = null;
		try {
			input = new FileInputStream(fileName);
			parser.parse(new InputSource(input));
		} catch (SAXParseException ex) {
			throw ex;
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (Exception e) {
				}
			}
		}
		
		System.out.println("---验证成功---");
	}
}


xml
<?xml version="1.0" encoding="UTF-8"?>
<ConfigMessage xsi:schemaLocation="http://schemas.hp.com/CfM/ucmdb/ hp.xsd" xmlns="http://schemas.hp.com/CfM/ucmdb/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ConfigMessageHeader>
        <cmdbContext>ES Adapter</cmdbContext>
        <dataSource>BMC</dataSource>
        <dataSourceContact>Hetal Joshi</dataSourceContact>
        <dataSourceContactEmail>[email protected]</dataSourceContactEmail>
        <simulate>false</simulate>
    </ConfigMessageHeader>
    <CIsAndRelations>
        <CIsForUpdate>
              <CI>
                <unix>
                <data_externalid>U1-ECF35C62870C11DFBD30000083EFE</data_externalid>
                <data_note>Import file #1 - original BMC data</data_note>
                <data_origin>BMC</data_origin>
                <name>ghthlp112.bmc.extract</name>
                <host_key>ghthlp112.bmc.extract</host_key>
                <os_family>unix</os_family>
                <unix_description>xxxx</unix_description>
                <markasdelete>true</markasdelete>
                </unix>
              </CI></CIsForUpdate>
        <relationsForUpdate/>
    </CIsAndRelations>
</ConfigMessage>



xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://schemas.hp.com/CfM/ucmdb/" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.hp.com/CfM/ucmdb/" elementFormDefault="qualified">
	<xs:annotation>
		<xs:documentation xml:lang="EN">
		</xs:documentation>
	</xs:annotation>
	<xs:element name="ConfigMessage">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="ConfigMessageHeader"/>
				<xs:element ref="CIsAndRelations"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="updateExisting" type="xs:boolean"/>
	<xs:element name="ignoreValidation" type="xs:boolean"/>
	<xs:element name="simulate" type="xs:boolean" default="false"/>
	<xs:element name="cmdbContext">
		<xs:simpleType>
			<xs:restriction base="xs:string"/>
		</xs:simpleType>
	</xs:element>
	<xs:element name="dataSource">
		<xs:simpleType>
			<xs:restriction base="xs:string"/>
		</xs:simpleType>
	</xs:element>
	<xs:element name="dataSourceContact">
		<xs:simpleType>
			<xs:restriction base="xs:string"/>
		</xs:simpleType>
	</xs:element>
	<xs:element name="dataSourceContactEmail">
		<xs:simpleType>
			<xs:restriction base="xs:string"/>
		</xs:simpleType>
	</xs:element>
	<xs:element name="ConfigMessageHeader">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="cmdbContext"/>
				<xs:element ref="dataSource"/>
				<xs:element ref="dataSourceContact"/>
				<xs:element ref="dataSourceContactEmail"/>
				<xs:element ref="simulate"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="CIsAndRelations">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="CIsForUpdate"/>
				<xs:element ref="relationsForUpdate"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="relationsForUpdate">
	</xs:element>
	<xs:element name="CIsForUpdate">
		<xs:complexType>
			<xs:sequence minOccurs="0" maxOccurs="unbounded">
				<xs:element ref="CI"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="CI">
		<xs:complexType>
			<xs:choice>
				<xs:element name="unix" type="unixType"/>
			</xs:choice>
		</xs:complexType>
	</xs:element>
	<xs:complexType name="managed_objectType">
		<xs:sequence minOccurs="0">
			<xs:element name="data_externalid" type="xs:string" minOccurs="0"/>
			<xs:element name="data_note" type="xs:string" minOccurs="0"/>
			<xs:element name="data_origin" type="xs:string" minOccurs="0"/>
			<xs:element name="name" type="xs:string" minOccurs="0"/>
		</xs:sequence>
	</xs:complexType>
	
	
	<xs:complexType name="configuration_itemType">
		<xs:complexContent>
			<xs:extension base="managed_objectType">
				<xs:sequence minOccurs="0">
					<xs:element name="is_save_persistency" type="xs:boolean" minOccurs="0"/>
					<xs:element name="track_changes" type="xs:boolean" minOccurs="0"/>
				</xs:sequence>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	
	
	<xs:complexType name="infrastructure_elementType">
		<xs:complexContent>
			<xs:extension base="configuration_itemType">
				<xs:sequence minOccurs="0">
					<xs:element name="codepage" type="xs:string" minOccurs="0"/>
					<xs:element name="credentials_id" type="xs:string" minOccurs="0"/>
					<xs:element name="language" type="xs:string" minOccurs="0"/>
					<xs:element name="customer_requested_exception" type="xs:boolean" minOccurs="0"/>
					<xs:element name="locationstring" type="xs:string" minOccurs="0"/>
				</xs:sequence>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	
	
	<xs:complexType name="nodeType">
		<xs:complexContent>
			<xs:extension base="infrastructure_elementType">
				<xs:sequence minOccurs="0">
					<xs:element name="bios_asset_tag" type="xs:string" minOccurs="0"/>
					<xs:element name="bios_serial_number" type="xs:string" minOccurs="0"/>
					<xs:element name="host_key" type="xs:string" minOccurs="0"/>
					<xs:element name="os_family" type="os_family_enum" minOccurs="0"/>
					<xs:element name="os_vendor" type="xs:string" minOccurs="0"/>
					<xs:element name="primary_dns_name" type="xs:string" minOccurs="0"/>
					<xs:element name="serial_number" type="xs:string" minOccurs="0"/>
					<xs:element name="snmp_sys_name" type="xs:string" minOccurs="0"/>
					<xs:element name="swap_memory_size" type="xs:integer" minOccurs="0"/>
					<xs:element name="sys_object_id" type="xs:string" minOccurs="0"/>
					<xs:element name="vendor" type="xs:string" minOccurs="0"/>
					<xs:element name="product_code" type="xs:string" minOccurs="0"/>
				</xs:sequence>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	
	<xs:complexType name="unixType">
		<xs:complexContent>
			<xs:extension base="nodeType">
				<xs:sequence minOccurs="0">
					<xs:element name="unix_description" type="xs:string" minOccurs="0"/>
					<xs:element name="markasdelete" type="xs:boolean" minOccurs="0"/>
				</xs:sequence>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	
	<xs:simpleType name="os_family_enum">
		<xs:restriction base="xs:string">
			<xs:enumeration value="baremetal_hypervisor"/>
			<xs:enumeration value="mainframe"/>
			<xs:enumeration value="unix"/>
			<xs:enumeration value="vax"/>
			<xs:enumeration value="windows"/>
		</xs:restriction>
	</xs:simpleType>
</xs:schema>

你可能感兴趣的:(java,spring,xml,ITeye,HP)