XmlBean Learning 1. xmlBean Learning

Source: Internet
Author: User

XmlBean Learning 1. xmlBean Learning

The emergence of xmlBean is seen in the Document. Because the project uses JMS and data is transmitted between modules through xml files, learn about xmlBean. java also provides DOM and SAX to parse xm, however, xmlbean converts xml into javabean, which simplifies xml reading and writing. Well, the following describes how to use xmlBean:

First, download xmlbean, an open-source project under apache: http://archive.apache.org/dist/xml/xmlbeans/xmlbeans-current.zip. the latest version is 2.6.

After decompression, the directory is:

Xmlbeans-1.0.3

-- Bin

-- Docs

-- Lib

-- Schemas

-- Src

Second: Configure environment variables, configure path (E: \ xmlbeans-1.0.3 \ bin ).

Third, create an xml file (customers. xml). The content is directly found online,

<?xml version="1.0" encoding="UTF-8"?><Customers>    <customer>        <id>1</id>        <gender>female</gender>        <firstname>Jessica</firstname>        <lastname>Lim</lastname>        <phoneNumber>1234567</phoneNumber>        <address>            <primaryAddress>                <postalCode>350106</postalCode>                <addressLine1>#25-1</addressLine1>                <addressLine2>SHINSAYAMA 2-CHOME</addressLine2>            </primaryAddress>            <billingAddress>                <receiver>Ms Danielle</receiver>                <postalCode>350107</postalCode>                <addressLine1>#167</addressLine1>                <addressLine2>NORTH TOWER HARBOUR CITY</addressLine2>            </billingAddress>        </address>    </customer>    <customer>        <id>2</id>        <gender>male</gender>        <firstname>David</firstname>        <lastname>Bill</lastname>        <phoneNumber>808182</phoneNumber>        <address>            <primaryAddress>                <postalCode>319087</postalCode>                <addressLine1>1033 WS St.</addressLine1>                <addressLine2>Tima Road</addressLine2>            </primaryAddress>            <billingAddress>                <receiver>Mr William</receiver>                <postalCode>672993</postalCode>                <addressLine1>1033 WS St.</addressLine1>                <addressLine2>Tima Road</addressLine2>            </billingAddress>        </address>    </customer></Customers>

This is a data model of a customer. Each customer has a customer ID, name, gender, phoneNumber, and address. The addresses include: primaryAddress and Bill address. Each address consists of a zip code, address 1, and address 2. the bill address also contains the recipient ).

Fourth: steps:

1. generate an XML Schema file

Under normal circumstances, each xml file has a Schema file, which is an xml constraint, defines the structure elements of the xml file and constraints on the element structure.

Xmlbean needs to use this file to understand the structure constraints of the xml file, such as the data type. With this file, XMLBean will generate a series of related java calsses for xm operations, developers use these java classess to perform xml operations.

XMLSPY and Stylus Studio can be used to generate Schema files through xml files. Add the Schema file of customer. xml (sustomer. xsd)

<? Xml version = "1.0" encoding = "UTF-8"?>
<Xs: schema xmlns: xs = "http://www.w3.org/2001/XMLSchema" elementFormDefault = "qualified">

<Xs: element name = "Customers">
<Xs: complexType>
<Xs: sequence>
<Xs: element maxOccurs = "unbounded" name = "customer"
Type = "customerType"/>
</Xs: sequence>
</Xs: complexType>
</Xs: element>
<Xs: complexType name = "customerType">
<Xs: sequence>
<Xs: element name = "id" type = "xs: int"/>
<Xs: element name = "gender" type = "xs: string"/>
<Xs: element name = "firstname" type = "xs: string"/>
<Xs: element name = "lastname" type = "xs: string"/>
<Xs: element name = "phoneNumber" type = "xs: string"/>
<Xs: element name = "address" type = "addressType"/>
</Xs: sequence>
</Xs: complexType>
<Xs: complexType name = "addressType">
<Xs: sequence>
<Xs: element name = "primaryAddress" type = "primaryAddressType"/>
<Xs: element name = "billingAddress" type = "billingAddressType"/>
</Xs: sequence>
</Xs: complexType>

<Xs: complexType name = "primaryAddressType">
<Xs: sequence>
<Xs: element name = "postalCode" type = "xs: string"/>
<Xs: element name = "addressLine1" type = "xs: string"/>
<Xs: element name = "addressLine2" type = "xs: string"/>
</Xs: sequence>
</Xs: complexType>
<Xs: complexType name = "billingAddressType">
<Xs: sequence>
<Xs: element name = "receiver" type = "xs: string"/>
<Xs: element name = "postalCode" type = "xs: string"/>
<Xs: element name = "addressLine1" type = "xs: string"/>
<Xs: element name = "addressLine2" type = "xs: string"/>
</Xs: sequence>
</Xs: complexType>
</Xs: schema>

 

2. Use scomp to generate java classes

Scomp is a compilation tool provided by XMLBean. It is in the bin directory. Through this tool, we can generate the above Schema file Java Classes. scomp syntax as follows :-
Scomp [options] [dirs] * [schemaFile. xsd] * [service. wsdl] * [config. xsdconfig] *
Description of main parameters:
-Src [dir] -- the generated Java Classes storage directory
-Srconly -- do not compile Java Classes, and do not generate Jar files
-Out [jarFileName] -- generated Jar file. The default value is xmltypes. jar.
-Compiler -- Java compiler path, that is, Javac location
SchemaFile. xsd -- XML Schema file location
Config. xsdconfig -- the location of the xsdconfig file. This file is mainly used to develop some file naming rules for the generated Java Class and the name of the Package. In this article, the package is sample. xmlbean.
Prepare a configuration file (named customer. xsdconfig) with the following content:
<Xb: config xmlns: xb = "http://xml.apache.org/xmlbeans/2004/02/xbean/config">

<Xb: namespace>
<Xb: package> sample. xmlbean </xb: package>
</Xb: namespace>

</Xb: config>

 

My running command is like this:

E: \ xmlbeans-1.0.3> scomp-src build/src-out E:/xmlbeans-1.0.3/customerXmlBean. jar schemas/customer. xsd-compiler C:/"Program Files"/Javak1.7.0 _ 04/bin/javac E:/xmlbeans-1.0.3/schemas/custom er/customer. the xsdconfig Command tells scomp to put the generated source code in build under E: \ xmlbeans-1.0.3; customerXmlBean will be generated. jar to the E:/xmlbeans-1.0.3 root directory, it is recommended that this path write absolute path; Schame file is customer. xsd file. The xsdconfig file is customer. xsdconfig file. We mainly use customerXml Bean. jar. Source code in src: CustomersDocument. java -- Java Class ing of the entire XML document
CustomerType. java -- ing of node sustomer
AddressType. java -- Node address ing
BillingAddressType. java -- ing of node billingAddress
PrimaryAddressType. java -- ing of node primaryAddress
Note: In the scomp command, spaces are separated by different parameters. Therefore, the file paths in the parameters include spaces, which must be enclosed by quotation marks.

This article describes how to use xmlbean to read and write xml.

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.