-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpharse_xml2array.php
111 lines (99 loc) · 3.13 KB
/
pharse_xml2array.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* @author RESS.IO Team
* @package Pharse
* @link https://github.com/ressio/pharse
*
* FORKED FROM
* @author Niels A.D.
* @package Ganon
* @link http://code.google.com/p/ganon/
*
* @license http://dev.perl.org/licenses/artistic.html Artistic License
*/
include_once('pharse_parser_html.php');
/**
* Converts a XML document to an array
*/
class XML_Parser_Array extends HTML_Parser_Base
{
/**
* Holds the document structure
* @var array array('name' => 'tag', 'attrs' => array('attr' => 'val'), 'childen' => array())
*/
public $root = array(
'name' => '',
'attrs' => array(),
'children' => array()
);
/**
* Current parsing hierarchy
* @var array
* @access private
*/
protected $hierarchy = array();
protected function parse_hierarchy($self_close)
{
if ($this->status['closing_tag']) {
$found = false;
for ($count = count($this->hierarchy), $i = $count - 1; $i >= 0; $i--) {
if (strcasecmp($this->hierarchy[$i]['name'], $this->status['tag_name']) === 0) {
for ($ii = ($count - $i - 1); $ii >= 0; $ii--) {
$e = array_pop($this->hierarchy);
if ($ii > 0) {
$this->addError('Closing tag "' . $this->status['tag_name'] . '" while "' . $e['name'] . '" is not closed yet');
}
}
$found = true;
break;
}
}
if (!$found) {
$this->addError('Closing tag "' . $this->status['tag_name'] . '" which is not open');
}
} else {
$tag = array(
'name' => $this->status['tag_name'],
'attrs' => $this->status['attributes'],
'children' => array()
);
if ($this->hierarchy) {
$current =& $this->hierarchy[count($this->hierarchy) - 1];
$current['children'][] = $tag;
$tag =& $current['children'][count($current['children']) - 1];
unset($current['tagData']);
} else {
$this->root = $tag;
$tag =& $this->root;
$self_close = false;
}
if (!$self_close) {
$this->hierarchy[] =& $tag;
}
}
}
protected function parse_tag_default()
{
if (!parent::parse_tag_default()) {
return false;
}
if ($this->status['tag_name'][0] !== '?') {
$this->parse_hierarchy(($this->status['self_close']) ? true : null);
}
return true;
}
protected function parse_text()
{
parent::parse_text();
if (($this->status['text'] !== '') && $this->hierarchy) {
$current =& $this->hierarchy[count($this->hierarchy) - 1];
if (!$current['children']) {
$current['tagData'] = $this->status['text'];
}
}
}
protected function parse_all()
{
return ((parent::parse_all()) ? $this->root : false);
}
}