-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml.php
87 lines (74 loc) · 2.08 KB
/
xml.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
<?php
class Xml {
// xmlを見やすくダンプ
//# 引数はSimpleXMLElementでもstringでもOK!
public static function dump($xml) {
echo self::xmlstr($xml);
}
// xmlを見やすい形式の文字列に変換
//# 引数はSimpleXMLElementでもstringでもOK!
public static function xmlstr($xml) {
$dom = new DOMDocument('1.0');
$dom->loadXML($xml instanceof SimpleXMLElement ? $xml->asXML() : $xml);
$dom->formatOutput = true;
return $dom->saveXML();
}
public static function xmlobj($xml) {
$tidy = new tidy;
$tidy->parseString($xml, array('output-xhtml' => true), 'UTF8');
$tidy->cleanRepair();
$body = $tidy->html();
$body = preg_replace('/\sxmlns="[^"]+"/', '', $body);
$body = str_replace('&', '&', $body);
try {
$xmlobj = new SimpleXMLElement($body);
}
catch (Exception $e) {
throw $e;
}
return $xmlobj;
}
public static function arrayToXML($array, &$node) {
foreach($array as $key => $value) {
// 属性のとき
if ($key == '@attributes') {
MXml::addAttributes($value, $node);
continue;
}
// 現ノード内に配列がある場合は再帰的に処理
if (is_array($value)) {
foreach($value AS $subkey => $subValue){
// 数字のとき自身のキーをスキップして現在キーに直接要素を挿入
$is_number_node = false;
if(is_numeric($subkey)){
$is_number_node = true;
$somenode = $node->addChild($key);
MXml::arrayToXML($subValue, $somenode);
}
}
if($is_number_node){
// 次の要素へスキップ
continue;
}
else{
$subnode = $node->addChild($key);
MXml::arrayToXML($value, $subnode);
}
}
// 配列がない場合は子ノードと値を設定
else {
$node->addChild($key, $value);
}
}
}
private function addAttributes($array, &$node) {
foreach ($array as $key => $value) {
$node->addAttribute($key,$value);
}
}
public function convert_xml($root, $req) {
$root_node = new SimpleXMLElement("<{$root}></{$root}>");
MXml::arrayToXML($req, $root_node);
return $root_node->asXML();
}
}