1XML::XPath::XMLParser(3U)ser Contributed Perl DocumentatiXoMnL::XPath::XMLParser(3)
2
3
4
6 XML::XPath::XMLParser - The default XML parsing class that produces a
7 node tree
8
10 my $parser = XML::XPath::XMLParser->new(
11 filename => $self->get_filename,
12 xml => $self->get_xml,
13 ioref => $self->get_ioref,
14 parser => $self->get_parser,
15 );
16 my $root_node = $parser->parse;
17
19 This module generates a node tree for use as the context node for XPath
20 processing. It aims to be a quick parser, nothing fancy, and yet has
21 to store more information than most parsers. To achieve this I've used
22 array refs everywhere - no hashes. I don't have any performance
23 figures for the speedups achieved, so I make no apologies for anyone
24 not used to using arrays instead of hashes. I think they make good
25 sense here where we know the attributes of each type of node.
26
28 All nodes have the same first 2 entries in the array: node_parent and
29 node_pos. The type of the node is determined using the ref() function.
30 The node_parent always contains an entry for the parent of the current
31 node - except for the root node which has undef in there. And node_pos
32 is the position of this node in the array that it is in (think: $node
33 == $node->[node_parent]->[node_children]->[$node->[node_pos]] )
34
35 Nodes are structured as follows:
36
37 Root Node
38 The root node is just an element node with no parent.
39
40 [
41 undef, # node_parent - check for undef to identify root node
42 undef, # node_pos
43 undef, # node_prefix
44 [ ... ], # node_children (see below)
45 ]
46
47 Element Node
48 [
49 $parent, # node_parent
50 <position in current array>, # node_pos
51 'xxx', # node_prefix - namespace prefix on this element
52 [ ... ], # node_children
53 'yyy', # node_name - element tag name
54 [ ... ], # node_attribs - attributes on this element
55 [ ... ], # node_namespaces - namespaces currently in scope
56 ]
57
58 Attribute Node
59 [
60 $parent, # node_parent - the element node
61 <position in current array>, # node_pos
62 'xxx', # node_prefix - namespace prefix on this element
63 'href', # node_key - attribute name
64 'ftp://ftp.com/', # node_value - value in the node
65 ]
66
67 Namespace Nodes
68 Each element has an associated set of namespace nodes that are
69 currently in scope. Each namespace node stores a prefix and the
70 expanded name (retrieved from the xmlns:prefix="..." attribute).
71
72 [
73 $parent,
74 <pos>,
75 'a', # node_prefix - the namespace as it was written as a prefix
76 'http://my.namespace.com', # node_expanded - the expanded name.
77 ]
78
79 Text Nodes
80 [
81 $parent,
82 <pos>,
83 'This is some text' # node_text - the text in the node
84 ]
85
86 Comment Nodes
87 [
88 $parent,
89 <pos>,
90 'This is a comment' # node_comment
91 ]
92
93 Processing Instruction Nodes
94 [
95 $parent,
96 <pos>,
97 'target', # node_target
98 'data', # node_data
99 ]
100
102 If you feel the need to use this module outside of XML::XPath (for
103 example you might use this module directly so that you can cache parsed
104 trees), you can follow the following API:
105
106 new
107 The new method takes either no parameters, or any of the following
108 parameters:
109
110 filename
111 xml
112 parser
113 ioref
114
115 This uses the familiar hash syntax, so an example might be:
116
117 use XML::XPath::XMLParser;
118
119 my $parser = XML::XPath::XMLParser->new(filename => 'example.xml');
120
121 The parameters represent a filename, a string containing XML, an
122 XML::Parser instance and an open filehandle ref respectively. You can
123 also set or get all of these properties using the get_ and set_
124 functions that have the same name as the property: e.g. get_filename,
125 set_ioref, etc.
126
127 parse
128 The parse method generally takes no parameters, however you are free to
129 pass either an open filehandle reference or an XML string if you so
130 require. The return value is a tree that XML::XPath can use. The parse
131 method will die if there is an error in your XML, so be sure to use
132 perl's exception handling mechanism (eval{};) if you want to avoid
133 this.
134
135 parsefile
136 The parsefile method is identical to parse() except it expects a single
137 parameter that is a string naming a file to open and parse. Again it
138 returns a tree and also dies if there are XML errors.
139
141 This file is distributed as part of the XML::XPath module, and is
142 copyright 2000 Fastnet Software Ltd. Please see the documentation for
143 the module as a whole for licencing information.
144
145
146
147perl v5.38.0 2023-07-21 XML::XPath::XMLParser(3)