NAME
XML::XPath::XMLParser - The default XML parsing class that produces a
node treeSYNOPSIS
my $parser = XML::XPath::XMLParser->new(
filename => $self->getfilename,
xml => $self->getxml,
ioref => $self->getioref,
parser => $self->getparser,
);my $rootnode = $parser->parse;
DESCRIPTION
This module generates a node tree for use as the context node for XPath processing. It aims to be a quick parser, nothing fancy, and yet has to store more information than most parsers. To achieve this I've usedarray refs everywhere - no hashes. I don't have any performance
figures for the speedups achieved, so I make no appologies for anyone not used to using arrays instead of hashes. I think they make good sense here where we know the attributes of each type of node. NNooddee SSttrruuccttuurree All nodes have the same first 2 entries in the array: nodeparent and nodepos. The type of the node is determined using the ref() function. The nodeparent always contains an entry for the parent of the currentnode - except for the root node which has undef in there. And nodepos
is the position of this node in the array that it is in (think: $node
== $node->[nodeparent]->[nodechildren]->[$node->[nodepos]] )
Nodes are structured as follows: RRoooott NNooddee The root node is just an element node with no parent. [undef, # nodeparent - check for undef to identify root node
undef, # nodepos
undef, # nodeprefix
[ ... ], # nodechildren (see below)
] EElleemmeenntt NNooddee [$parent, # nodeparent
, # nodepos 'xxx', # nodeprefix - namespace prefix on this element
[ ... ], # nodechildren
'yyy', # nodename - element tag name
[ ... ], # nodeattribs - attributes on this element
[ ... ], # nodenamespaces - namespaces currently in scope
] AAttttrriibbuuttee NNooddee [$parent, # nodeparent - the element node
, # nodepos 'xxx', # nodeprefix - namespace prefix on this element
'href', # nodekey - attribute name
'ftp://ftp.com/', # nodevalue - value in the node
] NNaammeessppaaccee NNooddeess Each element has an associated set of namespace nodes that are currently in scope. Each namespace node stores a prefix and the expanded name (retrieved from the xmlns:prefix="..." attribute). [$parent,
, 'a', # nodeprefix - the namespace as it was written as a prefix
'http://my.namespace.com', # nodeexpanded - the expanded name.
] TTeexxtt NNooddeess [$parent,
, 'This is some text' # nodetext - the text in the node
] CCoommmmeenntt NNooddeess [$parent,
, 'This is a comment' # nodecomment
] PPrroocceessssiinngg IInnssttrruuccttiioonn NNooddeess [$parent,
, 'target', # nodetarget
'data', # nodedata
] UUssaaggee If you feel the need to use this module outside of XML::XPath (for example you might use this module directly so that you can cache parsed trees), you can follow the following API: nneeww The new method takes either no parameters, or any of the following parameters: filename xml parser ioref This uses the familiar hash syntax, so an example might be:use XML::XPath::XMLParser;
my $parser = XML::XPath::XMLParser->new(filename => 'example.xml');
The parameters represent a filename, a string containing XML, an XML::Parser instance and an open filehandle ref respectively. You can also set or get all of these properties using the get and set functions that have the same name as the property: e.g. getfilename, setioref, etc. ppaarrssee The parse method generally takes no parameters, however you are free to pass either an open filehandle reference or an XML string if you so require. The return value is a tree that XML::XPath can use. The parse method will die if there is an error in your XML, so be sure to use perl's exception handling mechanism (eval{};) if you want to avoid this. ppaarrsseeffiillee The parsefile method is identical to parse() except it expects a single parameter that is a string naming a file to open and parse. Again it returns a tree and also dies if there are XML errors. NNOOTTIICCEESS This file is distributed as part of the XML::XPath module, and is copyright 2000 Fastnet Software Ltd. Please see the documentation for the module as a whole for licencing information.perl v5.8.8 2001-03-14 XPath::XMLParser(3)