Creating an XML Document
In this section you'll see how to create the XML document shown in
Listing 1 using the JDeveloper class you just created named
CreateXMLDocument.java. Open that class file, and import the DOM and SAX parsing APIs package
oracle.xml.parser.v2, and the DOM and SAX parsers package
oracle.xml.jaxp:
import oracle.xml.jaxp.*;
import oracle.xml.parser.v2.*;
Create a JXDocumentBuilderFactory object with the static
newInstance() method:
JXDocumentBuilderFactory factory = (JXDocumentBuilderFactory)
JXDocumentBuilderFactory.newInstance();
The JXDocumentBuilderFactory class extends the DocumentBuilderFactory class and adds some static fields to set factory attributes to the class. Table 1 lists the most important attributes. You'll use two of these, the
ERROR_STREAM and
SHOW_WARNINGS attributes later in the DOM parsing section.
Table 1. JXDocumentBuilderFactory Class Attributes: The table lists several of the most important attributes, along with a short description of each.
| Attribute |
Description |
| BASE_URL |
The base URL for parsing entities. |
| DEBUG_MODE |
This is a debugmode switch that you can set to Boolean.TRUE or Boolean.FALSE |
| ERROR_STREAM |
The error stream to use for reporting errors. The value is an OutputStream or PrintWriter. If the ErrorHandler event handler is set, ERROR_STREAM is not used. |
| SCHEMA_LANGUAGE |
Schema language for validation. |
| SCHEMA_SOURCE |
Schema source for validation. |
| SHOW_WARNINGS |
Value may be Boolean.TRUE or Boolean.FALSE. |
Create a JXDocumentBuilder object using the factory object by calling the
newDocumentBuilder() method. Cast the DocumentBuilder object returned by the method to a JXDocumentBuilder type as shown below:
JXDocumentBuilder documentBuilder = (JXDocumentBuilder)
factory.newDocumentBuilder();
You obtain a Document object from the JXDocumentBuilder object by calling the
newDocument() method. The XMLDocument class implements the Document interface, so you need to cast the returned Document object to an XMLDocument.
The XMLDocument class also implements several other interfaces:
- DocumentEditVAL
- ElementEditVAL
- DocumentEvent
- DocumentTraversal
- EventTarget
- NSResolver
The DocumentEditVAL and ElementEditVAL interfaces add dynamic validation capabilities as specified in the DOM 3 Validation specification. See my DevX article
Using DOM 3.0 Validation Techniques with XDK 10g for more information.
The DocumentEvent and EventTarget interfaces add event-handling capabilities, while the NSResolver interface aids in selecting namespace nodes with XPath. The
The XMLDocument class also provides some methods not specified in any of the implemented interfaces. Table 2 shows some of these methods.
Table 2. Additional XMLDocument Methods: The table lists some additional XMLDocument methods along with a description of each.
| Method |
Description |
| addID(String, XMLElement) |
Adds an ID Element for the document. |
| expectedElements(Element) |
Returns a vector of elements that may be added to the specified element. |
| getEncoding() |
Gets the document encoding. |
| setEncoding(String) |
Sets the document encoding |
| getVersion() |
Gets the XML version. |
| setVersion() |
Sets the XML Version. |
| getIDHashtable() |
Returns a hashtable of element IDs. |
| getSchema() |
Gets the XMLSchema specified in document. |
| setSchema(XMLSchema) |
Sets the XMLSchema for the document. |
printExternalDTD( OutputStream, String) |
Prints an external DTD to the specified OutputStream using the specified encoding. |
setDoctype(String rootname, String sysid, String pubid) |
Sets the Doctype. |
| setStandalone(String) |
Sets standalone mode for the XML declaration. |
| getStandalone() |
Gets the standalone mode for the XML declaration. |