Unmarshalling and Marshalling

At this point, marshalling and unmarshalling should be pretty routine. As in the last chapter, I’ll skip over application-specific business logic and move right to dealing with the actual marshal() and unmarshal() methods. You’ll see that the same principles, and sometimes even commands, used in JAXB and Zeus apply to Castor as well.

You’ll also need an XML document that corresponds to the HR XML Schema detailed earlier. Example 8-2 is such a document and is used throughout the rest of this chapter.

Example 8-2. HR instance document

<?xml version="1.0"?>

<employees>
  <employee id="2673">
    <name>Bobby Jones</name>
    <address>
      <street1>289 Running Brook Lane</street1>
      <city>Stanchion</city>
      <state>TX</state>
      <zip-code>79021</zip-code>
    </address>
    <organization id="24">
      <name>Billing</name>
    </organization>
    <office id="56">
      <address>
        <street1>112 Murdock</street1>
        <street2>Suite 2101</street2>
        <city>Millford</city>
        <state>TX</state>
        <zip-code>79025</zip-code>
      </address>    
    </office>
  </employee>
  
  <employee id="10982">
    <name>Cindy Cunningham</name>
    <address>
      <street1>1400 Sandy Lake Road</street1>
      <street2>Appartment 4D</street2>
      <city>Boston</city>
      <state>MA</state>
      <zip-code>20967</zip-code>
    </address>
    <organization id="11">
      <name>Marketing</name>
    </organization>
    <office id="12">
      <address>
        <street1>1800 Cambridge Drive</street1>
        <city>Boston</city>
        <state>MA</state>
        <zip-code>20968</zip-code>
      </address>    
    </office>
  </employee>
</employees>

Save this document as hr.xml ...

Get Java & XML Data Binding now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.