Powered by SmartDoc

The java.xml.pi option

The java.xml.pi option enables Relaxer objects to refer to processing instructions in an XML document.

Parameters

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

true
Enables support to access PIs in an XML document.
false
Disables support to access PIs in an XML document.

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

Artifacts

The java.xml.pi option adds the following methods to a Relaxer object. A client program can access PIs via these methods.

The rGetPi method accesses a PI value by a PI target name specified as a parameter.

The rGetPiMap method return a java.util.Map object containing information abount PIs in an XML document. The map object contains PI names and values as key/value pairs. The client can get all PI names and values in a document using this method.

Example

List 7.6.3.1[javaXmlPi.rng] is a sample schema for the java.xml.pi option.

javaXmlPi.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.pi option is as follows:

$ relaxer -java -java.xml.pi javaXmlPi.rng

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

$ relaxer -java.xml.pi javaXmlPi.rng

As a result, Relaxer generates six files:

List 7.6.3.1.1[JavaXmlPi.java] is a sample program for the java.xml.pi option.

JavaXmlPi.java
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;

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

	String uri = args[0];
	Account account = makeAccount(uri);
	printAccount(account);
	showAccount(account);
	getPi(account);
	dumpPis(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 getPi(Account account) {
	System.out.println("*** getPi ***");
	String myInfo = account.rGetPi("myInfo");
	System.out.println("myInfo:" + myInfo);
    }

    private static void dumpPis(Account account) {
	System.out.println("*** dumpPis ***");
	Map pis = account.rGetPiMap();
	Set keys = pis.keySet();
	Iterator iter = keys.iterator();
	while (iter.hasNext()) {
	    String key = (String)iter.next();
	    String value = (String)pis.get(key);
	    System.out.println(key + "=" + value);
	}
    }
}

This program adds two additional methods the getPi and the dumpPis. The getPi methods get a PI data via the rGetPi method and shows on the console. The dumpPis methods gets all PI data via the rGetPiMap methods and shows on the console.

Compilation of JavaXmlPi.java is shown here:

$ javac JavaXmlPi.java

No special options required.

Execution

List 7.6.3.2.1[JavaXmlPi.xml] is a valid XML document for testing.

JavaXmlPi.xml
<account accountNo="12345">
<?myInfo this is a sample PI ?>
  <balance>102030</balance>
<?balanceUnit yen ?>
  <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 JavaXmlPi class is shown here:

$ java JavaXmlPi javaXmlPi.xml
*** 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>
*** getPi ***
myInfo:this is a sample PI 
*** dumpPis ***
myInfo=this is a sample PI 
balanceUnit=yen 

The getPis method shows a data of PI which key is 'myInfo' and value is 'this is a sample PI'.

The dumpPis method shows all data of PI in which the XML document has.