Powered by SmartDoc

The java.xml.element option

The java.xml.element option enables a ability to retain DOM objects to be used in the unmarshalling process.

Parameters

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

true
Enables a capability to retain DOM objects to build a Relaxer object.
false
Disables a capability to ratain DOM objects to build a Relaxer object

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

Artifacts

The java.xml.element option adds the following method to Relaxer objects:

The rGetElement method indicates to a org.w3c.dom.Element which is the original DOM object to generate the Relaxer object.

Example

List 7.8.3.1[javaXmlElement.rng] is a sample schema for the java.xml.element option.

javaXmlElement.rng
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
         datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  <start>
    <ref name="account"/>
  </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.xml.element option is as follows:

$ relaxer -java -java.xml.element javaXmlElement.rng

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

$ relaxer -java.xml.element javaXmlElement.rng

As a result, Relaxer generates six files:

List 7.8.3.1.1[JavaXmlElement.java] is a sample program for the java.xml.element option.

JavaXmlElement.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.w3c.dom.Element;

public class JavaXmlElement {
    public static void main(String[] args)
	throws IOException, SAXException, ParserConfigurationException {

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

    private static Account makeAccount(String uri)
	throws IOException, SAXException, ParserConfigurationException {

	System.out.println("*** makeAccount ***");
	Account account = new Account(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) {
	System.out.println("*** showAccount ***");
	System.out.println("XML:" + account);
    }

    private static void printYetAnother(Account account) {
	System.out.println("*** printYetAnother ***");
	Element element = account.rGetElement();
	String yetAnother = element.getAttribute("yetAnother");
	System.out.println("yetAnother = " + yetAnother);
    }
}

Compilation of JavaXmlElement.java is shown here:

$ javac JavaXmlElement.java

Execution

List 7.8.3.2.1[javaXmlElement.xml] is an XML document for testing.

javaXmlElement.xml
<account accountNo="12345"
         yetAnother="for your information">
  <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 JavaXmlElement class is shown here:

*** makeAccount ***
*** printAccount ***
AccountNo:12345
Balance:102030
Owner:XML Taro
Address: [213] Yokohama
Phone: [123] 456-7890
Phone: [090] 123-4567
*** showAccount ***
XML:<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>
*** printYetAnother ***
yetAnother = for your information

The sample program access an attribute 'yetAnother', which is not defined in the shcema, via the DOM's org.w3c.dom.Element object.