I recently asked about the schema in my company. There are very few people who know the schema.
Schema is used to define XML. It is like defining the table structure in the database. xml without schma is a piece of sand.
Schema can be seen in many products, such as dataset in. net, infopath in office, and WSDL in WebService...
Schema Features:
Define simple and complex data structures
Use namespace to differentiate and reference each other
Language independence
You can add descriptions and other additional information...
Let's take a look at the simple functions of Schema:
Simple type, based on string, with a length of 2 ~ 5 digits
< Xs: simpletype Name = "Chinesename" >
< Xs: Restriction Base = "XS: string" >
< Xs: maxlength Value = "5" />
< Xs: minlength Value = "2" />
</ Xs: Restriction >
</ Xs: simpletype >
Simple type. The regular expression is used to limit the format based on string:
< Xs: simpletype Name = "Arenumber" >
< Xs: Restriction Base = "XS: string" >
< Xs: Pattern Value = "0 [1-9] [0-9] {1, 2 }" />
</ Xs: Restriction >
</ Xs: simpletype >
Simple type, based on string, enumeration:
< Xs: simpletype Name = "Gender" >
< Xs: Restriction Base = "XS: string" >
< Xs: Enumeration Value = "Male" />
< Xs: Enumeration Value = "Female" />
</ Xs: Restriction >
</ Xs: simpletype >
Posted @ steeven reading (115) | comment (0) | edit favorites
Java: schema + xmlbeans (2)
Writing XML by hand is a very painful task. Writing schema by hand is even more painful, that is, using xmlspy, or something that cannot be written, such as complicated data restrictions.
Xmlbeans is a Java Bean generation tool donated by BEA to Apache based on Schema. The above schema is compiled:
Scomp-Src SRC test. XSD
Generate the following class: (Note that the package is the targetnamespace of XSD and can be adjusted through the configuration file)
The Gender Enumeration type is automatically generated to generate nested enumeration classes. Is it very convenient?
Public Interface Gender extends org. Apache. xmlbeans. xmlstring
{
StaticFinal Enum male=Enum. forstring ("Male");
StaticFinal Enum female=Enum. forstring ("Female");
}
Test:
Arenumber Area = Arenumber. Factory. newinstance ();
Area. setstringvalue ( " 0467 " );
System. Out . Println (area );
System. Out . Println ( " Verification Result: " + Area. Validate ());
Gender gender = Gender. Factory. newinstance ();
Gender. setstringvalue (gender. Female. tostring ());
System. Out . Println (gender );
System. Out . Println ( " Verification Result: " + Gender. Validate ());
Chinesename CN = Chinesename. Factory. newinstance ();
CN. setstringvalue ( " Li " );
System. Out . Println (CN );
System. Out . Println ( " Verification Result: " + CN. Validate ());
Running result:
<XML-fragment>
0467
</XML-fragment>
Verification Result: True
<XML-fragment>
Female
</XML-fragment>
Verification Result: True
<XML-fragment>
Li
</XML-fragment>
Verification Result: false
Note:
The result of chinesename verification is false because the length does not match.
Because schematype only describes data, <XML-fragment> is only an anonymous container.
In addition to the newinstance () and validate () methods above, there are some other practical methods:
Gender. Factory. newvalue (object ); // Generate new instance
Gender. Factory. parse (File );
Gender. Factory. parse (Reader );
Gender. Factory. parse (inputstream );
Gender. Factory. parse (node );
Gender. Factory. parse (URL );
Gender. Factory. parse (string );
CN. newdomnode (); // Generate dom
CN. newinputstream (); // Generate inputstream
CN. newreader (); // Generate Reader
CN. xmltext (); // Generate XML text
CN. schematype (); // Get the compiled schema. Use it later
CN. Save (File );
CN. Save (writer );
CN. Save (outputstream );
CN. selectpath ( " Some/path [here = 'where'] " ); // Xpath
Posted @ steeven reading (121) | comment (2) | edit favorites