Parsing an XML Tree Document

To parse an XML document into a document object tree

  1. Create a JadeXMLDocument object.

  2. Call the JadeXMLDocument class parseString or parseFile method.

Objects in the tree are created as transient objects by default because that is generally the way they are used. However, you can create them as shared transient objects (or persistent objects), if required, in which case transaction control is your responsibility.

JadeXMLNode objects created with the add<node-type> (rather than the add<node-object>) methods are created with the same persistence as the node to which they are added.

The following example parses a simple document string into a tree of transient objects:

vars
    doc: JadeXMLDocument;
begin
    create doc;
    doc.parseString('<employee>John Smith</employee>');
end;

For an XML string to parse correctly, if an element is prefixed by a namespace, the namespace must be specified. The following line of code results in a parser exception.

doc.parseString('<wx:employee>John Smith</wx:employee>');

To avoid the exception, change the opening tag to the following.

<wx:employee xmlns:wx="http://required_namespace">

To create a tree of persistent objects, you first need to define your own subclasses of all the JadeXMLNode concrete classes. You use the JadeXMLDocumentParser class to map your classes to the JadeXMLNode classes so that node instances will be created in your user map files. You then call the parseDocumentFile or parseDocumentString method. The following example parses a string into a tree of persistent objects.

vars
    parser: JadeXMLDocumentParser;
    doc: MyDocument;
begin
    beginTransaction;
    create parser;
    parser.setClassMapping(JadeXMLElement, MyElement);
    parser.setClassMapping(JadeXMLAttribute, MyAttribute);
    parser.setClassMapping(JadeXMLText, MyText);
    parser.setClassMapping(JadeXMLComment, MyComment);
    parser.setClassMapping(JadeXMLCDATA, MyCDATA);
    parser.setClassMapping(JadeXMLProcessingInstruction,
                           MyProcessingInstruction);
    parser.setClassMapping(JadeXMLDocumentType, MyDocumentType);
    create doc persistent;
    parser.parseDocumentString(doc, '<employee>John Smith</employee>');
    commitTransaction;
end;