With the java.sax.input option, Relaxer objects can be generated by SAX events.
The java.sax.input option takes one of the following values as a parameter:
The default configuration is false
.
No parameter implies that the parameter is true
.
If you don't use the java.xml.namespace option, the java.sax.input option adds the following methods to Relaxer objects:
initElement(org.xml.sax.AttributeList)
IRDocumentClassHandler startElement(String, AttributeList)
IRDocumentClassHandler endElement(String)
characters(char[], int, int)
processingInstruction(String, String)
IRNode rGetParentRNode()
rSetParentRNode(IRNode)
IRNode[] rGetRNodes()
If you use the java.xml.namespace option, the java.sax.input option adds the following methods to Relaxer objects:
initElement(org.xml.sax.Attributes)
declarePrefixMapping(String, String)
IRContentClassHandler startElement(String, Attributes)
IRContentClassHandler endElement(String)
characters(char[], int, int)
processingInstruction(String, String)
IRNode rGetParentRNode()
rSetParentRNode(IRNode)
IRNode[] rGetRNodes()
List 7.14.3.1[javaSaxInput.rng] is a sample schema for the java.sax.input option.
<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>
Execution of Relaxer with the java.sax.input option is as follows:
$ relaxer -java -java.sax.input javaSaxInput.rng
Because the Java generator is a default generator, execution of Relaxer as shown below has the same effect:
$ relaxer -java.sax.input javaSaxInput.rng
As a result, Relaxer generates 10 files:
List 7.14.3.1.1[JavaSaxInput.java] is a sample program for the java.sax.input option.
import java.io.IOException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; import java.io.File; public class JavaSaxInput { public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException { String filename = args[0]; Account account = makeAccount(filename); printAccount(account); showAccount(account); } private static Account makeAccount(String filename) throws IOException, SAXException, ParserConfigurationException { System.out.println("*** makeAccount ***"); File file = new File(filename); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); JavaSaxInputContentHandler handler = new JavaSaxInputContentHandler(); parser.parse(file, handler); Account account = handler.getAccount(); 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); } }
Compilation of the JavaSaxInput
is shown here:
$ javac JavaSaxInput.java
List 7.14.3.2.1[javaSaxInput.xml] is a valid XML document with regard to the RENAX NG schema javaSaxInput.rng.
<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 JavaSaxInput
class with the valid document
is shown here:
$ java JavaSaxInput javaSaxInput.xml AccountNo:12345 Balance:102030 Owner:XML Taro Address: [213] Yokohama XML:<account \ accountNo="12345"><balance>102030</balance><owner>XML \ Taro</owner><address zip="213">Yokohama</address></account>
List 7.14.3.3.1[javaSaxInputNs.rng] is a sample schema for
the java.sax.input option with
the java.xml.namespace option.
The namespace name , which is specified by a ns
attribute in the grammar
element, is
http://example.com/account
.
<grammar xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes" ns="http://example.com/account"> <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>
Execution of the relaxer command with the java.sax.input option is as follows:
$ relaxer -java -java.sax.input -java.xml.namespace \ javaSaxInput.rng
Because the java generator is a default generator, execution of the relaxer command shown below makes same effect:
$ relaxer -java.sax.input -java.xml.namespace javaSaxInput.rng
As a result, Relaxer generates 8 files:
Account.java
is a Relaxer object.
URelaxer.java
and the RStack
are
fundamental utility classes.
URelaxer2.java
is a utility class for namespace operations.
IRNSContainer.java
and RNSContext.java
are
helper interface and object for namespace operations.
IRNode.java
is a helper interface for composite operation
which relates the java.pattern.composite
option.
import java.io.IOException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.SAXException; import java.io.File; public class JavaSaxInputNs { public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException { String filename = args[0]; Account account = makeAccount(filename); printAccount(account); showAccount(account); } private static Account makeAccount(String filename) throws IOException, SAXException, ParserConfigurationException { System.out.println("*** makeAccount ***"); File file = new File(filename); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); JavaSaxInputNsContentHandler handler = new JavaSaxInputNsContentHandler(); parser.parse(file, handler); Account account = handler.getAccount(); 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); } }
Compilation of JavaSaxInputNs.java
is shown here:
$ javac JavaSaxInputNs.java
List 7.14.3.5.1[javaSaxInputNs.xml] is an XML document for testing.
<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 JavaSaxInputNs
class is shown here:
$ java JavaSaxInputNs javaSaxInputNs.xml AccountNo:12345 Balance:102030 Owner:XML Taro Address: [213] Yokohama XML:<account xmlns="http://example.com/account" \ accountNo="12345"><balance>102030</balance><owner>XML \ Taro</owner><address zip="213">Yokohama</address></account>
List 7.14.3.5.2[javaSaxInputNsPrefix.xml] is an XML document for testing.
<z:account xmlns:z="http://example.com/account" accountNo="12345"> <z:balance>102030</z:balance> <z:owner>XML Taro</z:owner> <z:address zip="213">Yokohama</z:address> <z:phone area="123">456-7890</z:phone> <z:phone area="090">123-4567</z:phone> </z:account>
Execution of the JavaSaxInputNs
is shown here:
$ java JavaSaxInputNs javaSaxInputNsPrefix.xml java JavaSaxInputNs javaSaxInputNsPrefix.xml AccountNo:12345 Balance:102030 Owner:XML Taro Address: [213] Yokohama XML:<z:account xmlns:z="http://example.com/account" \ accountNo="12345"><z:balance>102030</z:balance><z:owner>XML \ Taro</z:owner><z:address \ zip="213">Yokohama</z:address></z:account>