Chang Liu

Nov 1999

# Perl Module XML::Node

## XML::Node

### Overview

XML::Node is for those who use Perl to process XML files. XML::Node is built on top of XML::Parser and provides a simplified programming interface. XML::Node users can register callback sub-routines or variables to specific type of XML nodes. The values of the specified XML nodes are automatically copied to the corresponding variables. When specified XML tags are found, registered callback sub-routines are called. XML::Node is a shortcut to XML::Parser if you don't care much about details of XML files.

Here is an example. This XML file contains order information:

#### orders.xml

    <Orders>
      <Order>
        <Item>A Book</Item>
        <Quantity>1</Quantity>
        <TagsThatIDontCare>Something</TagsThatIDontCare>
       </Order>
       <TagsThatIDontCare>Blah Blah</TagsThatIDontCare>
    </Orders>

This simple Perl script can parse the XML file and print out all the orders.

#### parse_orders.pl

    use XML::Node;

    my $item = "";
    my $quantity = "";

    $p = XML::Node->new();

    $p->register(">Orders>Order>Item","char" => \$item);
    $p->register(">Orders>Order>Quantity","char" => \$quantity);
    $p->register(">Orders>Order","end" => \&handle_order_end);

    print "Processing file [orders.xml]...\n";
    $p->parse("orders.xml");

    sub handle_order_end
    {
        print "Found order -- Item: [$item] Quantity: [$quantity]\n";
        $item = "";
        $quantity = "";
    }

### Download

The XML::Node module can be found at http://belmont-shores.ics.uci.edu/pub, ftp://belmont-shores.ics.uci.edu/pub, or Perl CPAN.

### Bug Report

Please submit any related bugs, issues, suggestions, or comments to http://belmont-shores.ics.uci.edu/bugzilla.