-
Notifications
You must be signed in to change notification settings - Fork 4
/
xml_tools.inc.php
98 lines (91 loc) · 1.85 KB
/
xml_tools.inc.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
<?php
function xml_node_to_array($node, $protect ="")
{
if($node->hasChildNodes())
{
$result = array();
foreach($node->childNodes as $subnode)
{
if($subnode->localName != "")
{
$key = trim($subnode->localName,$protect);
$val = xml_node_to_array($subnode,$protect);
$result[$key] = $val;
//echo($subnode->localName."<br/>");
}
}
if(count($result)>0)
return $result;
}
//echo($node->nodeValue."<br/>");
$result = str_replace(array("\n"), array(""), trim($node->nodeValue,$protect." "));
return $result;
}
function thing_to_xml_node($thing,$node, $doc, $protect = "", $fields = NULL)
{
if(is_object($thing) || is_array($thing))
{
foreach($thing as $field => $value)
{
if($fields == NULL || in_array($field,$fields))
{
if($field != "")
{
if(is_numeric(substr($field, 0, 1)))
$field = $protect.$field;
$subnode = $doc->createElement($field);
thing_to_xml_node($value,$subnode,$doc, $protect, $fields);
$node->appendChild($subnode);
}
else
{
$node->nodeValue = filt($value);
}
}
}
}
else
{
$node->nodeValue = filt($thing);
}
}
function filt($val)
{
return str_replace(array('&','"'," ","\r"),array('','','',''),stripInvalidXml(remove_br($val)));
}
/**
* Removes invalid XML
*
* @access public
* @param string $value
* @return string
*/
function stripInvalidXml($value)
{
$ret = "";
$current;
if (empty($value))
{
return $ret;
}
$length = strlen($value);
for ($i=0; $i < $length; $i++)
{
$current = ord($value{$i});
if (($current == 0x9) ||
($current == 0xA) ||
($current == 0xD) ||
(($current >= 0x20) && ($current <= 0xD7FF)) ||
(($current >= 0xE000) && ($current <= 0xFFFD)) ||
(($current >= 0x10000) && ($current <= 0x10FFFF)))
{
$ret .= chr($current);
}
else
{
$ret .= " ";
}
}
return $ret;
}
?>