gtpx1m0aXML User's Guide

Application Programming

The XML4C parser lets you interact with XML data. You can choose to use either the DOMpy or the SAX API with or without validation against a DTD or XML Schema document.

An application on TPF is coded in C++ language such that it uses one of the two APIs to interact with the parser. The chosen API determines how the parser accesses XML data. If the parser is asked to validate the data, it simultaneously interacts with the XML data and the associated DTD or XML schema document; otherwise, the parser interacts only with the XML document.

Figure 1. The XML Parser on TPF


Go to DOM or SAX for more information about each API; go to Validation for more information about the process of validating your XML document.

InputSource: Where Is the XML Document?

Whether you use the DOM or SAX API, your applications will need to tell the parser where to find the XML document. The location of the document is specified through the InputSource class and can be one of the following:

LocalFileInputSource
The XML document is in the TPF file system.

MemBufInputSource
The XML document is stored in memory.

STDInputSource
The XML document is coming into TPF through standard input (stdin).

URLInputSource
The XML document is located at the specified Web address (URL).
Note:
Although the XML4C parser allows an XML document to be specified as a uniform resource locator (URL), there is no HTTP client shipped with TPF to allow its use.

For more information about InputSource, go to XML4C Version 3.5.1 Documentation.

Document Type Definitions (DTDs): Defining the Rules

A DTD is one type of schema that is used for defining the tagging rules and structure for an XML document and is written using a strict (and specific) syntax. If you specify DOM or SAX as validating and your XML document uses a DTD to define the rules, the parser compares the XML document with the DTD to ensure that it conforms to the specified rules. The DTD can be in one of the following locations:

Note: On other platforms, DTDs can also be referenced through a URL. However, TPF does not have an HTTP client to support the use of this reference.

XML Schema: Another Way to Define Rules

XML Schema language can be used in place of a DTD to define the tagging structure and rules for an XML document. It is different from using a DTD because an XML Schema generally provides a more complete and precise definition document and is actually written in the XML language. An XML Schema is housed in a separate document and is referenced at the start of the XML document. For example, the following is taken from an XML document that references an XML Schema document called pnr.xsd:

<PNRroot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="pnr.xsd">

According to the Schema information in the XML4C Version 3.5.1 Documentation, "The schema must be specified by the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute on the root element of the document. The xsi prefix must be bound to the Schema document instance namespace as specified by the Recommendation."

Note: On other platforms, an XML Schema can also be referenced through a URL. However, TPF does not have an HTTP client to support the use of this reference.

Namespaces: A Two-Part Naming System

Simply put, an XML namespace is a collection of names and allows for two-part naming. Namespaces are often confused as a separate entity and something that exists or is coded. The W3C recommendation for namespaces simply uses a Universal Resource Identifier (URI) as a way of ensuring uniquely tagged elements. A namespace has an associated URI, of which each is unique by definition. By using this identifier with the words chosen for tag names, you can be assured that your tags are completely unique and can be used exactly the way you want them to be used. For example, an XML Schema based on a namespace will identify that namespace: <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

Each time a tag is used, associate that tag with that namespace as follows: <xsd:complexType name="USAddress">

You can use more than one namespace in a document. Using multiple namespaces can assist in using the same tag name in two different ways. For example, a tag name of <address> might mean a home address when associated with one namespace and a work address when associated with another namespace.

XML Schema documents do not require an associated namespace. If you do not have a namespace to associate with your XML Schema document, use the noNamespaceSchemaLocation attribute. For example, xsi:noNamespaceSchemaLocation="pnr.xsd"

Although a Web address (URL) is a type of URI, the use of URIs for namespaces does not require an HTTP client. The parser does not attempt to access the URI and, in fact, the URI does not even need to contain any data or content because namespaces only create a two-part naming convention.

For More Information

For more information, do the following: