Oracle9i Application Developer's Guide - XML Release 1 (9.0.1) Part Number A88894-01 |
|
This chapter contains the following sections:
Oracle provides a set of XML parsers for Java, C, C++, and PL/SQL. Each of these parsers is a stand-alone XML component that parses an XML document (or a standalone DTD or XML Schema) so that it can be processed by an application. Library and command-line versions are provided supporting the following standards and features:
These APIs permit applications to access and manipulate an XML document as a tree structure in memory. This interface is used by such applications as editors.
Additional features include:
The parsers are available on all Oracle platforms.
Figure 20-1 shows an XML document inputting XML Parser for Java. The DOM or SAX parser interface parses the XML document. The parsed XML is then transferred to the application for further processing.
If a stylesheet is used, the DOM or SAX interface also parses and outputs the XSL commands. These are sent together with the parsed XML to the XSLT Processor where the selected stylesheet is applied and the transformed (new) XML document is then output.
DOM and SAX APIs are explained in "DOM and SAX APIs".
The classes and methods used to parse an XML document are illustrated in the following diagrams:
The classes and methods used by the XSLT Processor to apply stylesheets are illustrated in the following diagram:
The V2 versions of the XML Parsers include an integrated XSL Transformation (XSLT) Processor for transforming XML data using XSL stylesheets. Using the XSLT processor, you can transform XML documents from XML to XML, XML to HTML, or to virtually any other text-based format. See Figure 20-1.
The processor supports the following standards and features:
The Java, C, and C++ XML parsers also support XML Namespaces. Namespaces are a mechanism to resolve or avoid name collisions between element types (tags) or attributes in XML documents.
This mechanism provides "universal" namespace element types and attribute names whose scope extends beyond this manual.
Such tags are qualified by uniform resource identifiers (URIs), such as:
<oracle:EMP xmlns:oracle="http://www.oracle.com/xml"/>
For example, namespaces can be used to identify an Oracle <EMP> data element as distinct from another company's definition of an <EMP> data element.
This enables an application to more easily identify elements and attributes it is designed to process. The Java, C, and C++ parsers support namespaces by being able to recognize and parse universal element types and attribute names, as well as unqualified "local" element types and attribute names.
The Java, C, and C++ parsers can parse XML in validating or non-validating modes.
Validation involves checking whether or not the attribute names and element tags are legal, whether nested elements belong where they are, and so on.
XML documents are made up of storage units called entities, which contain either parsed or unparsed data. Parsed data is made up of characters, some of which form character data, and some of which form markup.
Markup encodes a description of the document's storage layout and logical structure. XML provides a mechanism to impose constraints on the storage layout and logical structure.
A software module called an XML processor is used to read XML documents and provide access to their content and structure. It is assumed that an XML processor is doing its work on behalf of another module, called the application.
This parsing process is illustrated in Figure 20-2.
XML APIs generally fall into the following two categories:
See Figure 20-3. Consider the following simple XML document:
<?xml version="1.0"?> <EMPLIST> <EMP> <ENAME>MARY</ENAME> </EMP> <EMP> <ENAME>SCOTT</ENAME> </EMP> </EMPLIST>
A tree-based API (such as Document Object Model, DOM) builds an in-memory tree representation of the XML document. It provides classes and methods for an application to navigate and process the tree.
In general, the DOM interface is most useful for structural manipulations of the XML tree, such as reordering elements, adding or deleting elements and attributes, renaming elements, and so on. For example, for the XML document above, the DOM creates an in-memory tree structure as shown inFigure 20-3.
An event-based API (such as SAX) uses calls to report parsing events to the application. The application deals with these events through customized event handlers. Events include the start and end of elements and characters.
Unlike tree-based APIs, event-based APIs usually do not build in-memory tree representations of the XML documents. Therefore, in general, SAX is useful for applications that do not need to manipulate the XML tree, such as search operations, among others.
The above XML document becomes a series of linear events as shown in Figure 20-3.
Here are some guidelines for using the DOM and SAX APIs:
Use the SAX API when your data is mostly streaming data.
Oracle XML Parser can also compress XML documents. Using the compression feature, an in-memory DOM tree or the SAX events generated from an XML document can be compressed to generate a binary compressed output.
The compressed stream generated from DOM and SAX are compatible, that is, the compressed stream generated from SAX could be used to generate the DOM tree and vice versa. The compression is based on tokenizing the XML tags. This is based on the assumption that XML files typically have repeated tags and tokenizing the tags compresses the data. The compression depends on the type of input XML document -- the larger the number of tags, the less the text content, and the better the compression.
As with XML documents in general, you can store the compressed XML data output as a CLOB (Character Large Object) in the database.
An XML document is compressed into a binary stream by means of the serialization of an in-memory DOM tree. When a large XML document is parsed and a DOM tree is created in memory corresponding to it, it may be difficult to satisfy memory requirements and this could affect performance. The XML document is compressed into a byte stream and stored in an in-memory DOM tree. This can be expanded at a later time into a DOM tree without performing validation on the XML data stored in the compressed stream.
The compressed stream can be treated as a serialized stream, but note that the information in the stream is more controlled and managed, compared to the compression implemented by Java's default serialization.
In this release, there are two kinds of XML compressed streams:
The compressed stream is generated using SAX events and that generated using DOM serialization are compatible. You can use the compressed stream generated by SAX events to create a DOM tree and vice versa. The compression algorithm used is based on tokenizing the XML tag's. The assumption is that any XML file has repeated number of tags and therefore tokenizing the tags will give considerable compression.
If you already have XDK for Java installed, and are upgrading to Oracle9i, follow these steps:
ORACLE_HOME
/rdbms/admin
directory.
SYSDBA
privileges.
STARTUP
:
SQL> STARTUP
You may need to use the PFILE
option to specify the location of your initialization parameter file.
If you are upgrading from release 8.1.5, run xmlu815.sql
:
SQL> @xmlu815.sql
If you are upgrading from release 8.1.6, run xmlu816.sql
:
SQL> @xmlu816.sql
If you are upgrading from release 8.1.7, run xmlu817.sql
:
SQL> @xmlu817.sql
SHUTDOWN
:
SQL> SHUTDOWN
The XDK for Java component is upgraded to the new release.
ORACLE_HOME
/javavm/install
directory.
If the Oracle system has JSP installed, then complete the following steps:
ORACLE_HOME
/javavm/install
directory.
See Chapter 13 of the Oracle9i Migration manual.
Table 20-1 lists the XML Parser for Java examples provided with XDK for Java software. The samples are located in the sample/
subdirectory. They illustrate how to use Oracle XML Parser for Java.
Note that because some package names are different in V2, different files were generated to show the differences between V2 and V1 of the XML Parser for Java.
To run the sample programs:
.class
files.
xmlparserv2.jar
and the current directory to the CLASSPATH.
java <classname> <sample xml file>
java XSLSample <sample xsl file> <sample xml file>
A few XML
files such as class.xml
, empl.xml
, and family.xml
, are provided as test cases.
XSL stylesheet iden.xsl,
can be used to achieve an identity transformation of the supplied XML files:
<?xml version = "1.0"?> <!DOCTYPE course [ <!ELEMENT course (Name, Dept, Instructor, Student)> <!ELEMENT Name (#PCDATA)> <!ELEMENT Dept (#PCDATA)> <!ELEMENT Instructor (Name)> <!ELEMENT Student (Name*)> ]> <course> <Name>Calculus</Name> <Dept>Math</Dept> <Instructor> <Name>Jim Green</Name> </Instructor> <Student> <Name>Jack</Name> <Name>Mary</Name> <Name>Paul</Name> </Student> </course>
<?xml version="1.0"?> <!DOCTYPE employee [ <!ELEMENT employee (Name, Dept, Title)> <!ELEMENT Name (#PCDATA)> <!ELEMENT Dept (#PCDATA)> <!ELEMENT Title (#PCDATA)> ]> <employee> <Name>John Goodman</Name> <Dept>Manufacturing</Dept> <Title>Supervisor</Title> </employee>
<?xml version="1.0" standalone="no"?> <!DOCTYPE family SYSTEM "family.dtd"> <family lastname="Smith"> <member memberid="m1">Sarah</member> <member memberid="m2">Bob</member> <member memberid="m3" mom="m1" dad="m2">Joanne</member> <member memberid="m4" mom="m1" dad="m2">Jim</member> </family>
<!ELEMENT family (member*)> <!ATTLIST family lastname CDATA #REQUIRED> <!ELEMENT member (#PCDATA)> <!ATTLIST member memberid ID #REQUIRED> <!ATTLIST member dad IDREF #IMPLIED> <!ATTLIST member mom IDREF #IMPLIED>
<?xml version="1.0"?> <!-- Identity transformation --> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="*|@*|comment()|processing-instruction()|text()"> <xsl:copy> <xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
<!DOCTYPE doc [ <!ELEMENT doc (child*)> <!ATTLIST doc xmlns:nsprefix CDATA #IMPLIED> <!ATTLIST doc xmlns CDATA #IMPLIED> <!ATTLIST doc nsprefix:a1 CDATA #IMPLIED> <!ELEMENT child (#PCDATA)> ]> <doc nsprefix:a1 = "v1" xmlns="http://www.w3c.org" xmlns:nsprefix="http://www.oracle.com"> <child> This element inherits the default Namespace of doc. </child> </doc>
To write DOM based parser applications you can use the following classes:
Since DOMParser extends XMLParser, all methods of XMLparser are also available to DOMParser. Figure 20-4 shows the main steps you need when coding with the DOMParser()
class:
DOMParser()
class is called. Available properties to use with this class are:
DOMParser.reset()
to clean up any internal data structures, once the Parser has finished building the DOM tree.
DOMParser()
class is called. The available properties to apply to this class are:
XMLParser.parseDTD()
method along with the DTD input.
XMLParser.getDocumentType()
method then sends the resulting DTD object back to the new DOMParser()
and the process continues until the DTD has been applied.
The example, "XML Parser for Java Example 1: Using the Parser and DOM API (DomSample.java)", shows hoe to use DOMParser()
class.
The examples represent the way we write code so it is required to present the examples with Java coding standards (like all imports expanded), with documentation headers before the methods, and so on.
// This file demonstates a simple use of the parser and DOM API. // The XML file given to the application is parsed. // The elements and attributes in the document are printed. // This demonstrates setting the parser options. // import java.io.*; import java.net.*; import org.w3c.dom.*; import org.w3c.dom.Node; import oracle.xml.parser.v2.*; public class DOMSample { static public void main(String[] argv) { try { if (argv.length != 1) { // Must pass in the name of the XML file. System.err.println("Usage: java DOMSample filename"); System.exit(1); } // Get an instance of the parser DOMParser parser = new DOMParser(); // Generate a URL from the filename. URL url = createURL(argv[0]); // Set various parser options: validation on, // warnings shown, error stream set to stderr. parser.setErrorStream(System.err); parser.setValidationMode(DTD_validation); parser.showWarnings(true); // Parse the document. parser.parse(url); // Obtain the document. XMLDocument doc = parser.getDocument(); // Print document elements System.out.print("The elements are: "); printElements(doc); // Print document element attributes System.out.println("The attributes of each element are: "); printElementAttributes(doc); parser.reset(); } catch (Exception e) { System.out.println(e.toString()); } } static void printElements(Document doc) { NodeList nl = doc.getElementsByTagName("*"); Node n; for (int i=0; i<nl.getLength(); i++) { n = nl.item(i); System.out.print(n.getNodeName() + " "); } System.out.println(); } static void printElementAttributes(Document doc) { NodeList nl = doc.getElementsByTagName("*"); Element e; Node n; NamedNodeMap nnm; String attrname; String attrval; int i, len; len = nl.getLength(); for (int j=0; j < len; j++) { e = (Element)nl.item(j); System.out.println(e.getTagName() + ":"); nnm = e.getAttributes(); if (nnm != null) { for (i=0; i<nnm.getLength(); i++) { n = nnm.item(i); attrname = n.getNodeName(); attrval = n.getNodeValue(); System.out.print(" " + attrname + " = " + attrval); } } System.out.println(); } } static URL createURL(String fileName) { URL url = null; try { url = new URL(fileName); } catch (MalformedURLException ex) { File f = new File(fileName); try { String path = f.getAbsolutePath(); String fs = System.getProperty("file.separator"); if (fs.length() == 1) { char sep = fs.charAt(0); if (sep != '/') path = path.replace(sep, '/'); if (path.charAt(0) != '/') path = '/' + path; } path = "file://" + path; url = new URL(path); } catch (MalformedURLException e) { System.out.println("Cannot create url for: " + fileName); System.exit(0); } } return url; } }
See also Figure 20-4. The following provides comments for Example 1:
DOMParser()
. In Example 1, see the line:
DOMParser parser = new DOMParser();
This class has several properties you can use. Here the example uses:
parser.setErrorStream(System.err); parser.setValidationMode(DTD_validation); parser.showWarnings(true);
URL url = createURL(argv[0])
parser.parse(url);
XMLDocument doc = parser.getDocument();
Figure 20-3 illustrates the main processes involved when parsing an XML document using the DOM interface. The DOMNamespace() method is applied in the parser process at the "bubble" that states "Apply other DOM methods". The following example illustrates how to use DOMNamespace():
// This file demonstates a simple use of the parser and Namespace // extensions to the DOM APIs. // The XML file given to the application is parsed and the // elements and attributes in the document are printed. // import java.io.*; import java.net.*; import oracle.xml.parser.v2.DOMParser; import org.w3c.dom.*; import org.w3c.dom.Node; // Extensions to DOM Interfaces for Namespace support. import oracle.xml.parser.v2.XMLElement; import oracle.xml.parser.v2.XMLAttr; public class DOMNamespace { static public void main(String[] argv) { try { if (argv.length != 1) { // Must pass in the name of the XML file. System.err.println("Usage: DOMNamespace filename"); System.exit(1); } // Get an instance of the parser Class cls = Class.forName("oracle.xml.parser.v2.DOMParser"); DOMParser parser = (DOMParser)cls.newInstance(); // Generate a URL from the filename. URL url = createURL(argv[0]); // Parse the document. parser.parse(url); // Obtain the document. Document doc = parser.getDocument(); // Print document elements printElements(doc); // Print document element attributes System.out.println("The attributes of each element are: "); printElementAttributes(doc); } catch (Exception e) { System.out.println(e.toString()); } } static void printElements(Document doc) { NodeList nl = doc.getElementsByTagName("*"); XMLElement nsElement; String qName; String localName; String nsName; String expName; System.out.println("The elements are: "); for (int i=0; i < nl.getLength(); i++) { nsElement = (XMLElement)nl.item(i); // Use the methods getQualifiedName(), getLocalName(), getNamespace() // and getExpandedName() in NSName interface to get Namespace // information. qName = nsElement.getQualifiedName(); System.out.println(" ELEMENT Qualified Name:" + qName); localName = nsElement.getLocalName(); System.out.println(" ELEMENT Local Name :" + localName); nsName = nsElement.getNamespace(); System.out.println(" ELEMENT Namespace :" + nsName); expName = nsElement.getExpandedName(); System.out.println(" ELEMENT Expanded Name :" + expName); } System.out.println(); } static void printElementAttributes(Document doc) { NodeList nl = doc.getElementsByTagName("*"); Element e; XMLAttr nsAttr; String attrname; String attrval; String attrqname; NamedNodeMap nnm; int i, len; len = nl.getLength(); for (int j=0; j < len; j++) { e = (Element) nl.item(j); System.out.println(e.getTagName() + ":"); nnm = e.getAttributes(); if (nnm != null) { for (i=0; i < nnm.getLength(); i++) { nsAttr = (XMLAttr) nnm.item(i); // Use the methods getQualifiedName(), getLocalName(), // getNamespace() and getExpandedName() in NSName // interface to get Namespace information. attrname = nsAttr.getExpandedName(); attrqname = nsAttr.getQualifiedName(); attrval = nsAttr.getNodeValue(); System.out.println(" " + attrqname + "(" + attrname + ")" + " = " + attrval); } } System.out.println(); } } static URL createURL(String fileName) { URL url = null; try { url = new URL(fileName); } catch (MalformedURLException ex) { File f = new File(fileName); try { String path = f.getAbsolutePath(); String fs = System.getProperty("file.separator"); if (fs.length() == 1) { char sep = fs.charAt(0); if (sep != '/') path = path.replace(sep, '/'); if (path.charAt(0) != '/') path = '/' + path; } path = "file://" + path; url = new URL(path); } catch (MalformedURLException e) { System.out.println("Cannot create url for: " + fileName); System.exit(0); } } return url; } }
Applications can register a SAX handler to receive notification of various parser events. XMLReader is the interface that an XML parser's SAX2 driver must implement. This interface allows an application to set and query features and properties in the parser, to register event handlers for document processing, and to initiate a document parse.
All SAX interfaces are assumed synchronous: the parse methods must not return until parsing is complete, and readers must wait for an event-handler callback to return before reporting the next event.
This interface replaces the (now deprecated) SAX 1.0 Parser interface. The XMLReader interface contains two important enhancements over the old Parser interface:
Table 20-2 lists the class SAXParser() methods.
Figure 20-5 shows the main steps for coding with the SAXParser() class.
DOMParser()
class. Table 20-2 lists the available methods.
The example, "XML Parser for Java Example 3: Using the Parser and SAX API (SAXSample.java)", illustrates how you can use SAXParser() class and several handler interfaces.
// This file demonstates a simple use of the parser and SAX API. // The XML file given to the application is parsed and // prints out some information about the contents of this file. // import org.xml.sax.*; import java.io.*; import java.net.*; import oracle.xml.parser.v2.*; public class SAXSample extends HandlerBase { // Store the locator Locator locator; static public void main(String[] argv) { try { if (argv.length != 1) { // Must pass in the name of the XML file. System.err.println("Usage: SAXSample filename"); System.exit(1); } // (1) Create a new handler for the parser SAXSample sample = new SAXSample(); // (2) Get an instance of the parser Parser parser = new SAXParser(); // (3) Set Handlers in the parser parser.setDocumentHandler(sample); parser.setEntityResolver(sample); parser.setDTDHandler(sample); parser.setErrorHandler(sample); // (4) Convert file to URL and parse try { parser.parse(fileToURL(new File(argv[0])).toString()); } catch (SAXParseException e) { System.out.println(e.getMessage()); } catch (SAXException e) { System.out.println(e.getMessage()); } } catch (Exception e) { System.out.println(e.toString()); } } static URL fileToURL(File file) { String path = file.getAbsolutePath(); String fSep = System.getProperty("file.separator"); if (fSep != null && fSep.length() == 1) path = path.replace(fSep.charAt(0), '/'); if (path.length() > 0 && path.charAt(0) != '/') path = '/' + path; try { return new URL("file", null, path); } catch (java.net.MalformedURLException e) { throw new Error("unexpected MalformedURLException"); } } ////////////////////////////////////////////////////////////////////// // (5) Sample implementation of DocumentHandler interface. ////////////////////////////////////////////////////////////////////// public void setDocumentLocator (Locator locator) { System.out.println("SetDocumentLocator:"); this.locator = locator; } public void startDocument() { System.out.println("StartDocument"); } public void endDocument() throws SAXException { System.out.println("EndDocument"); } public void startElement(String name, AttributeList atts) throws SAXException { System.out.println("StartElement:"+name); for (int i=0;i<atts.getLength();i++) { String aname = atts.getName(i); String type = atts.getType(i); String value = atts.getValue(i); System.out.println(" "+aname+"("+type+")"+"="+value); } } public void endElement(String name) throws SAXException { System.out.println("EndElement:"+name); } public void characters(char[] cbuf, int start, int len) { System.out.print("Characters:"); System.out.println(new String(cbuf,start,len)); } public void ignorableWhitespace(char[] cbuf, int start, int len) { System.out.println("IgnorableWhiteSpace"); } public void processingInstruction(String target, String data) throws SAXException { System.out.println("ProcessingInstruction:"+target+" "+data); } ////////////////////////////////////////////////////////////////////// // (6) Sample implementation of the EntityResolver interface. ////////////////////////////////////////////////////////////////////// public InputSource resolveEntity (String publicId, String systemId) throws SAXException { System.out.println("ResolveEntity:"+publicId+" "+systemId); System.out.println("Locator:"+locator.getPublicId()+" "+ locator.getSystemId()+ " "+locator.getLineNumber()+" "+locator.getColumnNumber()); return null; } ////////////////////////////////////////////////////////////////////// // (7) Sample implementation of the DTDHandler interface. ////////////////////////////////////////////////////////////////////// public void notationDecl (String name, String publicId, String systemId) { System.out.println("NotationDecl:"+name+" "+publicId+" "+systemId); } public void unparsedEntityDecl (String name, String publicId, String systemId, String notationName) { System.out.println("UnparsedEntityDecl:"+name + " "+publicId+" "+ systemId+" "+notationName); } ////////////////////////////////////////////////////////////////////// // (8) Sample implementation of the ErrorHandler interface. ////////////////////////////////////////////////////////////////////// public void warning (SAXParseException e) throws SAXException { System.out.println("Warning:"+e.getMessage()); } public void error (SAXParseException e) throws SAXException { throw new SAXException(e.getMessage()); } public void fatalError (SAXParseException e) throws SAXException { System.out.println("Fatal error"); throw new SAXException(e.getMessage()); } }
To implement the XSLT Processor in the XML Parser for Java use XSLProcessor class.
Figure 20-6 shows the overall process used by class, XSLProcessor
.
XSLProcessor()
class declaration begins the XSLT process.
new XSLStylesheet() class
is declared with any of the following available methods:
Both inputs can be one of four types:
XSLProcessor.processXSL(xslstylesheet, xml instance)
XSLProcessor.processXSL()
method processes the XML input 1 through n times, using the selected stylesheet.
XSLProcessor.processXSL()
outputs either an output stream or a DOM document.
XML Parser for Java XSLT Processor is illustrated by the following examples:
/** * This file gives a simple example of how to use the XSL processing * capabilities of the Oracle XML Parser V2.0. An input XML document is * transformed using a given input stylesheet */ import org.w3c.dom.*; import java.util.*; import java.io.*; import java.net.*; import oracle.xml.parser.v2.*; public class XSLSample { /** * Transforms an xml document using a stylesheet * @param args input xml and xml documents */ public static void main (String args[]) throws Exception { DOMParser parser; XMLDocument xml, xsldoc, out; URL xslURL; URL xmlURL; try { if (args.length != 2) { // Must pass in the names of the XSL and XML files System.err.println("Usage: java XSLSample xslfile xmlfile"); System.exit(1); } // Parse xsl and xml documents parser = new DOMParser(); parser.setPreserveWhitespace(true); // parser input XSL file xslURL = createURL(args[0]); parser.parse(xslURL); xsldoc = parser.getDocument(); // parser input XML file xmlURL = createURL(args[1]); parser.parse(xmlURL); xml = parser.getDocument(); // instantiate a stylesheet XSLStylesheet xsl = new XSLStylesheet(xsldoc, xslURL); XSLProcessor processor = new XSLProcessor(); // display any warnings that may occur processor.showWarnings(true); processor.setErrorStream(System.err); // Process XSL DocumentFragment result = processor.processXSL(xsl, xml); // create an output document to hold the result out = new XMLDocument(); // create a dummy document element for the output document Element root = out.createElement("root"); out.appendChild(root); // append the transformed tree to the dummy document element root.appendChild(result); // print the transformed document out.print(System.out); } catch (Exception e) { e.printStackTrace(); } } // Helper method to create a URL from a file name static URL createURL(String fileName) { URL url = null; try { url = new URL(fileName); } catch (MalformedURLException ex) { File f = new File(fileName); try { String path = f.getAbsolutePath(); // This is a bunch of weird code that is required to // make a valid URL on the Windows platform, due // to inconsistencies in what getAbsolutePath returns. String fs = System.getProperty("file.separator"); if (fs.length() == 1) { char sep = fs.charAt(0); if (sep != '/') path = path.replace(sep, '/'); if (path.charAt(0) != '/') path = '/' + path; } path = "file://" + path; url = new URL(path); } catch (MalformedURLException e) { System.out.println("Cannot create url for: " + fileName); System.exit(0); } } return url; } }
This example code is not included in the sample/
subdirectory. It uses the XML Parser for Java v2, to perform the following tasks.
import org.w3c.dom.*; import java.util.*; import java.io.*; import java.net.*; import oracle.xml.parser.v2.*; public class XSLTransform { public static void main (String args[]) throws Exception { DOMParser parser; XMLDocument xml, xsldoc, out; URL xslURL; URL xmlURL; try { if (args.length != 2) { // Pass in the names of the XSL and XML files System.err.println("Usage: java XSLTransform xslfile xmlfile"); System.exit(1); } // Parse XSL and XML documents parser = new DOMParser(); parser.setPreserveWhitespace(true); xslURL = createURL(args[0]); parser.parse(xslURL); xsldoc = parser.getDocument(); xmlURL = createURL(args[1]); parser.parse(xmlURL); xml = parser.getDocument(); // Instantiate the stylesheet XSLStylesheet xsl = new XSLStylesheet(xsldoc, xslURL); XSLProcessor processor = new XSLProcessor(); // Display any warnings that may occur processor.showWarnings(true); processor.setErrorStream(System.err); // Process XSL DocumentFragment result = processor.processXSL(xsl, xml); // Create an output document to hold the result out = new XMLDocument(); // Create a dummy document element for the output document Element root = out.createElement("root"); out.appendChild(root); // Append the transformed tree to the dummy document element root.appendChild(result); // Print the transformed document out.print(System.out); } catch (Exception e) { e.printStackTrace(); } } }
See Figure 20-4 and Figure 20-6. The following provides comments for Example 5:
parser = new DOMParser(); parser.setPreserveWhitespace(true);
xslURL = createURL(args[0]); parser.parse(xslURL); xsldoc = parser.getDocument(); xmlURL = createURL(args[1]); xmlURL = createURL(args[1]); parser.parse(xmlURL); xml = parser.getDocument();
XSLStylesheet xsl = new XSLStylesheet(xsldoc, xslURL); XSLProcessor processor = new XSLProcessor();
processor.setErrorStream(System.err);
DocumentFragment result = processor.processXSL(xsl, xml);
out = new XMLDocument(); Element root = out.createElement("root"); out.appendChild(root); root.appendChild(result);
Using the SAXNamespace() class is illustrated in the following example:
// This file demonstrates a simple use of the Namespace extensions to // the SAX APIs. import org.xml.sax.*; import java.io.*; import java.net.URL; import java.net.MalformedURLException; // Extensions to the SAX Interfaces for Namespace support. import oracle.xml.parser.v2.XMLDocumentHandler; import oracle.xml.parser.v2.DefaultXMLDocumentHandler; import oracle.xml.parser.v2.NSName; import oracle.xml.parser.v2.SAXAttrList; import oracle.xml.parser.v2.SAXParser; public class SAXNamespace { static public void main(String[] args) { String fileName; //Get the file name if (args.length == 0) { System.err.println("No file Specified!!!"); System.err.println("USAGE: java SAXNamespace <filename>"); return; } else { fileName = args[0]; } try { // Create handlers for the parser // Use the XMLDocumentHandler interface for namespace support // instead of org.xml.sax.DocumentHandler XMLDocumentHandler xmlDocHandler = new XMLDocumentHandlerImpl(); // For all the other interface use the default provided by // Handler base HandlerBase defHandler = new HandlerBase(); // Get an instance of the parser SAXParser parser = new SAXParser(); // Set Handlers in the parser // Set the DocumentHandler to XMLDocumentHandler parser.setDocumentHandler(xmlDocHandler); // Set the other Handler to the defHandler parser.setErrorHandler(defHandler); parser.setEntityResolver(defHandler); parser.setDTDHandler(defHandler); try { parser.parse(fileToURL(new File(fileName)).toString()); } catch (SAXParseException e) { System.err.println(args[0] + ": " + e.getMessage()); } catch (SAXException e) { System.err.println(args[0] + ": " + e.getMessage()); } } catch (Exception e) { System.err.println(e.toString()); } } static public URL fileToURL(File file) { String path = file.getAbsolutePath(); String fSep = System.getProperty("file.separator"); if (fSep != null && fSep.length() == 1) path = path.replace(fSep.charAt(0), '/'); if (path.length() > 0 && path.charAt(0) != '/') path = '/' + path; try { return new URL("file", null, path); } catch (java.net.MalformedURLException e) { /* According to the spec this could only happen if the file protocol were not recognized. */ throw new Error("unexpected MalformedURLException"); } } private SAXNamespace() throws IOException { } } /*********************************************************************** Implementation of XMLDocumentHandler interface. Only the new startElement and endElement interfaces are implemented here. All other interfaces are implemented in the class HandlerBase. **********************************************************************/ class XMLDocumentHandlerImpl extends DefaultXMLDocumentHandler { public void XMLDocumentHandlerImpl() { } public void startElement(NSName name, SAXAttrList atts) throws SAXException { // Use the methods getQualifiedName(), getLocalName(), getNamespace() // and getExpandedName() in NSName interface to get Namespace // information. String qName; String localName; String nsName; String expName; qName = name.getQualifiedName(); System.out.println("ELEMENT Qualified Name:" + qName); localName = name.getLocalName(); System.out.println("ELEMENT Local Name :" + localName); nsName = name.getNamespace(); System.out.println("ELEMENT Namespace :" + nsName); expName = name.getExpandedName(); System.out.println("ELEMENT Expanded Name :" + expName); for (int i=0; i<atts.getLength(); i++) { // Use the methods getQualifiedName(), getLocalName(), getNamespace() // and getExpandedName() in SAXAttrList interface to get Namespace // information. qName = atts.getQualifiedName(i); localName = atts.getLocalName(i); nsName = atts.getNamespace(i); expName = atts.getExpandedName(i); System.out.println(" ATTRIBUTE Qualified Name :" + qName); System.out.println(" ATTRIBUTE Local Name :" + localName); System.out.println(" ATTRIBUTE Namespace :" + nsName); System.out.println(" ATTRIBUTE Expanded Name :" + expName); // You can get the type and value of the attributes either // by index or by the Qualified Name. String type = atts.getType(qName); String value = atts.getValue(qName); System.out.println(" ATTRIBUTE Type :" + type); System.out.println(" ATTRIBUTE Value :" + value); System.out.println(); } } public void endElement(NSName name) throws SAXException { // Use the methods getQualifiedName(), getLocalName(), getNamespace() // and getExpandedName() in NSName interface to get Namespace // information. String expName = name.getExpandedName(); System.out.println("ELEMENT Expanded Name :" + expName); } }
oraxml
is a command-line interface to parse an XML document. It checks for well-formedness and validity.
To use oraxml ensure the following:
oraxml
supports schema validation, include xschema.jar
also in your CLASSPATH
Use the following syntax to invoke oraxml
:
oraxml options* source
oraxml
expects to be given an XML file to parse. Table 20-3 lists oraxml's command line options.
oraxsl
is a command-line interface used to apply a stylesheet on multiple XML documents. It accepts a number of command-line options that dictate how it should behave.
To use oraxsl
ensure the following:
Use the following syntax to invoke oraxsl
:
oraxsl options* source? stylesheet? result?
oraxsl
expects to be given a stylesheet, an XML file to transform, and optionally, a result file. If no result file is specified, it outputs the transformed document to standard out. If multiple XML documents need to be transformed by a stylesheet, the -l or -d options in conjunction with the -s and -r options should be used instead. These and other options are described in Table 20-4.
XML extension functions for XSLT processing allow users of XSLT processor to call any Java method from XSL expressions. Java extension functions should belong to the namespace that starts with the following:
http://www.oracle.com/XSL/Transform/java/
An extension function that belongs to the following namespace:
http://www.oracle.com/XSL/Transform/java/classname
refers to methods in class classname
. For example, the following namespace:
http://www.oracle.com/XSL/Transform/java/java.lang.String
can be used to call java.lang.String
methods from XSL expressions.
If the method is a non-static method of the class, then the first parameter will be used as the instance on which the method is invoked, and the rest of the parameters are passed on to the method.
If the extension function is a static method, then all the parameters of the extension function are passed on as parameters to the static function.
The following XSL, static function example:
<xsl:stylesheet xmlns:math="http://www.oracle.com/XSL/Transform/java/java.lang.Math"> <xsl:template match="/"> <xsl:value-of select="math:ceil('12.34')"/> </xsl:template> </xsl:stylesheet>
prints out '13'.
The extension function 'new' creates a new instance of the class and acts as the constructor.
The following constructor function example:
<xsl:stylesheet xmlns:jstring="http://www.oracle.com/XSL/Transform/java/java.lang.String"> <xsl:template match="/"> <!-- creates a new java.lang.String and stores it in the variable str1 --> <xsl:variable name="str1" select="jstring:new('Hello World')"/> <xsl:value-of select="jstring:toUpperCase($str1)"/> </xsl:template> </xsl:stylesheet>
prints out 'HELLO WORLD'.
The result of an extension function can be of any type, including the five types defined in XSL:
They can be stored in variables or passed onto other extension functions.
If the result is of one of the five types defined in XSL, then the result can be returned as the result of an XSL expression.
Here is an XSL example illustrating the Return value extension function:
<!-- Declare extension function namespace --> <xsl:stylesheet xmlns:parser = "http://www.oracle.com/XSL/Transform/java/oracle.xml.parser.v2.DOMParser" xmlns:document = "http://www.oracle.com/XSL/Transform/java/oracle.xml.parser.v2.XMLDocument" > <xsl:template match ="/"> <!-- Create a new instance of the parser, store it in myparser variable --> <xsl:variable name="myparser" select="parser:new()"/> <!-- Call a non-static method of DOMParser. Since the method is anon-static method, the first parameter is the instance on which themethod is called. This is equivalent to $myparser.parse('test.xml') --> <xsl:value-of select="parser:parse($myparser, 'test.xml')"/> <!-- Get the document node of the XML Dom tree --> <xsl:variable name="mydocument" select="parser:getDocument($myparser)"/> <!-- Invoke getelementsbytagname on mydocument --> <xsl:for-each select="document:getElementsByTagName($mydocument,'elementname')"> ...... </xsl:for-each> </xsl:template> </xsl:stylesheet>
Overloading based on number of parameters and type is supported. Implicit type conversion is done between the five XSL types as defined in XSL.
Type conversion is done implicitly between (String, Number, Boolean, ResultTree) and from NodeSet to (String, Number, Boolean, ResultTree).
Overloading based on two types which can be implicitly converted to each other is not permitted.
The following overloading will result in an error in XSL, since String and Number can be implicitly converted to each other:
Mapping between XSL type and Java type is done as following:
String -> java.lang.String Number -> int, float, double Boolean -> boolean NodeSet -> XMLNodeList ResultTree -> XMLDocumentFragment
The XML Parser for Java Frequently Asked Questions (FAQs) are organized into the following topics:
I was wondering if someone could help me verify the syntax for the following DTD. I realize that I can use a DTD editor to do this for me, but the editor I'm using is not very good.
<?xml version="1.0"?> <!DOCTYPE CATALOG [ <!ELEMENT CATALOG ( ADMIN, SCHEMA?, DATA? ) > <!ATTLIST CATALOG xml:lang NMTOKEN #IMPLIED > <!ELEMENT ADMIN ( NAME, INFORMATION) > <!ELEMENT SCHEMA (CATEGORY | DESCRIPTOR)* > <!ELEMENT DATA (ITEM)*> <!ELEMENT NAME (#PCDATA) > <!ELEMENT INFORMATION ( DATE, SOURCE ) > <!ELEMENT DATE (#PCDATA) > <!ELEMENT SOURCE (#PCDATA) > <!ELEMENT CATEGORY (NAME | KEY | TYPE | UPDATE )* > <!ATTLIST CATEGORY ACTION (ADD|DELETE|UPDATE) #REQUIRED> <!ELEMENT DESCRIPTOR (NAME | KEY | UPDATE | OWNER | TYPE )* > <!ATTLIST DESCRIPTOR ACTION (ADD|DELETE|UPDATE) #REQUIRED> <!ELEMENT OWNER (NAME?, KEY? ) > <!ELEMENT KEY (#PCDATA) > <!ELEMENT TYPE (#PCDATA) > <!ELEMENT ITEM (OWNER?, NAMEVALUE*, UPDATE ) > <!ATTLIST ITEM ACTION (ADD | DELETE | UPDATE) #REQUIRED> <!ELEMENT UPDATE (NAME | KEY | NAMEVALUE )* > <!ELEMENT NAMEVALUE ( NAME, VALUE ) > <!ELEMENT VALUE (#PCDATA)* > ]>
I'm unsure about the ATTLIST syntax.
I loaded this into XMLAuthority 1.1 and did a Save As. XML Authority lets you visually inspect and edit DTD's and XML Schemas. Highly recommended. http://www.extensibility.com ($99.00).
It came back with:
<!ELEMENT CATALOG (ADMIN , SCHEMA? , DATA? )> <!ATTLIST CATALOG xml:lang NMTOKEN #IMPLIED > <!ELEMENT ADMIN (NAME , INFORMATION )> <!ELEMENT SCHEMA (CATEGORY | DESCRIPTOR )*> <!ELEMENT DATA (ITEM )*> <!ELEMENT NAME (#PCDATA )> <!ELEMENT INFORMATION (DATE , SOURCE )> <!ELEMENT DATE (#PCDATA )> <!ELEMENT SOURCE (#PCDATA )> <!ELEMENT CATEGORY (NAME | KEY | TYPE | UPDATE )*> <!ATTLIST CATEGORY ACTION (ADD | DELETE | UPDATE ) #REQUIRED > <!ELEMENT DESCRIPTOR (NAME | KEY | UPDATE | OWNER | TYPE )*> <!ATTLIST DESCRIPTOR ACTION (ADD | DELETE | UPDATE ) #REQUIRED > <!ELEMENT OWNER (NAME? , KEY? )> <!ELEMENT KEY (#PCDATA )> <!ELEMENT TYPE (#PCDATA )> <!ELEMENT ITEM (OWNER? , NAMEVALUE* , UPDATE )> <!ATTLIST ITEM ACTION (ADD | DELETE | UPDATE ) #REQUIRED > <!ELEMENT UPDATE (NAME | KEY | NAMEVALUE )*> <!ELEMENT NAMEVALUE (NAME , VALUE )> <!ELEMENT VALUE (#PCDATA )*>
My parser doesn't find the DTD file.
The DTD file defined in the <!DOCTYPE> declaration must be relative to the location of the input XML document. Otherwise, you'll need to use the setBaseURL(url) functions to set the base URL to resolve the relative address of the DTD if the input is coming from an InputStream.
Can I validate an XML file using an external DTD?
You need to include a reference to the applicable DTD in your XML document. Without it there is no way that the parser knows what to validate against. Including the reference is the XML standard way of specifying an external DTD. Otherwise you need to embed the DTD in your XML Document.
Do you have DTD caching? How do I set the DTD using v2 parser for DTD Cache purpose?
Yes, DTD caching is optional and is not enabled automatically.
The method to set the DTD is setDoctype(). Here is an example:
// Test using InputSource parser = new DOMParser(); parser.setErrorStream(System.out); parser.showWarnings(true); FileReader r = new FileReader(args[0]); InputSource inSource = new InputSource(r); inSource.setSystemId(createURL(args[0]).toString()); parser.parseDTD(inSource, args[1]); dtd = (DTD)parser.getDoctype(); r = new FileReader(args[2]); inSource = new InputSource(r); inSource.setSystemId(createURL(args[2]).toString()); parser.setDoctype(dtd); parser.setValidationMode(DTD_validation); parser.parse(inSource);doc = (XMLDocument)parser.getDocument(); doc.print(new PrintWriter(System.out));
How can XML Parser for Java (V2) recognize external DTD's when running from the server. The Java code has been loaded with loadjava and runs in the Oracle9i server process. My XML file has an external DTD reference.
I would like to put all my DTDs in a jar file, so that when the XML Parser needs a DTD it can get it from the jar. The current XML Parser supports a base URL(setBaseURL()), but that just points to a place where all the DTDs are exposed.
The solution involves a combination of:
InputStream is = YourClass.class.getResourceAsStream("/foo/bar/your.dtd");
This will open ./foo/bar/your.dtd in the first relative location on the CLASSPATH that it can be found, including out of your jar if it's in the CLASSPATH.
DOMParser d = new DOMParser(); d.parseDTD(is, "rootelementname"); d.setDoctype(d.getDoctype());
d.parse("yourdoc");
I am exporting Java objects to XML. I can construct a DOM with an XML Document and use its print method to export it. But, I am unable to set the DTD of these documents. I construct a parser, parse the DTD, and then get the DTD via Document doc = parser.getDocument() and DocType dtd = doc.getDocumentType().
How do I set the DTD of the freshly constructed XML Documents to use this one in order to be able to check the correctness of the documents using this DTD at a later time?
Your method of getting the DTD object is correct. However, we do not do any validation while creating the DOM tree using DOM APIs. So setting the DTD in the Document will not help validate the DOM tree that is constructed. The only way to validate an XML file is to parse the XML document using DOMParser or SAXParser.
How do I parse and get a DTD Object separately from parsing my XML document?
The parseDTD() method allows you to parse a DTD file separately and get a DTD object. Here is a sample code to do that:
DOMParser domparser = new DOMParser(); domparser.setValidationMode(DTD_validation); /* parse the DTD file */ domparser.parseDTD(new FileReader(dtdfile)); DTD dtd = domparser.getDocType();
The XML file has a tag like: <xn:subjectcode>. In the DTD, it is defined as <xn:subjectCode>
. When the file is parsed and validated against the DTD, it gives an error: XML-0148: (Error) Invalid element 'xn:subjectcode' in content of 'xn:Resource',...
When I changed the element name to <xn:subjectCode> instead of <xn:subjectcode> it works. Is the parser case-sensitive as far as validation against DTD's go - or is it because, there is a namespace also in the tag definition of the element and when a element is defined along with its namespace, the case-sensitivity comes into effect?
XML is inherently case-sensitive, therefore our parsers enforce case sensitivity in order to be compliant. When you run in non-validation mode only well-formedness counts. However <test></Test> would signal an error even in non-validation mode.
<PAYLOAD> <![CDATA[<?xml version = '1.0' encoding = 'ASCII' standalone = 'no'?> <ADD_PO_003> <CNTROLAREA> <BSR> <VERB value="ADD">ADD</VERB> <NOUN value="PO">PO</NOUN> <REVISION value="003">003</REVISION> </BSR> </CNTROLAREA> </ADD_PO_003>]]> </PAYLOAD>
The CDATA strategy is kind of odd. You won't be able to use a different encoding on the nested XML document included as text inside the CDATA, so having the XML Declaration of the embedded document seems of little value to me. If you don't need the XML Declaration, then why not just embed the message as real elements into the <PAYLOAD> instead of as a text chunk which is what CDATA does for you.
Just do:
String s = YourDocumentObject.selectSingleNode("/OES_MESSAGE/PAYLOAD");
YourParser.parse( new StringReader(s));
where s is the string you got in the previous step.
<xsl:value-of select="/OES_MESSAGE/PAYLOAD" disable-output-escaping="yes"/>
I am having trouble creating a DTD and parsing it using Oracle XML Parser for Java v2. I got the following error when I call DOMParser.parseDTD() function:
Attribute value should start with quote.
Please check my DTD and tell me what's wrong?
<?xml version = "1.0" encoding="UTF-8" ?> <!-- RCS_ID = "$Header: XMLRenderer.dtd 115.0 2000/09/18 03:00:10 fli noship $" --> <!-- RCS_ID_RECORDED = VersionInfo.recordClassVersion(RCS_ID, "oracle.apps.mwa.admin") --> <!-- Copyright: This DTD file is owned by Oracle Mobile Application Server Group. --> <!ELEMENT page (header?,form,footer?) > <!ATTLIST page name CDATA #REQUIRED lov (Y|N) 'N' > <!ELEMENT header EMPTY > <!ATTLIST header name CDATA #REQUIRED title CDATA home (Y|N) 'N' portal (Y|N) 'N' logout (Y|N) 'N' > <!ELEMENT footer EMPTY > <!ATTLIST footer name CDATA #REQUIRED home (Y|N) 'N' portal (Y|N) 'N' logout (Y|N) 'N' copyright (Y|N) 'N' > <!ELEMENT form (styledText|textInput|list|link|menu|submitButton|table|separator)+ > <!ATTLIST form name CDATA #REQUIRED title CDATA type CDATA > <!ELEMENT styledText (#PCDATA) > <!ELEMENT textInput EMPTY > <!ATTLIST textInput name CDATA #REQUIRED prompt CDATA #IMPLIED password (Y|N) 'N' required (Y|N) 'N' maxlength #IMPLIED size #IMPLIED format #IMPLIED default #IMPLIED > <!ELEMENT link (postfield*) > <!ATTLIST link name CDATA #REQUIRED title CDATA #REQUIRED baseurl CDATA #REQUIRED >
Your DTD syntax is not valid. When you declare ATTLIST with CDATA, you must put #REQUIRED, #IMPLIED, #FIXED, "any value", %paramatic_entity. For example, your DTD contains
<!ELEMENT header EMPTY > <!ATTLIST header name CDATA #REQUIRED title CDATA home (Y|N) 'N' portal (Y|N) 'N' logout (Y|N) 'N' >
should change as follows:
<!ELEMENT header EMPTY > <!ATTLIST header name CDATA #REQUIRED title CDATA #REQUIRED <- can replaced by #FIXED, #IMPLIED, or "title1" home (Y|N) 'N' portal (Y|N) 'N' logout (Y|N) 'N' >
Is there a standard extension (other than .xml or .txt) that should be used for external entities which are being referenced in an XML document. These external entities are not complete XML files, but rather only part of an XML file, starting with the <![CDATA[. Mostly they contain HTML, or Javascript code, but may also contain just some plain text. As an example, the external entity is A.txt which is being referenced in the XML document B.xml.
A.txt:
<![CDATA[<!-- This is just an html comment -->]]>
B.xml:
<?xml version="1.0"?> <!DOCTYPE B[ <!ENTITY htmlComment SYSTEM "A.txt"> ]> <B> &htmlComment; </B>
Currently we are using .txt as an extension for all such entities, but need to change that, otherwise the translation team assumes that these files need to get translated, whereas they don't. Is there a standard extension that we should be using?
I marked up your DTD syntax in "red (bold)" in your DTD. The file extension for external entities is unimportant so you can change it to any convenient extension, including *no* extension.:-)
How do I get the number of elements in a particular tag using the parser?
You can use the getElementsByTagName() method that returns a NodeList of all descent elements with a given tag name. You can then find out the number of elements in that NodeList to determine the number of the elements in the particular tag.
How does the XML DOM parser work?
The parser accepts an XML formatted document and constructs in memory a DOM tree based on its structure. It will then check whether the document is well-formed and optionally whether it complies with a DTD. It also provides methods to support DOM Level 1 and 2.
How do I create a node whose value I can set later?
If you check the DOM spec referring to the table discussing the node type, you will find that if you are creating an element node, its nodeValue is to be null and hence cannot be set. However, you can create a text node and append it to the element node. You can put the value in the text node.
How to traverse the XML tree
You can traverse the tree by using the DOM API. Or alternately, you can use the selectNodes() method which takes XPath syntax to navigate through the XML document. selectNodes() is part of oracle.xml.parser.v2.XMLNode.
How do I extract elements from the XML file?
If you're using DOM, the getElementsByTagName() method can be used to get all of the elements in the document.
If I add a DTD to an XML Document, does it validate the DOM tree?
No, we do not do any validation while creating the DOM tree using the DOM APIs. So setting the DTD in the Document will not help in validating the DOM tree that is constructed. The only way to validate an XML file is to parse the XML document using the DOMParser or SAXParser.
How do I efficiently obtain the value of first child node of the element without going through the DOM Tree?
If you do not need the entire tree, use the SAX interface to return the desired data. Since it is event-driven, it does not have to parse the whole document.
How do I create a DocType Node?
The only current way of creating a doctype node is by using the parseDTD functions. For example, emp.dtd has the following DTD:
<!ELEMENT employee (Name, Dept, Title)> <!ELEMENT Name (#PCDATA)> <!ELEMENT Dept (#PCDATA)> <!ELEMENT Title (#PCDATA)>
You can use the following code to create a doctype node:
parser.parseDTD(new FileInputStream(emp.dtd), "employee"); dtd = parser.getDocType();
How do I use the selectNodes() method in XMLNode class?
The selectNodes() method is used in XMLElement and XMLDocument nodes. This method is used to extract contents from the tree/subtree based on the select patterns allowed by XSL. The optional second parameter of selectNodes, is used to resolve Namespace prefixes (return the expanded namespace URL given a prefix). XMLElement implements NSResolver, so it can be sent as the second parameter. XMLElement resolves the prefixes based on the input document. You can implement the NSResolver interface, if you need to override the namespace definitions. The following sample code uses selectNodes
public class SelectNodesTest { public static void main(String[] args) throws Exception { String pattern = "/family/member/text()"; String file = args[0]; if (args.length == 2) pattern = args[1]; DOMParser dp = new DOMParser(); dp.parse(createURL(file)); // Include createURL from DOMSample XMLDocument xd = dp.getDocument(); XMLElement e = (XMLElement) xd.getDocumentElement(); NodeList nl = e.selectNodes(pattern, e); for (int i = 0; i < nl.getLength(); i++) { System.out.println(nl.item(i).getNodeValue()); } } } > java SelectNodesTest family.xml Sarah Bob Joanne Jim > java SelectNodesTest family.xml //member/@memberid m1 m2 m3 m4
I am using SAX to parse an XML document. How does it get the value of the data?
During a SAX parse the value of an element will be the concatenation of the characters reported from after the startElement event to before the corresponding endElement event is called.
Inside the SAXSample program, I did not see any line that explicitly calls setDocumentLocator and some other methods. However, these methods are 'run'. Can you explain when they are called and from where
SAX is a standard interface for event-based XML parsing. The parser reports parsing events directly through callback functions such as setDocumentLocator() and startDocument(). The application, in this case, the SAXSample, implements handlers to deal with the different events. Here is a good place to help you start learning about the event-driven API, SAX: http://www.megginson.com/SAX/index.html
Does the XML Parser DOMParser implement org.xml.sax.Parser interface at all? The documentation says it implements XML Constants and the API does not include that class at all.
You'll want oracle.xml.parser.v2.SAXParser to work with SAX and to have something that implements the org.xml.sax.Parser interface.
I am trying to create a XML file on the fly. I use the NodeFactory to construct a document (createDocument()). I have then setStandalone("no") and setVersion("1.0"). when I try to add a DOCTYPE node via appendChild(new XMLNode("test", Node.DOCUMENT_TYPE_NODE)), I get a ClassCastException. What is the mechanism to add a node of this type? I noticed that the NodeFactory did not have a mechanism for creating a DOCTYPE node.
There is no mechanism to create a new DOCUMENT_TYPE_NODE object via DOM APIs. The only way to get a DTD object is to parse the DTD file or the XML file using the DOMParser, and then use the getDocType() method.
Note that new XMLNode("test",Node.DOCUMENT_TYPE_NODE) does not create a DTDobject. It creates an XMLNode object with the type set to DOCUMENT_TYPE_NODE, which in fact should not be allowed. The ClassCastException is raised because appendChild expects a DTDobject (based on the type).
Also, we do not do any validation while creating the DOM tree using the DOM APIs. So setting the DTD in the Document will not help in validating the DOM tree that is constructed. The only way to validate an XML file is to parse the XML document using DOMParser or SAXParser.
I am using the XML Parser for Java v2. Given a XML document containing the following Calculus Math Jim Green Jack Mary Paul, I want to obtain the value of first child node of whose tag is. I could not find any method that can do that efficiently. The nearest match is method getElementsByTag("Name"), which traverses the entire tree under.
Your best bet, if you do not need the entire tree, is to use the SAX interface to return the desired data. Since it is event driven it does not have to parse the whole document.
Is there an example of XML document generation starting from information contained in simple variables? An example would be: A client fills a Java form and wants to obtain an XML document containing the given data.
Here are two possible interpretations of your question and answers to both. Let's say you have two variables in Java:
String firstname = "Gianfranco"; String lastname = "Pietraforte";
The two ways that come to mind first to get this into an XML document are as follows:
String xml = "<person><first>"+firstname+"</first>"+
"<last>"+lastname+"</last></person";
DOMParser d = new DOMParser(); d.parse( new StringReader(xml)); Document xmldoc = d.getDocument();
Document xmldoc = new XMLDocument(); Element e1 = xmldoc.createElement("person"); xmldoc.appendChild(e1); Element e2 = xmldoc.createElement("first"); e1.appendChild(e2); Text t = xmldoc.createText(firstname); e2.appendChild(t); // and so on
Can you suggest how to get a print out using the DOM API in Java:
<name>macy</name>
I want to print out "may". Don't know which class and what function to use. I was successful in printing "name" on to the console.
For DOM, you need to first realize that <name>macy</name> is actually an element named "name" with a child node (Text Node) of value "macy".
So, you can do the following:
String value = myElement.getFirstChild().getNodeValue();
We have a hash table of key value pairs, how do we build an XML file out of it using the DOM API? We have a hashtablekey = valuename = georgezip = 20000. How do we build this?
<key>value</key><name>george</name><zip>20000</zip>'
Is there a utility to do it automatically?
I have a question regarding our XML parser (v2) implementation. Say if I have the following scenario:
Document doc1 = new XMLDocument(); Element element1 = doc1.creatElement("foo"); Document doc2 = new XMLDocument(); Element element2 = doc2.createElement("bar"); element1.appendChild(element2);
My question is whether or not we should get a DOMException of WRONG_DOCUMENT_ERR on calling the appendChild() routine. This comes to my mind when I look at the XSLSample.java distributed with the XMLparser (v2). Any feedback would be greatly appreciated.
Yes you should get this error, since the owner document of element1 is doc1 while that of element2 is doc2. AppendChild() only works within a single tree and you are dealing with two different ones.
In XSLSample.java that's shipped with xmlparser v2:
DocumentFragment result = processor.processXSL(xsl, xml); // create an output document to hold the result out = new XMLDocument(); // create a dummy document element for the output document Element root = out.createElement("root"); out.appendChild(root); // append the transformed tree to the dummy document element root.appendChild(result);
Nodes root and result are created from different XML Documents, wouldn't this result in the WRONG_DOCUMENT_ERR when we try to append result to root?
This sample uses a document fragment that does not have a root node, therefore there are not two XML documents.
When appending a document fragment to a node, only the child nodes of the document fragment (but not the document fragment itself) is inserted. Wouldn't the parser check the owner document of these child nodes?
A DocumentFragment shouldn't be bound to a 'root' node, since, by definition, a fragment could very well be just a list of nodes. The root node, if any, should be considered a single child. That is, you could for example take all the lines of an Invoice document, and add them into an ProviderOrder document, without taking the invoice itself. How do we create a documentFragment without root? As the XSLT Processor does, so that we can append it to other documents?
I get the following error:
oracle.xml.parser.XMLDOMException: Node cannot be modified while trying to set the value of a newly created node as below: String eName="Mynode"; XMLNode aNode = new XMLNode(eName, Node.ELEMENT_NODE); aNode.setNodeValue(eValue);
How do I create a node whose value I can set later on?
Check the DOM notes where they discuss the node type. You will see that if you are creating an element node, its nodeValue is null and hence cannot be set.
I receive the following error when reading the attached file using SAX (Oracle XML Parser, v.2.0.2.9.0): if character data starts with a whitespace, characters( ) method discards characters that follows whitespace.
Is this a bug or can I force the parser to not discard those characters?
Use XMLParser.setPreserveWhitespace(true) to force the parser to not discard whitespace.
I have an XML string contains the following reference to a DTD, that is physically located in the directory where I start my program. The validating XML parser complains that this file can not be found.
<!DOCTYPE xyz SYSTEM "xyz.dtd" >
What are the rules for locating DTDs on the disk? Can anyone point me to a decent discussion of DOCTYPE attribute descriptions.
Are you parsing an InputStream or a URL? If you are parsing an InputStream the parser doesn't know where that InputStream came from so it cannot find the DTD in the "same directory as the current file". The solution is to setBaseURL()on DOMParser() to give the parser the URL "hint" information to be able to derive the rest when it goes to get the DTD.
Can multiple threads use a single XSLProcessor/XSLStylesheet instance to perform concurrent (at the same time) transformations?
As long as you are processing multiple files with no more than one XSLProcessor/XSLStylesheet instance per XML file you can do this simultaneously using threads. If you take a look at the readme.html file in the bin directory, it describes ORAXSL which has a threads parameter for multi-threaded processing.
Is it safe to use clones of a document in multiple threads? Is the public void setParam(String,String) throws XSLExceptionmethod of Class oracle.xml.parser.v2.XSLStylesheet supported? If no, is there another way to pass parameters at runtime to the XSLT Processor?
If you are copying the global area set up by the constructor to another thread then it should work.
That method is supported since XML Parser release 2.0.2.5.
You have it in your docs, but it is not implemented in the XSLStylesheet class (windows zip edition). First update your zip download file.
public static void serve(Document template, Document data,Element userdata,PrintWriter out) { XMLDocument clone = (XMLDocument)data.cloneNode(true); clone.getDocumentElement().appendChild(userdata.cloneNode(true)); serve(template, clone, out); }
I have some XML-Documents with encoding="iso-8859-1". I am trying to parse these with xmlparser SAX API. In characters (char[], int, int), I would like to output the content in iso-8859-1 (Latin1) too.
With System.out.println() it doesn't work correctly. German umlauts result in '?' in the output stream. Internally ,,,÷,',ý,Ù,>, are stored as 65508,65526,65532,65476,65494,65500,65503 respectively. What do I have to do to get the output in Latin1? Host system here is a SPARC Solaris 2.6.
You cannot use System.out.println(). You need to use an output stream which is encoding aware, for example, OutputStreamWriter.
You can construct an outputstreamwriter
and use the write(char[], int, int) method to:
print.Ex:OutputStreamWriter out = new OutputStreamWriter(System.out, "8859_1"); /* Java enc string for ISO8859-1*/
I'm having trouble with parsing XML stored in NCLOB column using UTF-8 encoding. Here is what I'm running:
The following XML sample that I loaded into the database contains two UTF-8 multi-byte characters:
<?xml version="1.0" encoding="UTF-8"?> <G> <A>GÂ,otingen, Brück_W</A> </G>
G(0xc2, 0x82)otingen, Br(0xc3, 0xbc)ck_W
If I'm not mistaken, both multibyte characters are valid UTF-8 encodings and they are defined in ISO-8859-1 as:
0xC2 LATIN CAPITAL LETTER A WITH CIRCUMFLEX 0xFC LATIN SMALL LETTER U WITH DIAERESIS
I wrote a Java stored function that uses the default connection object to connect to the database, runs a Select query, gets the OracleResultSet, calls the getCLOB method and calls the getAsciiStream() method on the CLOB object. Then it executes the following piece of code to get the XML into a DOM object:
DOMParser parser = new DOMParser(); parser.setPreserveWhitespace(true); parser.parse(istr); // istr getAsciiStreamXMLDocument xmldoc = parser.getDocument();
Before the stored function can do other tasks, this code throws an exception complaining that the above XML contains "Invalid UTF8 encoding".
I loaded the sample XML into the database using the thin JDBC driver. I tried two database configurations with WE8ISO8859P1/WE8ISO8859P1 and WE8ISO8859P1/UTF8 and both showed the same problem.
Yes, the character (0xc2, 0x82) is valid UTF-8. We suspect that the character is distorted when getAsciiStream() is called. Try to use getUnicodeStream() and getBinaryStream() instead of getAsciiStream().
If this does not work, try print out the characters before to make sure that they are not distorted before they are sent to the parser in step: parser.parse(istr)
I've got Japanese data stored in an nvarchar2 field in the database. I have a dynamic SQL procedure that utilizes the PL/SQL web toolkit that allows me to access data via OAS and a browser. This procedure uses the XML Parser to correctly format the result set in XML before returning it to the browser.
My problem is that the Japanese data is returned and displayed on the browser as upside down question marks. Is there anything I can do so that this data is correctly returned and displayed as Kanji?
Unfortunately, Java and XML default character set is UTF8 while I haven't heard of any UTF8 OS nor people using it as in their database and people writing their web pages in UTF8. All this means is that you have a character code conversion problem. Answer to your last question is 'yes'. We do have both PL/SQL and Java XML parsers working in Japanese. Unfortunately, we cannot provide a simple solution that will fit in this space.
This is my XML Document:
Documento de Prueba de gestin de contenidos. Roberto P0/00rez Lita
This is the way in which I parse the document:
DOMParser parser=new DOMParser(); parser.setPreserveWhitespace(true); parser.setErrorStream(System.err); parser.setValidationMode(false); parser.showWarnings(true); parser.parse ( new FileInputStream(new File("PruebaA3Ingles.xml")));
I get the following error:
XML-0231 : (Error) Encoding 'UTF-16' is not currently supported
I am using the XML Parser for Java V2_0_2_5 and I am confused because the documentation says that the UTF-16 encoding is supported in this version of the Parser. Does anybody know how can I parse documents containing spanish accents?
Oracle just uploaded a new release of V2 Parser. It should support UTF-16.Yet, other utilities still have some problems with UTF-16 encoding.
I need to store accented characters in my XML documents. If I manually add an accented character e.g. é, to my XML file and then attempt to parse the XML doc. with Oracle's XML Parser for Java the Parser throws the following exception:
'Invalid UTF-8 encoding'
Here's my encoding declaration in my xml header:
<?xml version="1.0" encoding="UTF-8"?>
Aside: If I specify UTF-16 as the default encoding the Oracle XML Parser for Java states that UTF-16 is not currently supported. From within my Java program if I define a Java String object as follows:
String name = "éééé";
and programmatically generate an XML document and save it to file then the é character is correctly written out to file. Can you tell me how I can successfully read in character data consisting of accented characters. I know that I can read in accented characters once I represent them in their HEX or Decimal format within the XML document, for example:
é
but I'd prefer not to do this.
You need to set the encoding based on the character set you were using when you created the xml file - I ran into this problem & solved it by setting the encoding to iso-8859-1 (western european ascii) - you may need to use something different depending on the tool and/or operating system you were using.
If you explicitly set the encoding to UTF-8 (or do not specify it at all), the parser interprets your accented character (which has an ascii value > 127) as the first byte of a UTF-8 multi-byte sequence. If the subsequent bytes do not form a valid UTF-8 sequence, you get this error.
This error just means that your editor is not saving the file with UTF-8 encoding. For example, it might be saving it with ISO-8859-1 encoding. Remember that the encoding is a particular scheme used to write the Unicode character number representation to disk. Just adding the string to the top of the document like:
<?xml version="1.0" encoding="UTF-8"?>
does not cause your *editor* to write out the bytes representing the file to disk using UTF-8 encoding. I believe Notepad uses UTF-8, so you might try that.
I am trying to add an XMLDocument as a child to an existing element. Here's an example:
import org.w3c.dom.*; import java.util.*; import java.io.*; import java.net.*; import oracle.xml.parser.v2.*; public class ggg {public static void main (String [] args) throws Exception { new ggg().doWork();; public void doWork() throws Exception {XMLDocument doc1 = new XMLDocument(); Element root1=doc1.createElement("root1"); XMLDocument doc2= new XMLDocument(); Element root2=doc2.createElement("root2"); root1.appendChild(root2); doc1.print(System.out);};};
This reports:
D:\Temp\Oracle\sample>c:\jdk1.2.2\bin\javac -classpath D:\Temp\Oracle\lib\xmlparserv2.jar;. ggg.javaD:\Temp\Oracle\sample>c:\jdk1.2.2\bin\java -classpath D:\Temp\Oracle\lib\xmlparserv2.jar;. gggException in thread "main" java.lang.NullPointerException at oracle.xml.parser.v2.XMLDOMException.(XMLDOMException.java:67) at oracle.xml.parser.v2.XMLNode.checkDocument(XMLNode.java:919) at oracle.xml.parser.v2.XMLNode.appendChild(XMLNode.java, Compiled Code) at oracle.xml.parser.v2.XMLNode.appendChild(XMLNode.java:494) at ggg.doWork(ggg.java:20) at ggg.main(ggg.java:12)
The following works for me:
DocumentFragment rootNode = new XMLDocumentFragment(); DOMParser d = new DOMParser(); d.parse("http://.../stuff.xml"); Document doc = d.getDocument(); Element e = doc.getDocumentElement(); // Important to remove it from the first doc // before adding it to the other doc. doc.removeChild(e); rootNode.appendChild(e);
You need to use the DocumentFragment class to do this as a document cannot have more than one root.
Actually, isn't this specifically a problem with appending a node created in another document, since all nodes contain a reference to the document they are created in? While Document Fragmentsolves this, it isn't a more than one root problem, is it? Is there a quick or easy way to convert a com.w3c.dom.Document to org.w3c.dom.DocumentFragment?
I have this piece of code:
XSLStylesheet XSLProcessorStylesheet = new XSLStylesheet(XSLProcessorDoc, XSLProcessorURL); XSLStylesheet XSLRendererStylesheet = new XSLStylesheet(XSLRendererDoc, XSLRendererURL); XSLProcessor processor = new XSLProcessor(); // configure the processorprocessor.showWarnings(true); processor.setErrorStream(System.err); XMLDocumentFragment processedXML = processor.processXSL(XSLProcessorStylesheet, XMLInputDoc); XMLDocumentFragment renderedXML = processor.processXSL(XSLRendererStylesheet, processedXML); Document resultXML = new XMLDocument(); resultXML.appendChild(renderedXML);
The last line causes Exception in thread "main" oracle.xml.parser.v2.
XMLDOMException: Node of this type cannot be added.
Do I have to create a root element _every time_, even if I know that the resulting DocumentFragment is a well formed XML Document (and of course has only one root element!)?
It happens, as you have guessed, because a Fragment can have more than one "root" element (for lack of a better term). In order to work around this, use the Node functions to extract the one root element from your fragment and cast it into an
I am uninstalling a version of XML Parser and installing a newer version. How do I do that? I know that there is something like dropjava, but still there are other packages which are loaded into the schema. I want to clean out the earlier version and install the new version in a clean manner.
You'll need to write SQL to write SQL based on the USER_OBJECTS table where:
SELECT 'drop java class '''| | dbms_java.longname(object_ name)| |''';
from user_objects where
OBJECT_TYPE = 'JAVA CLASS'and DBMS_JAVA.LONGNAME(OBJECT_NAME) LIKE 'oracle/xml/parser/%'
This will spew out a set of DROP JAVA CLASS command which you can capture in a file using SQL*Plus': SPOOL somefilenamecommand.
Then run that spool file as a SQL script and all the right classes will be dropped.
I'm getting an error message when I try installing XMLPARSER:
loadjava -user username/manager -r -v xmlparserv2.jar Error: Exception in thread "main" java.lang.NoClassDefFounderr:
oracle/jdbc/driver/OracleDriver at oracle.aurora.server.tools..
This is a failure to find the JDBC classes111.zip in your classpath. The loadjava utility connects to the database to load your classes using the JDBC driver.
I checked 'loadjava' and the path to classes111.zip is
<ORACLE_HOME>/jdbc/lib/classes111.zip
In version 8.1.6, classes111.zip resides in:
<ORACLE_HOME/jdbc/admin
What does an XML Parser do?
The parser accepts any XML document giving you a tree-based API (DOM) to access or modify the document's elements and attributes as well as an event-API (SAX) that provides a listener to be registered and report specific elements or attributes and other document events.
How do I convert XML files into HTML files?
You need to create an XSL stylesheet to render your XML into HTML. You can start with an HTML document in your desired format and populated with dummy data. Then you can replace this data with the XSLT commands that will populate the HTML with data from the XML document completing your stylesheet.
Does the XML Parser v2 validate against an XML Schema?
Yes. It supports both validating and non-validating modes. XML Schema is still under the development W3C XML Schema committee and is supported by Oracle9i. Currently, XML Parser for Java supports validating, non-validating, partial validating DTDs and XML Schemas with the modes: non-validating mode, DTD validating mode, partial validation mode, and schema validation mode.
How do I include binary data in an XML document?
There is no way to directly include binary data within the document; however, there are two ways to work around this:
What is the XML Schema?
XML Schema is a W3C XML standards effort to bring the concept of data types to XML documents and in the process replace the syntax of DTDs to one based on XML. For more details, check out http://www.w3.org/TR/xmlschema-1/ and http://www.w3.org/TR/xmlschema-2/. XML Schema is supported in Oracle9i and higher.
Does Oracle participate in defining the XML/XSL standard?
Oracle has representatives participating actively in the following 3C Working Groups related to XML/XSL: XML Schema, XML Query, XSL, XLink/XPointer, XML Infoset, DOM and XML Core.
How do I determine the version number of the XDK toolkit that I downloaded?
You can find out the full version number by looking at the readme.html file included in the archive and linked off of the Release Notes page.
How do I insert these characters in the XML documents: >,<,>=, and <=?
You need to use the entities < for < and > for >.
Is support for Namespaces and Schema included?
The current XML parsers support Namespaces. Schema support is provided in Oracle9i and higher.
Can I use JDK 1.1.x with XML Parser v2 for Java?
v2 of XML Parser for Java has nothing to do with Java2. It is simply a designation that indicates that it is not backwards compatible with the v1 Parser and that it includes XSLT support. The v2 parser will work fine with JDK 1.1.x.
I have a set of records say 100, I am showing 10 at a time, now on each column name I have made a link, on the click of the same, I want to sort the data in the page alone, based on that column. How to go about it?
It depends on how you are going about. If you are writing for IE5 alone and receiving XML data, you could just use MS's XSL to sort data in a page.If you are writing for other browser and browsers are getting data as HTML, then you have to have a sort parameter in XSQL script and use it in ORDER BY clause. Just passed it along with skip-rows parameter.
Do I need Oracle9i to run the XML Parser for Java?
XML Parser for Java can be used with any of the supported version JavaVMs. The only difference with 9i is that you can load it into the database and use JServer, which is an internal JVM. For other database versions or servers, you simply run it in an external JVM and as necessary connect to a database through JDBC.
Is it possible to dynamically set the encodings in the XML file?
No, you need to include the proper encoding declaration in your document as per the specification. You cannot use setEncoding() to set the encoding for you input document. SetEncoding() is used with oracle.xml.parser.v2.XMLDocument to set the correct encoding for the printing.
How do I parse a string?
We do not currently have any method that can directly parse an XML document contained within a String. You would need to convert the String into an InputStream or InputSource before parsing. An easy way is to create a ByteArrayInputStream using the bytes in the String.
How do I display my XML document?
If you are using IE5 as your browser you can display the XML document directly. Otherwise, you can use our XSLT Processor in v2 of the parser to create the HTML document using an XSL Stylesheet. The Oracle XML Transviewer bean also allows you to view your XML document.
I am having problems using System.out.println() with special character encoding.
You can't use System.out.println(). You need to use an output stream which is encoding aware (Ex.OutputStreamWriter). You can construct an OutputStreamWriter and use the write(char[ ], int, int) method to print.
/* Example */ OutputStreamWriter out = new OutputStreamWriter (System.out, "8859_1"); /* Java enc string for ISO8859-1*/
How do I to get ampersand from character data?
You cannot have "raw" ampersands in XML data. You need to use the entity, & instead. This is defined in the XML standard.
I have a tag in XML <COMPANYNAME>
When we try to use "A&B", the parser gives an error with invalid character. How do we use special characters when parsing companyname tag? We are using the Oracle XML Parser for C.
You have to represent literal...
& as &
< as <
I think you may want to use special characters as part of XML name. For example: <A&B>abc</A&B>
If this is the case, using name entity doesn't solve the problem. According to XML 1.0 spec(http://www.w3.org/TR/2000/REC-xml-20001006), NameChar and Name are defined as follows:
[4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar |Extender
[5] Name ::= (Letter | '_' | ':') (NameChar)*
To answer your question, special character such as '&', '$', '#',... are not allowed to be used as NameChar. Hence, if you are creating XML document from scratch, you can use a workaround by using only valid NameChars. For example, <A_B>, <AB>, <A_AND_B>...
They are still readable.
If you are generating XML from external data sources such as database tables, then this is a problem. XML 1.0 does not address it.
In Oracle, the new type, XMLType, will help address this problem by offering a function which maps SQL names to XML names. This will address this problem at the application level. The SQL to XML name mapping function will escape invalid XML NameChar in the format of _XHHHH_ where HHHH is a Unicode value of the invalid character. For example, table name "V$SESSION" will be mapped to XML name "V_X0024_SESSION".
At last, escaping invalid characters is a hack to give people a way to serialize names so that they can reload them somewhere else.
How do I parse XML from data of type String?
Check out the following example:
/* xmlDoc is a String of xml */ byte aByteArr [] = xmlDoc.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream (aByteArr, 0, aByteArr.length); domParser.parse(bais);
How do I extract data from an XML document into type String?
Here is an example to do that:
XMLDocument Your Document; /* Parse and Make Mods */ : StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); YourDocument.print(pw); String YourDocInString = sw.toString();
Does XML Parser for Java support Disabling Output Escaping?
Yes, since version 2.022, the parser provides an option to xsl:text to disable output escaping.
Is the XML Parser for Java only available for use with Oracle 9i? Is it possible to use with Oracle 8.0.5
The XML Parser for Java can be used with any of the supported version JavaVMs. The only difference with Oracle9i is that you can load it into the database and use JServer which is an internal VM. For 8.0.5 you simple run it externally and connect through JDBC.
We need to be able to read (and separate) several XML documents as a single string. One solution would be to delimit these documents using some (programatically generated) special character that we know for sure can never occur inside an xml document. The individual documents can then be easily tokenized and extracted/parsed as required.
Has any one else done this before? Any suggestions for what character can be used as the delimiter (for instance can characters in the range #x0-#x8 ever occur inside an xml document?)
As far as legality is concerned and you limit it to 8-bit, #x0-#x8; #xB, #xC, #xE, and #xF are not legal. HOWEVER this assumes that you preprocess the doc and not depend upon exceptions as not ALL parsers reject ALL illegal characters.
Element, which you then append to the Document.
How can I merge two XML Documents?
This is not possible with the current DOM specification. DOM2 specification may address this.
You can use a DOM-approach or an XSLT-based approach to accomplish this. If you use DOM, then you'll have to remove the node from one document before you append it into the other document to avoid ownership errors.
Here's an example of the XSL-based approach. Assume your two XML source files are:
demo1.xml
<messages> <msg> <key>AAA</key> <num>01001</num> </msg> <msg> <key>BBB</key> <num>01011</num> </msg> </messages>
demo2.xml
<messages> <msg> <key>AAA</key> <text>This is a Message</text> </msg> <msg> <key>BBB</key> <text>This is another Message</text> </msg> </messages>
Here is a stylesheet the "joins" demo1.xml to demo2.xml based on matching the "<key>" values.
demomerge.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes"/> <xsl:variable name="doc2" select="document('demo2.xml')"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="msg"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> <text><xsl:value-of select="$doc2/messages/msg[key=current()/key]/text"/> </text> </xsl:copy> </xsl:template> </xsl:stylesheet>
If you use the command-line "oraxsl
" to test this out, you would do:
$ oraxsl demo1.xml demomerge.xsl
And you'll get the merged result of:
<messages> <msg> <key>AAA</key> <num>01001</num> <text>This is a Message</text> </msg> <msg> <key>BBB</key> <num>01011</num> <text>This is another Message</text> </msg></messages>
Obviously not as efficient for larger-sized files as an equivalent database "join" between two tables, but this illustrates the technique if you only have XML files to work with.Error: Cannot Find Class
I am using SAX to parse an XML document. How I can get the value of a particular tag? For example, Java. How do I get the value for title? I know there are startElement, endElement, and characters methods.
During a SAX parse the value of an element will be the concatenation of the characters reported from after startElement to before the corresponding endElement is called.
We are using Oracle XML Parser for Java on NT 4.0. When we are parsing an XML document with an external DTD we get the following error:
<!DOCTYPE listsamplereceipt SYSTEM "file:/E:/ORACLE/utl_file_dir/dadm/ae.dtd"> java.lang.SecurityExceptionat oracle.aurora.rdbms.SecurityManagerImpl.checkFile(SecurityManagerImpl.java)at oracle.aurora.rdbms.SecurityManagerImpl.checkRead(SecurityManagerImpl.java)at java.io.FileInputStream.<init>(FileInputStream.java)at java.io.FileInputStream.<init>(FileInputStream.java)at sun.net.www.MimeTable.load(MimeTable.java)at sun.net.www.MimeTable.<init>(MimeTable.java)at sun.net.www.MimeTable.getDefaultTable(MimeTable.java)at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java)at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection. java)at java.net.URL.openStream(URL.java)at oracle.xml.parser.v2.XMLReader.openURL(XMLReader.java:2313)at oracle.xml.parser.v2.XMLReader.pushXMLReader(XMLReader.java:176)at ...
What is causing this?
Grant the JAVASYSPRIV role to your user running this code to allow it to open the external file/URL.
Shouldn't file b.xml be in the format:
<?xml version="1.0" ?> <b> <ok/> </b>
Does Oracle XML Parser come with a utility to parse an XML file and see the parsed output?
The following works, both browsing it in IE5 as well as parsing it with Oracle XML Parser v2. Even though I'm sure it works fine in Oracle XML Parser 1.0, you should be using the latest parser version as it is faster than v1.
File: a.xml
<?xml version="1.0" ?> <!DOCTYPE a [<!ENTITY b SYSTEM "b.xml">]> <a>&b;</a>
File: b.xml
<ok/>
When I browse/parse a.xml I get the following:
<a> <ok/> </a>
import oracle.xml.parser.*; import java.io.*; import java.net.*; import org.w3c.dom.*; import org.xml.sax.*; /* ** Simple Example of Parsing an XML File from a String ** and, if successful, printing the results. ** ** Usage: java ParseXMLFromString <hello><world/></hello> */ public class ParseXMLFromString { public static void main( String[] arg ) throws IOException, SAXException { String theStringToParse = "<?xml version='1.0'?>"+ "<hello>"+ " <world/>"+ "</hello>"; XMLDocument theXMLDoc = parseString( theStringToParse ); // Print the document out to standard out theXMLDoc.print(System.out); } public static XMLDocument parseString( String xmlString ) throws IOException, SAXException { XMLDocument theXMLDoc = null; // Create an oracle.xml.parser.v2.DOMParser to parse the document. XMLParser theParser = new XMLParser(); // Open an input stream on the string ByteArrayInputStream theStream = new ByteArrayInputStream( xmlString.getBytes() ); // Set the parser to work in non-Validating mode theParser.setValidationMode(DTD_validation); try { // Parse the document from the InputStream theParser.parse( theStream ); // Get the parsed XML Document from the parser theXMLDoc = theParser.getDocument(); } catch (SAXParseException s) { System.out.println(xmlError(s)); throw s; } return theXMLDoc; } private static String xmlError(SAXParseException s) { int lineNum = s.getLineNumber(); int colNum = s.getColumnNumber(); String file = s.getSystemId(); String err = s.getMessage(); return "XML parse error in file " + file + "\n" + "at line " + lineNum + ", character " + colNum + "\n" + err; } }
From where I can download oracle.xml.parser.v2.OraXSL
?
It's part of our integrated XML Parser for Java V2 release. Our XML Parser, DOM, XPath implementation, and XSLT engine are nicely integrated into a single, cooperating package. http://otn.oracle.com/tech/xml/xdk_java/
We are interested in using the Oracle database to primarily store XML. We would like to parse incoming XML documents and store data and tags in the database. We are concerned about the following two aspects of XML in Oracle:
Relational mapping of parsed XML data. We prefer hierarchical storage of parsed XML data. Is this a valid concern? Will XMLType in Oracle9i address this concern?
A lack of an "Ambiguous Content Mode" in the Oracle Parser for Java is limiting to our business. Are there plans to add an "Ambiguous Content Mode" to the Oracle Parser for Java?
Lots of customers initially have this concern. It depends on what kind of XML data you are storing. If you are storing XML datagrams that are really just encoding of relational information, a purchase order, for example, then you will get much better performance and much better query flexibility (via SQL) to store the data contained in the XML documents in relational tables, then on-demand reproduce an XML format when any particular data is needed to be extracted.
If you are storing documents that are more mixed-content, like legal proceeding, chapters of a book, reference manuals, and so on. Then storing them in chunks and searching them using Oracle Text's XML search capabilities is the best bet.
The book, "Building Oracle XML Applications" by Steve Muench, covers both of these storage and searching techniques with lots of examples.
For the second point, Oracle's XML Parser implements all the XML 1.0 standard, and the XML 1.0 standard requires XML documents to have unambiguous content models, so there's no way a compliant XML 1.0 parser can implement ambiguous content models. See: http://www.xml.com/axml/target.html#determinism
I don't know what is wrong here. This is my news_xsl.xsl file:
<?xml version ="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <HTML> <HEAD> <TITLE> Sample Form </TITLE> </HEAD> <BODY> <FORM> <input type="text" name="country" size="15"> </FORM> </BODY> </HTML> </xsl:template> </xsl:stylesheet>
ERROR:End tag 'FORM' does not match the start tag 'input'. Line 14, Position 12 </FORM>- ----------^news.xml <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="news_xsl.xsl"?> <GREETING/>
Unlike in HTML, in XML you must know that every opening/starting tag should have an ending tag. So even the input that you are giving should have a matching ending tag, so you should modify your script like this:
<FORM> <input type="text" name="country" size="15"> </input> </FORM>
OR
<FORM> <input type="text" name="country" size="15"/> </FORM>
And also always remember, in XML the tags are case sensitive, unlike in HTML. So be careful.
Is the output method "html" supported in the recent version of the XML/XSL parser? I was trying to use the <BR> tag with the <xsl utput method="xml"/> declaration but I got an XSLException error message indicating a not well-formed XML document. Then I tried the following output method declaration: <xsl utput method="html"/>but I got the same result.
Here's a simple XSL stylesheet I was using:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl utput method="html"/> <xsl:template match="/"> <HTML> <HEAD></HEAD> <BODY> <P> Blah blah<BR> More blah blah<BR> </P> </BODY> </HTML> </xsl:template>
How do I use a not well-formed tag (like <IMG>, <BR>, etc.) in an XSL stylesheet?
We fully support all options of <xsl utput> The problem here is that your XSL Stylesheet must be a well-formed XML document, so everywhere you are using the <BR> element, you need to use <BR/> instead.<xsl utput method="html"/> requests that when the XSLT Engine *writes out* the result of your transformation, is a proper HTML document. What the XSLT engine reads *in* must be well-formed XML.
Sorry for jumping in on this thread, but I have a question regarding your reply. I have an XSL stylesheet that preforms XML to HTML conversion. Everything works correctly with the exception of those HTML tags that are not well formed. Using your example if I have something like:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> ...... <input type="text" name="{NAME}" size="{DISPLAY_LENGTH}" maxlength="{LENGTH}"> </input> ...... </xsl:stylesheet>
It would render HTML in the format of
<HTML>......<input type="text" name="in1" size="10" maxlength="20"/> ...... </HTML>
While IE can handle this Netscape can not. Is there anyway to generate completely cross browser compliant HTML with XSL?
If you are seeing:
<input ... />
instead of:
<input>
Then you are likely using the incorrect way of calling XSLProcessor.processXSL() since it appear that it's not doing the HTML output for you. Use:
void processXSL(style,sourceDoc,PrintWriter)
instead of:
DocumentFragment processXSL(style,sourceDoc)
and it will work correctly.
I'm using <xsl utput method="html" encoding="iso-8859-1" indent = "no" />. Is it possible to prevent XSLT from outputting <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> in the head because Netscape 4.0 has difficulties with this statement. It renders the page twice.
The XSLT 1.0 Recommendation says in Section 16.2 ("HTML Output Method")...If there is a HEAD element, then the html output method should add a META element immediately after the start-tag of the HEAD element specifying the character encoding actually used.
For example:
<HEAD><META http-equiv="Content-Type" content="text/html; charset=EUC-JP">.
So any XSLT 1.0-compliant engine needs to add this.
Netscape 4.0 has following bug:
When Mozilla hits the meta-encoding tag it stops rendering the page and does a refresh. So you experience this annoying flickering. So I probably have to do a replacement in the servlets Outputstream, but I don't like doing so. Are there any alternatives.
Only alternatives I can think of are:
Neither is pretty, but either one might provide a workaround.
Where can I find more info on the XSL error messages. I get the error XSL-1900, exception occurred. What does this mean? How can I find out what caused the exception?
If you are using Java, you could write Exception routines to trap errors.Using tools such as JDeveloper also helps.
The error messages of our components are usually more legible. XSL-1900 indicates possible internal error or incorrect usage.
I am trying to generate an HTML form for inputting data using column names from the user_tab_columns table and the following XSL code:
<xsl:template match="ROW"> <xsl:value-of select="COLUMN_NAME"/> <: lt;INPUT NAME="<xsl:value-of select="COLUMN_NAME"/>> </xsl:template>
although 'gt;' is generated as '>' 'lt;' is generated as '#60;'. How do I generate the "<" character?
Using the following:
<xsl:text disable-output-escaping="yes">entity-reference</xsl:text>
does what I need.
I can't seem to display HTML from XML.In my XML file I store the HTML snippet in an XML tag:
<PRE> <body.htmlcontent> <<table width="540" border="0" cellpadding="0" cellspacing="0"><tr><td><font face="Helvetica, Arial" size="2"><!-- STILL IMAGE GOES HERE --><img src="graphics/imagegoeshere.jpg" width="200" height="175" align="right" vspace="0" hspace="7"><!-- END STILL IMAGE TAG --><!-- CITY OR TOWN NAME GOES FIRST FOLLOWED BY TWO LETTER STATE ABBREVIATION --><b>City, state abbreviation</b> - <!-- CITY OR TOWN NAME ENDS HERE --><!-- STORY TEXT STARTS HERE -->Story text goes here.. <!-- STORY TEXT ENDS HERE --></font></td></tr></table> </body.htmlcontent> </PRE>
I use the following in my XSL:
<xsl:value-of select="body.HTMLcontent" disable-output-escaping="yes"/>
However, the HTML output
<PRE><</PRE>
is still outputted and all of the HTML tags are displayed in the browser. How do I display the HTML properly?
That doesn't look right. All of the < are #60; in the code with an ampersand in front of them. They are still that way when they are displayed in the browser.
Even more confusing is that it works with oraxsl, but not with XSLSample.java.
This makes sense. Here's why:
The former supports <xsl:output> and all options related to writing out output that might not be valid XML (including the disable output escaping). The latter is pure XML-to-XML tree returned, so no <xsl:output> or disabled escaping can be used since nothing's being output, just a DOM tree fragment of the result is being returned.
Is there any site which has good examples or small tutorials on XSLT?
This site is an evolving tutorial on lots of different XML/XSLT/XPath-related subjects:
http://zvon.vscht.cz/ZvonHTML/Zvon/zvonTutorials_en.html
I am in the process of trying to convert an xml document from one format to another by means of an xsl (or xslt) stylesheet. Before incorporating it into my java code, I tried testing the transformation from the command line:
> java oracle.xml.parser.v2.oraxsl jwnemp.xml jwnemp.xsl newjwnemp.xml
The problem is that instead of returning the transformed xml file (newjwnemp.xml), the above command just returns a file with the xsl code from jwnemp.xsl in it. I cannot figure out why this is occurring. I have attached the two input files.
<?xml version="1.0"?> <employee_data> <employee_row> <employee_number>7950</employee_number> <employee_name>CLINTON</employee_name> <employee_title>PRESIDENT</employee_title> <manager>1111</manager> <date_of_hire>20-JAN-93</date_of_hire> <salary>125000</salary> <commission>1000</commission> <department_number>10</department_number> </employee_row> </employee_data> <?xml version='1.0'?> <ROWSET xmlns:xsl="HTTP://www.w3.org/1999/XSL/Transform"> <xsl:for-each select="employee_data/employee_row"> <ROW> <EMPNO><xsl:value-of select="employee_number"/></EMPNO> <ENAME><xsl:value-of select="employee_name"/></ENAME> <JOB><xsl:value-of select="employee_title"/></JOB> <MGR><xsl:value-of select="manager"/></MGR> <HIREDATE><xsl:value-of select="date_of_hire"/></HIREDATE> <SAL><xsl:value-of select="salary"/></SAL> <COMM><xsl:value-of select="commission"/></COMM> <DEPTNO><xsl:value-of select="department_number"/></DEPTNO> </ROW> </xsl:for-each> </ROWSET>
This is occurring nearly 100%-likely because you have the wrong XSL namespace uri for your xmlns:xsl="..." namespace declaration.
If you use: xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
everything works.
If you use xmlns:xsl="-- any other string here --"
If will do what you're seeing.
I cannot find anything about using XSL. Can you help? I would like to get an XML and XSL file to show my company what they can expect from this technology. XML alone is not very impressive for users.
A pretty good starting place for XSL is the following page:
http://metalab.unc.edu/xml/books/bible/updates/14.html
It shows pretty much in english what the gist of xsl is. XSL isn't really anything more than an XML file, so I don't think that it will be anymore impressive to show to a customer. There's also the main website for XSL which is: http://www.w3.org/style/XSL/
I recall seeing discussions about XSLProcessor producing more than one result from one XML and XSL. How can this can be achieved?
XML Parser 2.0.2.8 supports <ora:output> to handle this.
Can any one suggest good books for learning about XML/XSL?
There are many excellent articles, white papers, and books that describe all facets of XML technology. Many of these are available on the world wide web. The following are some of the most useful resources we have found:
I would like to know if there are any release plans for the XML Parser or an XDK for HP/UX platform.
HP-UX ports for our C/C++ Parser as well as our C++ Class Generator are available. Look for an announcement on http://technet.oracle.com
Can we compress XML documents when saving them to the database as a CLOB? If they are compressed, what is the implication of using Oracle Text (intermedia) against the documents? We have large XML documents that go up to 1 megabyte and they need to be minimized.
The main requirement is to save cost in terms of disk storage as the XML documents stored are history information (more of a datawarehouse environment). We could save a lot of disk space if we could compress the documents before storage. The searching capability is only secondary, but a big plus.
XDK for Java support a compression mechanism in Oracle9i. It supports streaming compression/uncompression. The compression is achieved by removing the markup in the XML Document. The initial version does not support searching the compressed data. This is planned for a future release.
If you want to store and search your XML docs, Oracle Text can handle this. I am sure that the size of individual document is not a problem for Oracle Text.
If you want to compress the 1megabyte docs for saving disk space/costs, Oracle Text will not be able to automatically handle a compressed XML document.
Try looking at XMLZip:
http://www.xmls.com/resources/xmlzip.xml?id=resources_xmlzip
My only concern would be the performance hit to do the uncompression. If you are just worried about transmitting the XML from client to server or vice versa, then HTTP compression could be easier.
I would like to generate an XML-document based on 2 tables with a master detail relationship between them. Suppose I have two tables :
And a master detail relationship between PARENT and CHILD. How can I generate a document that looks like this ?
<?xml version = '1.0'?> <ROWSET> <ROW num="1"> <parent_name>Bill</parent_name> <child_name>Child 1 of 2</child_name> <child_name>Child 2 of 2</child_name> </ROW> <ROW num="2"> <parent_name>Larry</parent_name> <child_name>Only one child</child_name> </ROW> </ROWSET>
You can (should) use an object view to generate an XML document from a master-detail structure. In your case:
create type child_type is object (child_name <data type child_name>) ; / create type child_type_nst is table of child_type ; / create view parent_child as select p.parent_name , cast ( multiset ( select c.child_name from child c where c.parent_id = p.id ) as child_type_nst ) child_type from parent p /
A SELECT * FROM parent_child, processed by an SQL to XML utility would generate a valid XML document for your parent child relationship. The structure would not look like the one you have presented, though. It would be like:
<?xml version = '1.0'?> <ROWSET> <ROW num="1"> <PARENT_NAME>Bill</PARENT_NAME> <CHILD_TYPE> <CHILD_TYPE_ITEM> <CHILD_NAME>Child 1 of 2</CHILD_NAME> </CHILD_TYPE_ITEM> <CHILD_TYPE_ITEM> <CHILD_NAME>Child 2 of 2</CHILD_NAME> </CHILD_TYPE_ITEM> </CHILD_TYPE> </ROW> <ROW num="2"> <PARENT_NAME>Larry</PARENT_NAME> <CHILD_TYPE> <CHILD_TYPE_ITEM> <CHILD_NAME>Only one child</CHILD_NAME> </CHILD_TYPE_ITEM> </CHILD_TYPE> </ROW> </ROWSET>
|
Copyright © 1996-2001, Oracle Corporation. All Rights Reserved. |
|