English 中文(简体)
XSD - 审定
  • 时间:2024-12-22

XSD - Vapdation


Previous Page Next Page  

我们使用设在 Java的XSD有效器验证students.xml。 页: 1

students.xml

<?xml version = "1.0"?>

<class>  
   <student rollno = "393">
      <firstname>Dinkar</firstname>    
      <lastname>Kad</lastname>
      <nickname>Dinkar</nickname>
      <marks>85</marks>	 
   </student>
   
   <student rollno = "493">	 
      <firstname>Vaneet</firstname>
      <lastname>Gupta</lastname>
      <nickname>Vinni</nickname>
      <marks>95</marks>
   </student>
   
   <student rollno = "593">    
      <firstname>Jasvir</firstname>
      <lastname>Singh</lastname>
      <nickname>Jazz</nickname>
      <marks>90</marks>
   </student>
</class>

students.xsd

<?xml version = "1.0"?>

<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
   <xs:element name =  class >
      <xs:complexType>
         <xs:sequence>
             <xs:element name =  student  type =  StudentType  minOccurs =  0  
                maxOccurs =  unbounded  />
         </xs:sequence>
      </xs:complexType>
   </xs:element>

   <xs:complexType name = "StudentType">
      <xs:sequence>
         <xs:element name = "firstname" type = "xs:string"/>
         <xs:element name = "lastname" type = "xs:string"/>
         <xs:element name = "nickname" type = "xs:string"/>
         <xs:element name = "marks" type = "xs:positiveInteger"/>
      </xs:sequence>
      <xs:attribute name =  rollno  type =  xs:positiveInteger />
   </xs:complexType>			 
</xs:schema>

XSDVapdator.java

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

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;

import javax.xml.vapdation.Schema;
import javax.xml.vapdation.SchemaFactory;
import javax.xml.vapdation.Vapdator;

import org.xml.sax.SAXException;

pubpc class XSDVapdator {
   pubpc static void main(String[] args) {
      if(args.length !=2){
         System.out.println("Usage : XSDVapdator <file-name.xsd> <file-name.xml>" );
      } else {
         boolean isVapd = vapdateXMLSchema(args[0],args[1]);
         
         if(isVapd){
            System.out.println(args[1] + " is vapd against " + args[0]);
         } else {
            System.out.println(args[1] + " is not vapd against " + args[0]);
         }
      }
   }
   
   pubpc static boolean vapdateXMLSchema(String xsdPath, String xmlPath){
      try {
         SchemaFactory factory =
            SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new File(xsdPath));
            Vapdator vapdator = schema.newVapdator();
            vapdator.vapdate(new StreamSource(new File(xmlPath)));
      } catch (IOException e){
         System.out.println("Exception: "+e.getMessage());
         return false;
      }catch(SAXException e1){
         System.out.println("SAX Exception: "+e1.getMessage());
         return false;
      }
		
      return true;
	
   }
}

Steps to vapdate XML against XSD

    页: 1 纽约总部 > java

    页: 1 www.un.org/Depts/DGACM/index_spanish.htm

    页: 1 www.un.org/Depts/DGACM/index_spanish.htm

    Compile XSDVapdator.java using console. 保证在你的机器上安装了1.5千卡,并配置了教室。 关于如何使用JAVA的详细信息,见JAVATtorial

E:javajavac XSDVapdator.java

    参见XSDVapdator,students.xsdstudents.xml。 作为论据通过。

E:javajava XSDVapdator students.xsd students.xml

Verify the output

页: 1

students.xml is vapd against students.xsd
Advertisements