Hi first of all we explain the relevance of these two words and a brief introduction to why did the post. Marshalling
: It means creating an XML document tree content (in our case the content tree would be a Java Object.)
unmarshalling: It means creating a tree of XML document content. This is basically
serialization and deserialization.
This post came about because some time ago I made a development where the information came in an XML and parse the XML I should apply some business logic and return an XML response. For this all they gave me was a call request and a XML response.
The truth is that he had no looking forward to doing things by hand so I started researching and found some pretty interesting things, such as JAXB API is Java technology that provides an API and a tool to bind XML schema to a representation in code java.
Basically, this API provides us with a set of Annotations and classes to make our structure bindeo between Java objects and XML, or better yet if we possess the XSD of the XML you can use the xjc tool (comes by default in the distrubution of jdk) for us to generate the kinds of domain.
XMLs:
request.xml
\u0026lt;? Xml version = '1 .0 'encoding =' utf-8 '?>
<Parameters>
<UserId>AH875</UserId>
<SystemId>Test</SystemId>
</Parameters>
response.xml
<?xml version='1.0' encoding='utf-8' ?>
<Response>
<Status>Ok</Status>
<Data>
<SubSystem>
<SubSystemId>1</SubSystemId>
<Name>SubSystem1</Name>
\u0026lt;/ SubSystem>
\u0026lt;SubSystem>
\u0026lt;subsystemID> 2 \u0026lt;/ SUBSYSTEM>
\u0026lt;Name> SubSystem2 \u0026lt;/ Name>
\u0026lt;/ SubSystem>
\u0026lt;/ Data>
\u0026lt;/ Response>
As I said the truth was eager to do it by hand so I started looking for a tool that generates XML based on XSD and found a very good, here is a link
Trang (trang20030619.zip) .
If the down and earn the use of XML XSD I leave mine here
XSDs:
request.xsd
\u0026lt;? xml version = "1.0" encoding = "UTF-8"?>
\u0026lt;xs: schema xmlns: xs = "http: / / www.w3.org/2001/XMLSchema "elementFormDefault =" qualified ">
\u0026lt;xs:element name="Parameters">
\u0026lt;xs:complexType>
\u0026lt;xs:sequence>
\u0026lt;xs : element ref = "UserId" />
\u0026lt;xs:element ref="SystemId"/>
\u0026lt;/ xs: sequence>
\u0026lt;/ xs: complexType>
</xs:element>
<xs:element name="UserId" type="xs:NCName"/>
<xs:element name="SystemId" type="xs:NCName"/>
</xs:schema>
response.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="Response">
<xs:complexType>
<xs:sequence>
<xs:element ref="Status"/>
<xs:element ref="Data"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Status" type="xs:NCName"/>
<xs:element name="Data">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="SubSystem"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SubSystem">
<xs:complexType>
<xs:sequence>
<xs:element ref="SubSystemId"/>
<xs:element ref="Name"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SubSystemId" type="xs:integer"/>
<xs:element name="Name" type="xs:NCName"/>
</xs:schema>
Now that we can use our xjc xsd to get our java classes with their respective annotations.
Here is the complete example that can download and test. The truth is many things and solved in an elegant and efficient only need to know. Example
.
Greetings, Luis
0 comments:
Post a Comment