Creating XML Tree Documents

Methods defined in JadeXMLNode subclasses enable you to create new documents from scratch. You can create node objects, add them to an XML document, and set their textual content.

The following example illustrates the use of these methods.

create1();
vars
    doc : JadeXMLDocument;
    library, book, title, author : JadeXMLElement;
begin
    create doc;
    doc.addComment('An example XML document');
    library := doc.addElement('library');
    book := library.addElement('book');
    book.addAttribute('isbn', '0-246-13655-3');
    title := book.addElement('title');
    title.setText('Mystery');
    author := book.addElement('author');
    author.setText('Peter Straub');
    book := library.addElement('book');
    book.addAttribute('isbn', '1-876590-17-3');
    title := book.addElement('title');
    title.setText('False Memory');
    author := book.addElement('author');
    author.setText('Dean Koontz');
    write doc.writeToString;
    delete doc;
end;

The following example shows the output from the create1 method in the previous example.

<?xml version="1.0"?>
<!--An example XML document-->
<library>
  <book isbn="0-246-13655-3">
    <title>Mystery</title>
    <author>Peter Straub</author>
  </book>
  <book isbn="1-876590-17-3">
    <title>False Memory</title>
    <author>Dean Koontz</author>
  </book>
</library>

To create a persistent document from scratch, you create instances of your own JadeXMLNode subclasses and then add them to the tree. For example, the following example is similar to the previous example except that it creates a tree of persistent objects.

create2();
vars
    doc: MyDocument;
    comment: MyComment;
    attribute: MyAttribute;
    library, book, title, author: MyElement;
begin
    beginTransaction;
    create doc persistent;
    create comment persistent;
    doc.addCommentObject(comment, 'An example XML document');
    create library persistent;
    doc.addElementObject(library, 'library');
    create book persistent;
    library.addElementObject(book, 'book');
    create attribute persistent;
    book.addAttributeObject(attribute, 'isbn', '0-246-13655-3');
    create title persistent;
    book.addElementObject(title, 'title');
    title.setText('Mystery');
    create author persistent;
    book.addElementObject(author, 'author');
    author.setText('Peter Straub');
    create book persistent;
    library.addElementObject(book, 'book');
    create attribute persistent;
    book.addAttributeObject(attribute, 'isbn', '1-876590-17-3');
    create title persistent;
    book.addElementObject(title, 'title');
    title.setText('False Memory');
    create author persistent;
    book.addElementObject(author, 'author');
    author.setText('Dean Koontz');
    write doc.writeToString;
    commitTransaction;
end;