Powered by SmartDoc

The java.pathlist option

The java.pathlist option enables a pathlist facility for Relaxer objects.

Parameters

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

true
Enables a pathlist facility.
false
Disables a pathlist facility.

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

Artifacts

The java.pathlist option generates additional classes as follows:

The java.pathlist option adds the following methods to Relaxer objects:

Example

List 7.25.3.1[javaPathlist.rng] is a sample schema for the java.pathlist option.

javaPathlist.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>
      <ref name="address"/>
      <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.pathlist option is as follows:

$ relaxer -java -java.pathlist javaPathlist.rng

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

$ relaxer -java.pathlist javaPathlist.rng

As a result, Relaxer generates seven files:

List 7.25.1.1[JavaPathlist.java] is a sample program for the java.pathlist option.

JavaPathlist.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import java.util.Properties;
import java.io.FileInputStream;

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

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

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

	System.out.println("*** makeAccount ***");
	Properties properties = new Properties();
	properties.load(new FileInputStream(filename));
	Account account = new Account();
	account.setup(properties);
	return (account);
    }

    private static void printAccount(Account account) {
	System.out.println("*** printAccount ***");
	String accountNo = account.getAccountNo();
	long balance = account.getBalance();
	String owner = account.getOwner();
	Address address = account.getAddress();
	String zip = address.getZip();
	String place = address.getContent();
	System.out.println("AccountNo:" + accountNo);
	System.out.println("Balance:" + balance);
	System.out.println("Owner:" + owner);
	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);
	}
	System.out.println("XML:" + account);
    }
}

Compilation of JavaPathlist.java class is shown here:

$ javac JavaPathlist.java

Execution

List 7.25.2.1[javaPathlist.properties] is a property file for test.

javaPathlist.properties
/account/@accountNo=12345
/account/balance=102030
/account/owner=XML Taro
/account/address=Yokohama
/account/address/@zip=213
/account/phone[1]=456-7890
/account/phone[1]/@area=123
/account/phone[2]=123-4567
/account/phone[2]/@area=090

Execution of the JavaPathlist class is shown here:

$ java JavaPatternIdmap javaPathlist.properties
*** 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