Using XMLBeans to bind Xml-java data

Source: Internet
Author: User
Tags bind datetime visibility

Create a Xmlbean

Before you start creating XMLBeans, you need to download and install Apache XMLBeans 1.02 on your system. When a file is extracted from the XMLBeans archive, the bin directory and the Lib directory are seen in the uncompressed file. Then, place the bin directory in the path and place the Xbean.jar package in the Lib directory in the Classpath path.

The XML schema file (XSD file) creates the XMLBeans class. These XMLBeans classes can parse all XML instance documents that conform to XML schemas. Also, by using these XMLBeans classes, you can create an instance document.

For example, the following list of Weather_latlong.xsd schemas describes the contents of an XML document that contains a geographical location of weather, latitude and longitude information, all based on zip code.

<xsd:schema
xmlns:xsd= "Http://www.w3.org/2001/XMLSchema"
<!--this XML schema describes XML documents
containing either weather details or Latlong
Details of a location based on Zipcode two Global
E Lements Weather and Latlong, and one Global
Attribute Zipcode are declared.-->
<xsd:element name= "Weather" &G T
<xsd:complextype>
<xsd:sequence>
<xsd:element name= "Temperature"
Type= "Xsd:flo" At "/>
<xsd:element name=" humidity "
type=" Xsd:float "/>
<xsd:element name=" Visibility "
type= "Xsd:float"/>
<xsd:element name= "Datetime"
type= "Xsd:datetime"/>
</xsd:seque Nce>
<xsd:attribute ref= "Zipcode"/>
</xsd:complextype>
</xsd:element>
<xsd: Element name= "Latlong"
<xsd:complextype>
<xsd:sequence>
<xsd:element name= "Latitude"
type= "Xsd:string"/>
<xsd:element name= "longitude"
type= "xsd:string"/>
</xsd:sequence>
<xsd:attribut      E ref= "Zipcode"/>
</xsd:complextype>
</xsd:element>
<xsd:attribute name= "Zipcode"
Type= "xsd:string"/>
</xsd:schema>

The next steps will generate a set of XMLBeans classes that represent the XSD type pattern above. In the prompt for the working directory (where the files are extracted from the sample archive), enter the following command line:

scomp-outweather.jarweather_latlong.xsd

After compiling the above schema, XMLBeans generates the following five interfaces. Weatherdocument, Weatherdocument$weather, Latlongdocument, Latlongdocument$latlong and Zipcodeattribute.

Here, the Weatherdocument interface represents the document element, and the Weatherdocument$weather interface represents the global element weather. Similarly, the latlongdocument and Latlongdocument$latlong interfaces represent global element Latlong. The Zipcodeattribute interface represents the global property zipcode.

XMLBeans class

The XMLBeans class is discussed in detail below. XMLBeans provides 46 types of Java, reflecting the 46 built-in types defined in the XML Schema specification. For example, the xsd:string type is defined by the XMLBeans, which provides a xmlstring data type corresponding to it.

The weather interface created in weather_latlong.xsd mode is a local element visibility of the xsd:float type that declares the following two methods:

floatgetVisibility();

And

org.apache.xmlbeans.XmlFloatxgetVisibility();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.