This chapter contains these topics:
MSDN: Loading a DataSet from XML MS Support: How To Read XML Data into a DataSet by Using Visual C#.NET MSDN: DataSet.ReadXml Method MSDN: DataSet.ReadXml Method (XmlReader) Once you have the dataset, you can access the table inside it. To use xml ensure that your environment is set up as described in 'Setting C XDK Environment Variables on UNIX' and 'Setting C XDK Environment Variables on Windows'. Use the following syntax on the command line to invoke xml (the Windows version is xml.exe): xml options document URI xml -f options document filespec. Secure XML to C# Class Converter. It easy to use and generated CSharp Class based on XML also Support XML File and URL.
Introduction to the XML Parser for C
This section contains the following topics:
See Also:
'Introduction to the XML Parser for Java' for a generic introduction to XML parsing with DOM and SAX. Much of the information in the introduction is language-independent and applies equally to C.Prerequisites
The Oracle XML parser for C reads an XML document and uses DOM or SAX APIs to provide programmatic access to its content and structure. You can use the parser in validating or nonvalidating mode.
This chapter assumes that you are familiar with the following technologies:
Document Object Model (DOM). DOM is an in-memory tree representation of the structure of an XML document.
Simple API for XML (SAX). SAX is a standard for event-based XML parsing.
Document Type Definition (DTD). An XML DTD defines the legal structure of an XML document.
XML Schema. Like a DTD, an XML schema defines the legal structure of an XML document.
XML Namespaces. Namespaces are a mechanism for differentiating element and attribute names.
If you require a general introduction to the preceding technologies, consult the XML resources listed in 'Related Documents' of the preface.
Standards and Specifications
XML 1.0 is a W3C Recommendation. The C XDK API provides full support for XML 1.0 (Second Edition). You can find the specification for the Second Edition at the following URL:
The DOM Level 1, Level 2, and Level 3 specifications are W3C Recommendations. The C XDK API provides full suport for DOM Level 1 and 2, but no support for Level 3. You can find links to the specifications for all three levels at the following URL:
SAX is available in version 1.0, which is deprecated, and 2.0. SAX is not a W3C specification. The C XDK API provides full support for both SAX 1.0 and 2.0. You can find the documentation for SAX at the following URL:
XML Namespaces are a W3C Recommendation. You can find the specification at the following URL:
See Also:
Chapter 29, 'XDK Standards' for a summary of the standards supported by the XDKUsing the XML Parser for C
Oracle XML parser for C checks if an XML document is well-formed, and optionally validates it against a DTD. Your application can access the parsed data through the DOM or SAX APIs.
This section contains the following topics:
Overview of the Parser API for C
The core of the XML parsing API are the XML, DOM, and SAX APIs. Table 15-1 describes the interfaces for these APIs. Refer to Oracle XML API Reference for the complete API documentation.
Table 15-1 Interfaces for XML, DOM, and SAX APIs
Package | Interfaces | Function Name Convention |
---|---|---|
XML | This package implements a single XML interface. The interface defines functions for the following tasks:
| Function names begin with the string Xml . Refer to Oracle Database XML C API Reference for API documentation. |
DOM | This package provides programmatic access to parsed XML. The package implements the following interfaces:
| Function names begin with the string XmlDom . Refer to Oracle Database XML C API Reference for API documentation. |
SAX | This package provides programmatic access to parsed XML. The package implements the SAX interface, which defines functions that receive notifications for SAX events. | Function names begin with the string XmlSax . Refer to Oracle Database XML C API Reference for API documentation. |
XML Parser for C Datatypes
Refer to Oracle XML API Reference for the complete list of datatypes for the C XDK. Table 15-2 describes the datatypes used in the XML parser for C.
Table 15-2 Datatypes Used in the XML Parser for C
Datatype | Description |
---|---|
| String pointer |
| Master XML context |
| SAX callback structure (SAX only) |
| 32-bit (or larger) unsigned integer |
| Native unsigned integer |
XML Parser for C Defaults
Note the following defaults for the XML parser for C:
Character set encoding is UTF-8. If all your documents are ASCII, then setting the encoding to US-ASCII increases performance.
The parser prints messages to
stderr
unless an error handler is provided.The parser checks inputs documents for well-formedness but not validity. You can set the property 'validate' to validate the input.
Note:
It is recommended that you set the default encoding explicitly if using only single byte character sets (such as US-ASCII or any of the ISO-8859 character sets) for faster performance than is possible with multibyte character sets such as UTF-8.The parser conforms to the XML 1.0 specification when processing whitespace, that is, the parser reports all whitespace to the application but indicates which whitespace can be ignored. However, some applications may prefer to set the property 'discard-whitespace,' which discards all whitespace between an end-element tag and the following start-element tag.
See Also:
Oracle Database XML C API Reference for the DOM, SAX, and callback APIs
Using the XML Parser for C: Basic Process
Figure 15-1 illustrates the calling sequence for the XML parser for C.
Figure 15-1 XML Parser for C Calling Sequence
Description of the illustration adxdk096.gif
Perform the following steps in your application:
Initialize the parsing process with the
XmlCreate()
function. The following sample code fragment is fromDOMNamespace.c
:Parse the input item, which can be an XML document or string buffer
If you are parsing with DOM, call the
XmlLoadDom()
function. The following sample code fragment is fromDOMNamespace.c
:If you are parsing with SAX, call the
XmlLoadSax()
function. The following sample code fragment is fromSAXNamespace.c
:If you are using the DOM interface, then include the following steps:
Use the
XmlLoadDom()
function to callXmlDomGetDocElem()
. This step calls other DOM functions, which are typically node or print functions that output the DOM document, as required. The following sample code fragment is fromDOMNamespace.c
:Invoke the
XmlFreeDocument()
function to clean up any data structures created during the parse process. The following sample code fragment is fromDOMNamespace.c
:
If you are using the SAX interface, then include the following steps:
Process the results of the invocation of
XmlLoadSax()
with callback functions.Register the callback functions. Note that you can set any of the SAX callback functions to
NULL
if not needed.
Use
XmlFreeDocument()
to clean up the memory and structures used during a parse. The program does not free memory allocated for parameters passed to the SAX callbacks or for nodes and data stored with the DOM parse tree until you callXMLFreeDocument()
orXMLDestroy()
. The following sample code fragment is fromDOMNamespace.c
:Either return to Step 2 or proceed to the next step.
Terminate the parsing process with
XmlDestroy()
. The following sample code fragment is fromDOMNamespace.c
:If threads fork off somewhere in the sequence of calls between initialization and termination, the application produces unpredictable behavior and results.
You can use the memory callback functions XML_ALLOC_F
and XML_FREE_F
for your own memory allocation. If you do, then specify both functions.
Running the XML Parser for C Demo Programs
The $ORACLE_HOME/xdk/demo/c/
(UNIX) and %ORACLE_HOME%xdkdemoc
(Windows) directories include several XML applications that illustrate how to use the XML parser for C with the DOM and SAX interfaces. Table 15-3 describes the demos.
Table 15-3 C Parser Demos
Directory | Contents | Demos |
---|---|---|
dom | The make utility compiles source files name.c to produce demo program name and output file name.out . The name.std is the expected output. The following demo programs use the DOM API:
| |
sax | The make utility compiles source files name.c to produce demo program name and output file name.out . The name.std is the expected output. The following demo programs use the SAX APIs:
|
You can find documentation that describes how to compile and run the sample programs in the README
in the same directory. The basic steps are as follows:
Change into the
$ORACLE_HOME/xdk/demo/c
directory (UNIX) or%ORACLE_HOME%xdkdemoc
directory (Windows).Make sure that your environment variables are set as described in 'Setting C XDK Environment Variables on UNIX' and 'Setting C XDK Environment Variables on Windows'.
Run
make
(UNIX) orMake.bat
(Windows) at the system prompt. Themake
utility changes into each demo subdirectory and runsmake
to do the following:Compiles the C source files with the
cc
utility. For example, theMakefile
in the$ORACLE_HOME/xdk/demo/c/dom
directory includes the following line:Runs each demo program and redirects the output to a file. For example, the
Makefile
in the$ORACLE_HOME/xdk/demo/c/dom
directory includes the following line:
Compare the
*.std
files to the*.out
files for each program. The*.std
file contains the expected output for each program. For example,DOMSample.std
contains the expected output from runningDOMSample
.
Using the C XML Parser Command-Line Utility
The xml
utility, which is located in $ORACLE_HOME/bin
(UNIX) or %ORACLE_HOME%bin
(Windows), is a command-line interface that parses XML documents. It checks for both well-formedness and validity.
To use xml
ensure that your environment is set up as described in 'Setting C XDK Environment Variables on UNIX' and 'Setting C XDK Environment Variables on Windows'.
Use the following syntax on the command line to invoke xml
(the Windows version is xml.exe
):
Table 15-4 describes the command-line options.
Table 15-4 C Parser Command-Line Options
Option | Description |
---|---|
-B BaseURI | Sets the base URI for the XSLT processor. The base URI of http://pqr/xsl.txt resolves pqr.txt to http://pqr/pqr.txt . |
-c | Checks well-formedness, but performs no validation. |
-e encoding | Specifies default input file encoding ('incoding'). |
-E encoding | Specifies DOM/SAX encoding ('outcoding'). |
-f file | Interprets the file as filespec, not URI. |
-G xptr_exprs | Evaluates XPointer scheme examples given in a file. |
-h | Shows usage help and basic list of command-line options. |
-hh | Shows complete list command-line options. |
-i n | Specifies the number of times to iterate the XSLT processing. |
-l language | Specifies the language for error reporting. |
-n | Traverses the DOM and reports the number of elements, as shown in the following sample output: |
-o XSLoutfile | Specifies the output file of the XSLT processor. |
-p | Prints the document/DTD structures after the parse. For example, the root element <greeting>hello</greeting> is printed as: |
-P | Prints the document from the root element. For example, the root element <greeting>hello</greeting> is printed as: |
-PP | Prints from the root node (DOC) and includes the XML declaration. |
-PE encoding | Specifies the encoding for -P or -PP output. |
-PX | Includes the XML declaration in the output. |
-s stylesheet | Specifies the XSLT stylesheet. |
-v | Displays the XDK parser version and then exits. |
-V var value | Tests top-level variables in CXSLT. |
-w | Preserves all whitespace. |
-W | Stops parsing after a warning. |
-x | Exercises the SAX interface and prints the document, as shown in the following sample output: |
Using the XML Parser Command-Line Utility: Example
You can test xml
on the various XML files located in $ORACLE_HOME/xdk/demo/c
. Example 15-1 displays the contents of NSExample.xml
.
You can parse this file, count the number of elements, and display the DOM tree as shown in the following example:
The output is shown in the next example.
Example 15-2 xml.out
Using the DOM API for C
This section contains the following topics:
Creating the Top-Level Context
When using the DOM to process XML in a traditional file system or on the Web, you need to perform implementation-specific steps only for startup.
Your application requires a top-level xmlctx
. This context contains encoding information, low-level memory callbacks, error message language, encoding, and so on. In short, this context contains those things that should remain consistent for all XDK components. The basic steps for initialization are as follows:
Allocate an
xmlctx
withXmlCreate()
. For example:Load documents and generate DOM events. After the application obtains an
xmlctx
, it can load a serialized XML document with theXmlLoadDom()
orXmlLoadSax()
functions. Given the Document node, all API DOM functions are available. You can generate DOM as in the following example:To generate SAX events, you need a SAX callback structure as shown in the following example:
You can tear down the XML context with the
XmlDestroy()
function.
Controlling the Data Encoding of XML Documents for the C API
XML data occurs in many encodings. You can control the XML encoding in the following ways:
Specify a default encoding to assume for files that are not self-describing
Specify the presentation encoding for DOM or SAX
Re-encode when a DOM is serialized
Input XML data is always encoded. Some encodings are entirely self-describing, such as UTF-16, which requires a specific BOM before the start of the actual data. The XMLDecl
or MIME header of the document can also specify an encoding. If the application cannot determine the specific encoding, then it applies the default input encoding. If you do not provide a default, then the application assumes UTF-8 on ASCII platforms and UTF-E on EBCDIC platforms.
The API makes a provision for cases when the encoding data of the input document is corrupt. For example, suppose an ASCII document with an XMLDecl
of encoding=ascii
is blindly converted to EBCDIC. The new EBCDIC document contains (in EBCDIC) an XMLDecl
that incorrectly claims the document is ASCII. The correct behavior for a program that is re-encoding XML data is to regenerate but not convert the XMLDecl
. The XMLDecl
is metadata, not data itself. This rule is often ignored, however, which results in corrupt documents. To work around this problem, the API provides an additional flag that enables you to forcibly set the input encoding, thereby overcoming an incorrect XMLDecl
.
The precedence rules for determining input encoding are as follows:
Forced encoding as specified by the user
Caution:
Forced encoding can result in a fatal error if there is a conflict. For example, the input document is UTF-16 and starts with a UTF-16 BOM, but the user specifies a forced UTF-8 encoding. In this case, the parser will object about the conflict.Protocol specification (HTTP header, and so on)
XMLDecl
specificationUser's default input encoding
The default, which is UTF-8 on ASCII platforms or UTF-E on EBCDIC platforms
After the application has determined the input encoding has been determined, it can parse the document and present the data. You are allowed to choose the presentation encoding; the data will be in that encoding regardless of the original input encoding.
When an application writes back a DOM in serialized form, it can choose at that time to re-encode the presentation data. Thus, the you can place the serialized document in any encoding.
Using NULL-Terminated and Length-Encoded C API Functions
C# Convert Xml To Json
The native string representation in C is NULL-terminated. Thus, the primary DOM interface takes and returns NULL-terminated strings. When stored in table form, however, Oracle XML DB data is not NULL-terminated but length-encoded. Consequently, the XDK provides an additional set of length-encoded APIs for the high-frequency cases to improve performance. In particular, the DOM functions in Table 15-5 have dual APIs.
Table 15-5 NULL-Terminated and Length-Encoded C API Functions
NULL-Terminated API | Length-Encoded API |
---|---|
XmlDomGetNodeName() | XmlDomGetNodeNameLen() |
XmlDomGetNodeLocal() | XmlDomGetNodeLocalLen() |
XmlDomGetNodeURI() | XmlDomGetNodeURILen() |
XmlDomGetNodeValue() | XmlDomGetNodeValueLen() |
XmlDomGetAttrName() | XmlDomGetAttrNameLen() |
XmlDomGetAttrLocal() | XmlDomGetAttrLocalLen() |
XmlDomGetAttrURI() | XmlDomGetAttrURILen() |
XmlDomGetAttrValue() | XmlDomGetAttrValueLen() |
Handling Errors with the C API
The C API functions typically either return a numeric error code (0 for success, nonzero on failure), or pass back an error code through a variable. In all cases, the API stores error codes. Your application can retrieve the most recent error by calling the XmlDomGetLastError()
function.
C Convert Lowercase Letter To Upper
By default, the API outputs error messages to stderr
. However, you can register an error message callback at initialization time. When an error occurs, the application invokes the registered callback and does not print an error.
Using the SAX API for C
To use SAX, initialize an xmlsaxcb
structure with function pointers and pass it to the XmlLoadSax()
call. You can also include a pointer to a user-defined context structure, which you pass to each SAX function.
See Also:
Oracle Database XML C API Reference for the SAX callback structureUsing OCI and the XDK C API
This section contains the following topics:
Using XMLType Functions and Descriptions
You can use the C API for XML for XMLType
columns in the database. An Oracle Call Interface (OCI) program can access XML data stored in a table by initializing the values of OCI handles such as the following:
Environment handle
Service handle
Error handle
Optional parameters
The program can pass these input values to the function OCIXmlDbInitXmlCtx()
, which returns an XML context. After the program makes calls to the C API, the function OCIXmlDbFreeXmlCtx()
frees the context.
Table 15-6 describes a few of the functions for XML operations.
Table 15-6 XMLType Functions
Function Name | Description |
---|---|
XmlCreateDocument() | Create empty XMLType instance |
XmlLoadDom() and so on | Create from a source buffer |
XmlXPathEvalexpr() and family | Extract an XPath expression |
XmlXslProcess() and family | Transform using an XSLT stylesheet |
XmlXPathEvalexpr() and family | Check if an XPath exists |
XmlDomIsSchemaBased() | Is document schema-based? |
XmlDomGetSchema() | Get schema information |
XmlDomGetNodeURI() | Get document namespace |
XmlSchemaValidate() | Validate using schema |
Cast (void *) to (xmldocnode *) | Obtain DOM from XMLType |
Cast (xmldocnode *) to (void *) | Obtain XMLType from DOM |
Initializing an XML Context
An XML context is a required parameter in all the C DOM API functions. This opaque context encapsulates information pertaining to data encoding, error message language, and so on. The contents of this XML context are different for XDK applications and for Oracle XML DB applications.
Caution:
Do not use an XML context for XDK in an XML DB application, or an XML context for XML DB in an XDK application.For Oracle XML DB, the two OCI functions that initialize and free an XML context have the following prototypes:
See Also:
Oracle Call Interface Programmer's Guide for reference material on the functions.
Oracle Call Interface Programmer's Guide for a discussion about OCI support for XML.
Oracle Database XML C API Reference for reference information on the DOM APIs
Creating XMLType Instances on the Client
You can construct new XMLType
instances on the client by using the XmlLoadDom()
calls. Follow these basic steps:
You first have to initialize the
xmlctx
, as illustrated in the example in 'Using the DOM API for C'.You can construct the XML data itself from the following sources:
User buffer
Local file
URI
The return value from these is an
(xmldocnode *)
, which you can use in the rest of the common C API.Finally, you can cast the
(xmldocnode *)
to a (void *)
and directly provide it as the bind value if required.
You can construct empty XMLType
instances by using the XmlCreateDocument()
call. This function would be equivalent to an OCIObjectNew()
for other types. You can operate on the (xmldocnode *)
returned by the preceding call and finally cast it to a (void *)
if it needs to be provided as a bind value.
Operating on XML Data in the Database Server
You can operate on XML data in Oracle Database by means of OCI statement calls. You can bind and define XMLType
values using xmldocnode
and use OCI statements to select XML data from the database. You can use this data directly in the C DOM functions. Similarly, you can bind the values directly to SQL statements.
Using OCI and the XDK C API: Examples
C Convert Linked List To Array
Example 15-3 illustrates how to construct a schema-based document with the DOM API and save it to the database. Note that you must include the header files xml.h
and ocixmldb.h
.
Example 15-3 Constructing a Schema-Based Document with the DOM API
Convert Xml Free
Example 15-4 illustrates how to get a document from the database and modify it with the DOM API.
Example 15-4 Modifying a Database Document with the DOM API