The first parameter must be a tokenized array, which is
an array returned by
XML_PullParser_getToken,
XML_PullParser_getElement, XML_PullParser_getChild,
or one of several other token-returning functions.
1
If this parameter is absent, then the function will first look for the
$current_element
and if that hasn't been set then it will use the current token. One or both of these
will have been
preset internally. 2
The second parameter is a list of elements for inclusion in the returned sequence array.
It is either a variable length list of names or an array of names. These will all be strings.
If this parameter is absent then all the elements found in the first parameter
(or the internal default array) are included in the returned array.
Listing 10
1. $tags = array("entry");
2. $child_tags = array("server","ipaddress", "domain");
3.
4. $parser = new XML_PullParser_doc($doc,$tags,$child_tags);
5.
6. while($token = $parser->XML_PullParser_getToken())
7. {
8. $parser->XML_PullParser_getElement('server');
9. $seq = $parser->XML_PullParser_getSequence();
10.
11. for($i=0; $i < count($seq); $i++) {
12. list($server, $which) = each($seq[$i]);
13.
14. $name = $parser->XML_PullParser_getText($server,$which);
15. echo "Name: $name\n";
16.
17. $ip = $parser->XML_PullParser_getAttributes($server,$which);
18. echo "\tIP: " . $parser->XML_PullParser_getAttrVal('ip', $ip) . "\n";
19. }
10.
21. }
/*
Result
Name: example_1.com
IP: 192.168.10.1
Name: example_2.com
IP: 192.168.10.2
Name: example_3.com
IP: 192.168.10.3
*/
Line 8 stores the three server elements in the $current_element,
and line 9 creates the sequence array, which we then loop through. Each element in the
array is in turn an associative array of the form:
Element_Name => $which
In the present case, the first element would be "SERVER" => 1, the second "SERVER" => 2, etc. In line 14
the element name and its which value are passed into
XML_PullParser_getText and in line 17 they are passed into
XML_PullParser_getAttributes. Therefore, the two functions know the name
of the element to address-- server -- and which element in the sequence of
server elements.
To treat all of the elements in
Entry, we would not call
XML_PullParser_getElement.
In that case, if
XML_PullParser_getSequence is not passed the array parameter, it returns
a sequencing array based on
the current token.
Listing 11 shows how this might be implemented using a
switch statement.
Listing 11
1. while($token = $parser->XML_PullParser_getToken())
2. {
3.
4. $seq = $parser->XML_PullParser_getSequence();
5.
6. for($i=0; $i < count($seq); $i++) {
7. list($element, $which) = each($seq[$i]);
8.
9. switch($element) {
12. case 'IPADDRESS':
13. echo "$element: $which\n";
14. echo $parser->XML_PullParser_getText($element,$which) . "\n";
15. break;
16. case 'SERVER':
17. echo "$element: $which\n";
18. echo $parser->XML_PullParser_getText($element,$which) . "\n";
19. $ip = $parser->XML_PullParser_getAttributes($element,$which);
20. echo "\tIP: " . $parser->XML_PullParser_getAttrVal('ip', $ip) . "\n";
21. break;
22. case 'DOMAIN':
23. echo "$element: $which\n";
24. echo $parser->XML_PullParser_getText($element,$which) . "\n";
25. break;
26. case 'ALIAS':
27. echo "$element: $which\n";
28. echo $parser->XML_PullParser_getText($element,$which) . "\n";
29. break;
30. default:
31. echo "default: $element: $which\n";
32.
33. }
34. }
35.
35. }
/* Result
default: ENTRY: 1
IPADDRESS: 1
172.20.19.6
DOMAIN: 1
example.com
SERVER: 1
example_1.com
IP: 192.168.10.1
SERVER: 2
example_2.com
IP: 192.168.10.2
SERVER: 3
example_3.com
IP: 192.168.10.3
ALIAS: 1
www.example.com
*/
It is worth noting here that the
switch statement use upper case for each of the
case
statements. The PHP XML facility defaults to upper case for all element and attribute names. This can
be changed in
XML_PullParser by calling the package level function
XML_PullParser_caseSensitive with a value of
true. 4