class TreeParser

Implements a TreeParser.

Warning!

Due to the nature of libxml2, xml parsing, unlike ferite, is not currently completely thread safe. This is exhibited within the error reporting of the parsers and is currently unavoidable.

Description

Parse XML into a tree.

Example

uses "xml";
uses "console";
uses "array";
uses "string";

global {
    number dumpElements = 1;
}

function whitespace( number count ) {
    string s = "";
    while( count > 0 )
    {
        s += "    ";
        count--;
    }
    return s;
}


function processElement( object element, number depth ) {
    string ed = String.trim( element.getElementData(), " \t\n\r" );
    
    if( dumpElements )
        Console.println( whitespace(depth) + ">>>>< Element: " +
                element.getElementName() + " Ns:" + element.getNamespace() +
                " Attr:" + element.getAttributes() );
    
    if( ed != "" && dumpElements )
        Console.println( whitespace(depth + 1) + "---- '$ed'" );
    
    if( element.hasChildren() )
    {
        array children = element.getChildren();
        Array.each( children ) using ( child ) {
            processElement( child, depth + 1 );
        };
    }
    
    if( dumpElements )
        Console.println( whitespace(depth) + "<<<< Element: " +
                element.getElementName() );
}

object o = new XML.TreeParser();
o.parseFile( argv[0] );

processElement( o.getDocumentElement(), 0 );

if( !dumpElements )
    Console.println( o.toString() );

class contents [NB. Highlighted attributes are static members]
Functions
function getDocumentElement() - Retrieve the document element from a parsed xml entity.
function keepBlanks(number) - Make the parser keep all whitespace.
function parseChunk(string) - Parse an xml chunk into a tree.
function parseFile(string) - Parse an xmlfile into a tree.
function saveToFile(string) - Save the parser tree to a file.
function toString() - Return the XML tree as a string.
function validation(number) - Should we validate the XML agains the DTD in the DOCTYPE element?
function xpathArray(string) - Retrive an array of values from the tree using XPath.
function xpathNode(string) - Retrive a string from the tree using XPath.

Functions

function getDocumentElement Click to go up to the list
Retrieve the document element from a parsed xml entity.
Declaration:
    function getDocumentElement( )
Returns:
    The document element as an XML Element object or a null object on failure.

function keepBlanks Click to go up to the list
Make the parser keep all whitespace.
Declaration:
    function keepBlanks( number bool )
Parameters:
    Parameter #1: number bool - true (default) for yes and false for no.

function parseChunk Click to go up to the list
Parse an xml chunk into a tree.
Declaration:
    function parseChunk( string chunk )
Parameters:
    Parameter #1: string chunk - The XML string to parse.

function parseFile Click to go up to the list
Parse an xmlfile into a tree.
Declaration:
    function parseFile( string filename )
Parameters:
    Parameter #1: string filename - The XML to parse.

function saveToFile Click to go up to the list
Save the parser tree to a file.
Declaration:
    function saveToFile( string filename )
Parameters:
    Parameter #1: string filename - The name of the file to save to.
Returns:
    true on success false on failure.

function toString Click to go up to the list
Return the XML tree as a string.
Declaration:
    function toString( )
Returns:
    a string containing the XML data.

function validation Click to go up to the list
Should we validate the XML agains the DTD in the DOCTYPE element?
Declaration:
    function validation( number bool )
Parameters:
    Parameter #1: number bool - true for validate and false for no validation (default).

function xpathArray Click to go up to the list
Retrive an array of values from the tree using XPath.
Declaration:
    function xpathArray( string expr )
Parameters:
    Parameter #1: string axpr - The XPath expression.
Returns:
    An array containing the result of the xpath expression.

function xpathNode Click to go up to the list
Retrive a string from the tree using XPath.
Declaration:
    function xpathNode( string expr )
Parameters:
    Parameter #1: string txpath - The XPath expression.
Returns:
    A XML.Element node, or null

Automatically generated at 12:08PM, Wednesday 25 May 2005 by feritedoc.