Two data binding from XML to Java code creating classes from XML data

Source: Internet
Author: User
Tags xmlns

The second of the data binding series is how to generate a Java language from the XML data restriction. This article demonstrates how to generate classes and code with complete code, and provides suggestions on how to customize your own version. Haven't you read the first article? The first, "objects, ubiquitous objects", explains how data binding transforms XML and Java language objects into each other. It compares data binding and other methods of working with XML in Java programs, and introduces an example of an XML configuration document. The first part also introduces the use of XML schemas to constrain data.

Before you dive into Java programs and XML code, take a quick look at the basics laid out in the first part of this series.

In the first section, we know that as long as you can identify a set of constraints on a document, you can convert the document into a Java object. Those constraints provide the interface for the data. As shown in the WEB service configuration documentation example, an XML document should be an instance of an existing Java class and generate that class from a data constraint. Finally, you will see an XML schema that represents the constraints of the sample XML document.

If you have any questions about the details, please review the first article.

Build the Foundation

Now you can start creating Java classes from an XML schema. The class must accurately represent data constraints and provide a simple read and write method that the Java application will use. Before we begin, let's review listing 1 to see the XML schema defined for the Webserviceconfiguration document.

Listing 1. XML schema representing the WEB container configuration document Data interface

<?xml version= "1.0"
<schema targetnamespace= "http://www.enhydra.org"
xmlns= "http://" Www.w3.org/1999/xmlSchema "
xmlns:enhydra=" http://www.enhydra.org "

<complextype name=" Serviceconfiguration ""
<attribute name= "name" type= "string"/>
<attribute name= "version" Type= " Float "/>
</complextype>
<element name=" serviceconfiguration "type=" Serviceconfiguration "/>
<complextype name= "webserviceconfiguration"
basetype= "serviceconfiguration"
derivedby= "extension"
<element name= "Port"
<complextype>
<attribute name= "protocol" type= "string"/>
<attribute name= "number" type= "integer"/>
<attribute name= "protected" type= "string"/>
</ Complextype>
</element>
<element name= "document"
<complextype>
<attribute Name= "root" type= "string"/>
<attribute name= "index" type= "string"/>
<attRibute name= "Error" type= "string"/>
</complextype>
</element>
</complextype>
<element name= "webserviceconfiguration" type= "webserviceconfiguration"/>
</schema>

Generate code

Before you start generating Java code, you must first determine the name of the core class. will use the Schemamapper in the Org.enhydra.xml.binding package, which is part of the Enhydra Application Server Utility class collection. You can also put any required support classes into this package.

In addition to the class name, you must determine the Java APIs that are used to read and create the XML. As discussed in the previous article, the three main options are SAX, DOM, and JDOM. Because SAX is only useful for reading XML documents, it is not appropriate to create XML. Because you want to convert Java objects to XML representations during the packaging phase, you need to create XML in this phase. This narrows the range of selections to DOM and JDOM. In the case where both options are available, in this case I choose to use the JDOM API just to show its functionality (not just because I am one of its co-authors!). )。

Finally, it is important to point out how XML schemas are provided to the Schemamapper class. In general, you can assume that the generation of the class is completed offline (through the static Main method). You can also use a class from a run-time environment only by making the Main method call a Non-static method. Once you have made these decisions, you can begin to sketch the framework of the class.

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.