Powered by SmartDoc

The java.jaxb option

The java.jaxb option enables a JAXB facility for Relaxer objects.

Parameters

The java.jaxb option takes one of the following values as a parameter:

true
Enables a JAXB facility.
false
Disables a JAXB facility.

The default configuration is false. No parameter implies that the parameter is true.

Artifacts

Example

List 4.18.3.1[javaJaxb.rng] is a sample schema for the java.jaxb option.

javaJaxb.rng
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
         datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
         ns="http://example.com/account">
  <start>
    <choice>
      <ref name="account"/>
      <ref name="address"/>
      <ref name="phone"/>
    </choice>
  </start>
  <define name="account">
    <element name="account">
      <attribute name="accountNo">
        <data type="token"/>
      </attribute>
      <element name="balance">
        <data type="int"/>
      </element>
      <element name="owner">
        <data type="token"/>
      </element>
      <optional>
        <ref name="address"/>
      </optional>
      <zeroOrMore>
        <ref name="phone"/>
      </zeroOrMore>
    </element>
  </define>
  <define name="address">
    <element name="address">
      <attribute name="zip">
        <data type="token"/>
      </attribute>
      <text/>
    </element>
  </define>
  <define name="phone">
    <element name="phone">
      <attribute name="area">
        <data type="token"/>
      </attribute>
      <data type="token"/>
    </element>
  </define>
</grammar>

Build

Execution of Relaxer with the java.jaxb option is as follows:

$ relaxer -java -java.jaxb javaJaxb.rng

Because the Java generator is a default generator, execution of Relaxer as shown below has the same effect:

$ relaxer -java.jaxb javaJaxb.rng

As a result, Relaxer generates six files:

List 4.18.3.1.1[JavaJaxb.java] is a sample program for the java.jaxb option.

JavaJaxb.java
package com.example;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.Marshaller;
import javax.xml.bind.JAXBException;
import org.xml.sax.SAXException;

public class JavaJaxb {
    public static void main(String[] args)
	throws JAXBException {

	String uri = args[0];
	Account account = makeAccount(uri);
	printAccount(account);
	showAccount(account);
    }

    private static Account makeAccount(String uri) throws JAXBException {
	System.out.println("*** makeAccount ***");
	JAXBContext jc = JAXBContext.newInstance("com.example");
        Unmarshaller u = jc.createUnmarshaller();
        Account account = (Account)u.unmarshal(new File(uri));
	return (account);
    }

    private static void printAccount(Account account) {
	System.out.println("*** printAccount ***");
	String accountNo = account.getAccountNo();
	long balance = account.getBalance();
	String owner = account.getOwner();
	System.out.println("AccountNo:" + accountNo);
	System.out.println("Balance:" + balance);
	System.out.println("Owner:" + owner);
	Address address = account.getAddress();
	if (address != null) {
	    String zip = address.getZip();
	    String place = address.getContent();
	    System.out.println("Address: [" + zip + "] " + place);
	}
	Phone[] phones = account.getPhone();
	for (int i = 0;i < phones.length;i++) {
	    Phone phone = phones[i];
	    String area = phone.getArea();
	    String number = phone.getContent();
	    System.out.println("Phone: [" + area + "] " + number);
	}
    }

    private static void showAccount(Account account) throws JAXBException {
	System.out.println("*** showAccount ***");
	JAXBContext jc = JAXBContext.newInstance("com.example");
        Marshaller m = jc.createMarshaller();
	System.out.print("XML:");
	m.marshal(account, System.out);
	System.out.println();
    }
}

Compilation of JavaJaxb.java class is shown here:

$ javac JavaJaxb.java

Execution

List 4.18.3.2.1[javaJaxb.xml] is an XML document for testing.

javaJaxb.xml
<account xmlns="http://example.com/account" accountNo="12345">
  <balance>102030</balance>
  <owner>XML Taro</owner>
  <address zip="213">Yokohama</address>
  <phone area="123">456-7890</phone>
  <phone area="090">123-4567</phone>
</account>

Execution of the JavaJaxb class is shown here:

$ java JavaPatternIdmap javaPathlist.xml
*** makeAccount ***
*** printAccount ***
AccountNo:12345
Balance:102030
Owner:XML Taro
Address: [213] Yokohama
Phone: [123] 456-7890
Phone: [090] 123-4567
XML:<account \
    accountNo="12345"><balance>102030</balance><owner>XML \
    Taro</owner><address id="id_address" \
    zip="213">Yokohama</address><phone \
    area="123">456-7890</phone><phone id="id_phone" \
    area="090">123-4567</phone></account>
*** findElementsById ***
Address: [213] Yokohama
Phone: [090] 123-4567