-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleXml.php
167 lines (146 loc) · 3.35 KB
/
simpleXml.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Creater:kiss
* Date:2014/7/8
* @desc:simplexml生成xml类
*
*/
class GenerateXml
{
public $root;
//数组中标签数组
const TAG = 'tag';
//数组中标签的属性
const PROPERTY = 'property';
//数组中标签的子标签
const CHILD = 'children';
//数组中代表名字
const TEXT = 'text';
//数组中代表值
const VALUE = 'value';
//xml数组,根据此数组格式生成xml
public $xmlArray;
public function __construct()
{
if (empty($this->root)) {
$this->root = "<root />";
}
if (empty($this->xmlArray)) {
$this->xmlArray = array();
}
}
public function run()
{
if (count($this->xmlArray) == 0) {
echo "xmlArray没有传入数据";
} else {
$xml = self::generateData();
$this->formatXml($xml);
}
}
protected function generateData()
{
$data = self::exitData($this->xmlArray);
$xml = new SimpleXMLElement($this->root);
$tag = $xml->addChild($data[self::TAG]);
foreach ($data[self::PROPERTY] as $Property) {
$tag->addAttribute($Property[self::TEXT],$Property[self::VALUE]);
}
$this->childrenGetData($tag, $data[self::CHILD]);
$xml = $xml->asXML();
return $xml;
}
protected function childrenGetData($tag, $data)
{
if (!empty($data)) {
foreach ($data as $children) {
$childrenData = self::exitData($children);
$childrenTag = $tag->addChild($childrenData[self::TAG]);
foreach ($childrenData[self::PROPERTY] as $Property) {
$childrenTag->addAttribute($Property[self::TEXT],$Property[self::VALUE]);
}
$children = $this->arrayExists(self::CHILD, $children);
$this->childrenGetData($childrenTag, $children);
}
}
}
protected function exitData($array)
{
$Tag = $this->arrayExists(self::TAG, $array);
$Property = $this->arrayExists(self::PROPERTY, $array);
$Child = $this->arrayExists(self::CHILD, $array);
$arr = Array(self::TAG=>$Tag,self::PROPERTY=>$Property,self::CHILD=>$Child);
return $arr;
}
public function arrayExists($value, $arr)
{
if (array_key_exists($value, $arr)) {
$result = $arr[$value]?$arr[$value]:"";
} else {
$result = "";
}
return $result;
}
public function formatXml($xml)
{
$xmlDoc = new DOMDocument();
$xmlDoc->preserveWhiteSpace = false;
$xmlDoc->formatOutput = true;
$xmlDoc->loadXML($xml);
echo $xmlDoc->saveXML();
}
}
$arr = array(
"tag" => "Tag",
"property" => array(
array(
"text" => "Property",
"value" => "Property"
)
),
"children" => array(
array(
"tag" => "childrenTag",
"property" => array(
array(
"text" => "childrenProperty",
"value" => "childrenProperty"
)
),
"children" => array(
array(
"tag" => "Ta1g",
"property" => array(
array(
"text" => "Property1",
"value" => "Property1"
)
)
)
)
),
array(
"tag" => "childrenTag2",
"property" => array(
array(
"text" => "childrenProperty",
"value" => "childrenProperty"
)
),
"children" => array(
array(
"tag" => "Ta5g",
"property" => array(
array(
"text" => "Property5",
"value" => "Property5"
)
)
)
)
)
)
);
$xml= new GenerateXml();
$xml->xmlArray = $arr;
$xml->run();