SAX uses a callback model for interacting with your code; you may also have heard this model called event-based programming. Whatever you call it, it’s a bit of a departure for object-oriented developers, so give it some time if you’re new to this type of programming.
In short, the parsing process is going to hum along, tearing through an XML document. Every time it encounters a tag, or comment, or text, or any other piece of XML, it calls back into your code, signaling that an event has occurred. Your code then has an opportunity to act, based on the details of that event.
For example, if SAX encounters the opening tag of an element, it fires off a startElement event. It provides information about that event, such as the name of the element, its attributes, and so on, and then your code gets to respond. You, as a programmer, have to write code for each event that is important to youfrom the start of a document to a comment to the end of an element.
February 13th, 2008 | Posted in xml SAX | No Comments
I’m increasingly of the “learning is best done by doing” philosophy, so I’m not going to hit you with a bunch of concept and theory before getting to code. SAX is a simple API, so you only need to understand its basic model, and how to get the API on your machine; beyond that, code will be your best teacher.
February 13th, 2008 | Posted in xml SAX | No Comments
XML is fundamentally about data; programming with XML, then, has to be fundamentally about getting at that data. That process, called parsing, is the basic task of the APIs I’ll cover in the next several chapters. This chapter describes how an XML document is parsed, focusing on the events that occur within this process. These events are important, as they are all points where application-specific code can be inserted and data manipulation can occur.
I’m also going to introduce you to one of the two core XML APIs in Java: SAX, the Simple API for XML (http://www.saxproject.org). SAX is what makes insertion of this application-specific code into events possible. The interfaces provided in the SAX package are an important part of any programmer’s toolkit for handling XML. Even though the SAX classes are small and few in number, they provide a critical framework for Java and XML to operate within. Solid understanding of how they help in accessing XML data is critical to effectively leveraging XML in your Java programs.
February 13th, 2008 | Posted in xml SAX | No Comments
- <?xml version="1.0"?>
- <grammar ns="" xmlns="http://relaxng.org/ns/structure/1.0"
- datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
- <start>
- <choice>
- <notAllowed/>
- <element name="toc">
- <optional>
- <attribute name="label">
- <data type="token"/>
- </attribute>
- </optional>
- <oneOrMore>
- <element name="topic">
- <optional>
- <attribute name="label">
- <data type="token"/>
- </attribute>
- </optional>
- <optional>
- <attribute name="href">
- <data type="token"/>
- </attribute>
- </optional>
- <element name="link">
- <optional>
- <attribute name="toc">
- <data type="token"/>
- </attribute>
- </optional>
- </element>
- </element>
- </oneOrMore>
- </element>
- </choice>
- </start>
- </grammar>
February 13th, 2008 | Posted in Constraints | No Comments
- <?xml version="1.0"?>
- <grammar ns="" xmlns="http://relaxng.org/ns/structure/1.0"
- datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
- <start>
- <choice>
- <ref name="topic"/>
- <element name="toc">
- <attribute name="label">
- <data type="normalizedString"/>
- </attribute>
- <oneOrMore>
- <ref name="topic"/>
- </oneOrMore>
- </element>
- <ref name="link"/>
- </choice>
- </start>
- <define name="link">
- <element name="link">
- <attribute name="toc">
- <data type="normalizedString"/>
- </attribute>
- </element>
- </define>
- <define name="topic">
- <element name="topic">
- <optional>
- <attribute name="href">
- <data type="normalizedString"/>
- </attribute>
- </optional>
- <attribute name="label">
- <data type="normalizedString"/>
- </attribute>
- <optional>
- <ref name="link"/>
- </optional>
- </element>
- </define>
- </grammar>
February 13th, 2008 | Posted in Constraints | No Comments
To convert DTDs to RELAX NG, use Sun’s RELAX NG Converter, which you download from https://msv.dev.java.net. The RELAX NG Converter began as its own project, but is now part of Sun’s Multi-Schema XML Validator project
Move the downloaded and extracted folder into somewhere useful; I moved mine into /usr/local/java, and then renamed the folder (rngconv-20030225) to a more manageable name (rngconv). Then just run java and supply the converter JAR file as the module to run:
java -jar /usr/local/java/rngconv/rngconv.jar toc.dtd > toc-dtd.rng
February 13th, 2008 | Posted in Constraints | No Comments
- <?xml version="1.0" encoding="UTF-8" ?>
- <grammar xmlns="http://relaxng.org/ns/structure/1.0"
- xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
- xmlns:java="http://www.relaxer.org/xmlns/relaxer/java"
- xmlns:relaxer="http://www.relaxer.org/xmlns/relaxer"
- xmlns:sql="http://www.relaxer.org/xmlns/relaxer/sql"
- datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
- ns="">
- <start>
- <ref name="toc"/>
- </start>
- <define name="toc">
- <element name="toc">
- <attribute name="label">
- <data type="token"/>
- </attribute>
- <oneOrMore>
- <ref name="topic"/>
- </oneOrMore>
- </element>
- </define>
- <define name="topic">
- <element name="topic">
- <optional>
- <attribute name="href">
- <data type="token"/>
- </attribute>
- </optional>
- <attribute name="label">
- <data type="token"/>
- </attribute>
- <ref name="link"/>
- </element>
- </define>
- <define name="link">
- <element name="link">
- <attribute name="toc">
- <data type="token"/>
- </attribute>
- </element>
- </define>
- </grammar>
February 13th, 2008 | Posted in Constraints | No Comments
< coolcode ><xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
xmlns:dw=”http://www.ibm.com/developerWorks/”
elementFormDefault=”unqualified”
attributeFormDefault=”unqualified” version=”4.0″>< / coolcode >
February 13th, 2008 | Posted in Constraints | No Comments
Last but not least (in this RELAX NG crash course, at least), you can type your data in RELAX NG, using the data element. Here’s the definition of a point type, from the RELAX NG tutorial, for example:
<element name="point" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"> <element name="x"> <data type="double"/> </element> <element name="y"> <data type="double"/> </element> </element>
When you use the string data type (instead of the <text/> tag) for an element, you have to specify the length allowed as well; for this reason, only use the string data type when you have a maximum length in mind:
<element name="email"> <data type="string"> <param name=”maxLength”>127</param> </data> </element>
That’s not much on RELAX NG, but it’s plenty to help you get started. As mentioned earlier, RELAX NG by Eric van der Vlist (O’Reilly) is available for a more in-depth look at the schema language.
February 13th, 2008 | Posted in Constraints | No Comments
Attributes are equally easy to specify:
<zeroOrMore> <element name="phonebook"> <oneOrMore> <element name="entry"> <element name="firstName"> <text/> </element> <optional> <element name="middleName> </optional> <element name="firstName"> <text/> </element> <zeroOrMore> <element name="address"> <attribute name="type"> <choice> <value>home</value> <value>work</value> <!-- and so on --> </choice> </attribute> </element> </zeroOrMore> <!-- etc... --> </element> </oneOrMore> </element> </zeroOrMore>
I also tossed in the choice operator, which allows you to indicate specific values that are allowed for the attribute.
February 13th, 2008 | Posted in Constraints | No Comments