Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F1045287
xml_helper.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Jun 26, 2:56 AM
Size
4 KB
Mime Type
text/x-php
Expires
Sat, Jun 28, 2:56 AM (4 m, 4 s)
Engine
blob
Format
Raw Data
Handle
675842
Attached To
rINP In-Portal
xml_helper.php
View Options
<?php
class
kXMLHelper
extends
kHelper
{
var
$RootElement
=
null
;
/**
* Enter description here...
*
* @var kXMLNode
*/
var
$CurrentElement
=
null
;
/**
* Parses XML data specified and returns root node
*
* @param string $xml
* @return kXMLNode
*/
function
&
Parse
(
$xml
=
null
)
{
$this
->
Clear
();
// in case if Parse method is called more then one time
$xml_parser
=
xml_parser_create
();
xml_set_element_handler
(
$xml_parser
,
Array
(&
$this
,
'startElement'
),
Array
(&
$this
,
'endElement'
)
);
xml_set_character_data_handler
(
$xml_parser
,
Array
(&
$this
,
'characterData'
)
);
if
(!
xml_parse
(
$xml_parser
,
$xml
,
1
))
{
$this
->
RootElement
=
new
kXMLNode
(
'ERROR'
,
array
(
'code'
=>
xml_get_error_code
(
$xml_parser
),
'message'
=>
xml_error_string
(
xml_get_error_code
(
$xml_parser
))));
trigger_error
(
sprintf
(
'XML error: %s at line %d'
,
xml_error_string
(
xml_get_error_code
(
$xml_parser
)),
xml_get_current_line_number
(
$xml_parser
)),
E_USER_WARNING
);
}
xml_parser_free
(
$xml_parser
);
$root_copy
=
$this
->
RootElement
;
return
$root_copy
;
}
function
startElement
(&
$Parser
,
&
$Elem
,
$Attrs
)
{
$parent
=&
$this
->
CurrentElement
;
// 1. $parent is now reference to $this->CurrentElement
$this
->
CurrentElement
=&
new
kXMLNode
(
$Elem
,
$Attrs
);
// 2. =& ensures, that new object won't be assigned to $parent as well (don't remove)
if
(!
isset
(
$this
->
RootElement
)
||
is_null
(
$this
->
RootElement
))
{
$this
->
RootElement
=&
$this
->
CurrentElement
;
}
if
(!
is_null
(
$parent
))
{
$parent
->
AddChild
(
$this
->
CurrentElement
);
}
}
function
characterData
(
$Parser
,
$Line
)
{
$this
->
CurrentElement
->
AppendData
(
$Line
);
}
function
endElement
(
$Parser
,
$Elem
)
{
if
(
$this
->
CurrentElement
->
Parent
!=
null
)
{
$this
->
CurrentElement
=&
$this
->
CurrentElement
->
Parent
;
}
}
function
Clear
()
{
unset
(
$this
->
RootElement
);
unset
(
$this
->
CurrentElement
);
}
}
class
kXMLNode
{
var
$Name
=
null
;
var
$Attributes
=
array
();
var
$Children
=
array
();
var
$Data
=
null
;
var
$firstChild
=
null
;
var
$lastChild
=
null
;
/**
* Parent node
*
* @var kXMLNode
*/
var
$Parent
=
null
;
/**
* Node position relative to other nodes of it's parent
*
* @var int
*/
var
$Position
=
0
;
function
kXMLNode
(
$name
,
$attrs
=
array
())
{
$this
->
Name
=
$name
;
$this
->
Attributes
=
$attrs
;
}
function
SetParent
(&
$elem
)
{
$this
->
Parent
=&
$elem
;
}
/**
* Adds new child to current node
*
* @param kXMLNode $a_child
*/
function
AddChild
(&
$a_child
)
{
$node_count
=
count
(
$this
->
Children
);
$a_child
->
Position
=
$node_count
;
if
(
$node_count
==
0
)
{
$this
->
firstChild
=&
$a_child
;
$this
->
lastChild
=&
$a_child
;
}
else
{
$this
->
lastChild
=&
$a_child
;
}
$this
->
Children
[]
=&
$a_child
;
$a_child
->
SetParent
(
$this
);
}
function
AppendData
(
$data
)
{
$this
->
Data
.=
$data
;
}
function
&
GetChild
(
$path
)
{
$entries
=
explode
(
'/'
,
strtoupper
(
$path
));
$cur
=
array_shift
(
$entries
);
if
(
$cur
==
$this
->
Name
)
$cur
=
array_shift
(
$entries
);
if
(!
$cur
)
return
$this
;
if
(!
isset
(
$this
->
Children
[
$cur
]))
return
false
;
$left
=
implode
(
'/'
,
$entries
);
if
(!
$left
)
return
$this
->
Children
[
$cur
];
return
$this
->
Children
[
$cur
]->
GetChild
(
$left
);
}
function
GetChildValue
(
$path
)
{
$child
=&
$this
->
GetChild
(
$path
);
if
(
$child
!==
false
)
{
return
$child
->
Data
;
}
}
function
&
GetChildByPosition
(
$position
)
{
if
(
$position
<
count
(
$this
->
Children
)
)
{
return
$this
->
Children
[
$position
];
}
else
{
$false
=
false
;
return
$false
;
}
}
function
&
FindChild
(
$name
)
{
$name
=
strtoupper
(
$name
);
if
(
$this
->
Name
==
$name
)
return
$this
;
// if (isset($this->Children[$name])) return $this->Children[$name];
// $children = array_keys($this->Children);
foreach
(
$this
->
Children
as
$elem
)
{
$child
=&
$elem
->
FindChild
(
$name
);
if
(
$child
!==
false
)
{
return
$child
;
}
}
$false
=
false
;
return
$false
;
}
function
FindChildValue
(
$name
,
$attr
=
null
)
{
$child
=&
$this
->
FindChild
(
$name
);
if
(
$child
!==
false
)
{
if
(
isset
(
$attr
))
{
return
$child
->
Attributes
[
strtoupper
(
$attr
)];
}
return
$child
->
Data
;
}
}
/**
* Returns next node to this, false in case of end list
*
* @return kXMLNode
*/
function
&
NextSibling
()
{
if
(!
is_null
(
$this
->
Parent
))
{
$ret
=&
$this
->
Parent
->
GetChildByPosition
(
$this
->
Position
+
1
);
return
$ret
;
}
else
{
$false
=
false
;
return
$false
;
}
}
/**
* Reconstructs XML of the node and subnodes
*
* $param bool $content_only
*/
function
GetXML
(
$content_only
=
false
)
{
$xml
=
''
;
if
(!
$content_only
)
{
$xml
=
'<'
.
$this
->
Name
;
if
(
count
(
$this
->
Attributes
))
{
$xml
.=
' '
;
$att_contents
=
array
();
foreach
(
$this
->
Attributes
as
$name
=>
$value
)
{
$att_contents
[]
=
$name
.
'="'
.
$value
.
'"'
;
}
$xml
.=
implode
(
' '
,
$att_contents
);
}
$xml
.=
'>'
;
}
$xml
.=
$this
->
Data
;
foreach
(
$this
->
Children
as
$node
)
{
$xml
.=
$node
->
GetXML
();
}
if
(!
$content_only
)
{
$xml
.=
'</'
.
$this
->
Name
.
'>'
;
}
return
$xml
;
}
}
?>
Event Timeline
Log In to Comment