| 1 |
XML::Node Version 0.11 |
| 2 |
|
| 3 |
Here is a quick but complete example to show you how XML::Node promises |
| 4 |
to keep your XML processing scripts short and clear. |
| 5 |
---------- orders.xml ---------------------------------------------- |
| 6 |
<Orders> |
| 7 |
<Order ID="0008"> |
| 8 |
<Item>A Book</Item> |
| 9 |
<Quantity>1</Quantity> |
| 10 |
<TagsThatIDontCare>Something</TagsThatIDontCare> |
| 11 |
</Order> |
| 12 |
<TagsThatIDontCare>Blah Blah</TagsThatIDontCare> |
| 13 |
</Orders> |
| 14 |
|
| 15 |
---------- parse-orders.pl ------------------------------------------ |
| 16 |
use XML::Node; |
| 17 |
|
| 18 |
my $item = ""; |
| 19 |
my $quantity = ""; |
| 20 |
my $id = ""; |
| 21 |
|
| 22 |
$p = XML::Node->new(); |
| 23 |
|
| 24 |
$p->register(">Orders>Order:ID","char" => \$item); |
| 25 |
$p->register(">Orders>Order>Item","char" => \$item); |
| 26 |
$p->register(">Orders>Order>Quantity","char" => \$quantity); |
| 27 |
$p->register(">Orders>Order","end" => \&handle_order_end); |
| 28 |
|
| 29 |
print "Processing file [orders.xml]...\n"; |
| 30 |
$p->parsefile("orders.xml"); |
| 31 |
|
| 32 |
sub handle_order_end |
| 33 |
{ |
| 34 |
print "Found order [$id] -- Item: [$item] Quantity: [$quantity]\n"; |
| 35 |
$id=""; |
| 36 |
$item = ""; |
| 37 |
$quantity = ""; |
| 38 |
} |
| 39 |
|
| 40 |
--------------------------------------------------------------------- |
| 41 |
Copyright (c) 1999,2000,2001 Chang Liu |
| 42 |
All rights reserved. |
| 43 |
|
| 44 |
This program is free software; you can redistribute it and/or modify |
| 45 |
it under the same terms as Perl itself. |
| 46 |
|
| 47 |
This is a Perl extension interface to XML::Parser. It requires at |
| 48 |
least version 5.004 of perl. The documentation for this extension can |
| 49 |
be found in pod format in the file Node.pm. The 'perldoc' program, |
| 50 |
provided with the perl distribution, can be used to view this |
| 51 |
documentation. |
| 52 |
|
| 53 |
The purpose of this module is to simplify interface of XML |
| 54 |
parser. Instead of worrying about "start", "end", and "char" callbacks |
| 55 |
of every single XML node, you can simply say that you only want to be |
| 56 |
notified when a path, such as ">TestCase>Name", is found. Path |
| 57 |
">TestCase>Name" corresponds to XML nodes: |
| 58 |
<TestCase> |
| 59 |
... |
| 60 |
<Name>Something</Name> |
| 61 |
... |
| 62 |
</TestCase> |
| 63 |
|
| 64 |
Using XML::Node, you can easily ignore the parts of XML files that you |
| 65 |
are not interested in. Another feature of XML::Node is that you can |
| 66 |
register a variable instead of a callback function. The corresponding |
| 67 |
string found in a XML file will be automatically appended to your |
| 68 |
variable. This way, you don't have to write a callback function for |
| 69 |
this type of simple handling. Here's an example: |
| 70 |
|
| 71 |
my $testcase_name = ""; |
| 72 |
register(">TestCase>Name","char" => \$testcase_name); |
| 73 |
|
| 74 |
|
| 75 |
--------------------------------------------------------------------- |
| 76 |
To install this module, cd to the directory that contains this |
| 77 |
README file and type the following: |
| 78 |
|
| 79 |
perl Makefile.PL |
| 80 |
|
| 81 |
Alternatively, if you plan to install XML::Node somewhere other than |
| 82 |
your system's perl library directory. You can type something like |
| 83 |
this: |
| 84 |
|
| 85 |
perl Makefile.PL PREFIX=/home/me/perl INSTALLDIRS=perl |
| 86 |
|
| 87 |
Then to build you run make. |
| 88 |
|
| 89 |
make |
| 90 |
|
| 91 |
You can then test the module by typing: |
| 92 |
|
| 93 |
make test |
| 94 |
|
| 95 |
There are some sample utilities in the samples directory along with an |
| 96 |
xml form of the XML specification to test them on. You may need to |
| 97 |
change the '#!' line at the top of these utilities to what is |
| 98 |
appropriate for your system. If you're going to play around with them |
| 99 |
prior to installing the module, you would need to add the blib paths |
| 100 |
to your perl search path, like this (assuming your current directory |
| 101 |
is samples): |
| 102 |
|
| 103 |
perl -I../blib/lib -I../blib/arch test.pl |
| 104 |
|
| 105 |
or set your PERLLIB environment variable. |
| 106 |
|
| 107 |
If you have write access to the installation directories, you may then |
| 108 |
install by typing: |
| 109 |
|
| 110 |
make install |
| 111 |
|
| 112 |
After this, you may use "man XML::Node" to view the man page of this |
| 113 |
module. |
| 114 |
|
| 115 |
|
| 116 |
--------------------------------------------------------------------- |
| 117 |
When testing the package, make sure "make install" is called every |
| 118 |
time after "Node.pm" is modified. |
| 119 |
--------------------------------------------------------------------- |
| 120 |
To rebuild the package: |
| 121 |
edit MANIFEST |
| 122 |
perl Makefile.PL |
| 123 |
make dist or nmake dist (make sure tar and gzip are on path.) |
| 124 |
|
| 125 |
--Chang Liu ([email protected] | [email protected]) |
| 126 |
|
| 127 |
|