-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml_merger.php
133 lines (126 loc) · 4.16 KB
/
xml_merger.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
<?php
/**
* Webkul Software.
*
* @package Webkul_CodeGenerator
* @author Mahesh Singh
*/
/**
* XmlGenerator class
*/
class XmlGenerator {
/**
* Add new node to parent node
*
* @param SimpleXMLElement $parentNode
* @param string $childNode
* @param null|array $nodeValue
* @param array $attributes
* @param bool $idAttribute
* @return Element
*/
public function addXmlNode(
$parentNode,
$childNode,
$nodeValue = null,
$attributes = [],
$idAttribute = false,
$isUniqueNode = false
) {
if (!$parentNode instanceof \SimpleXMLElement){
$parentNode = new \SimpleXMLElement($parentNode);
}
if ($isUniqueNode) {
$removeNode = $parentNode->xpath("//{$childNode}");
if (isset($removeNode[0])) {
$this->removeNode($removeNode[0]);
}
}
if (is_array($nodeValue)) {
if ($idAttribute) {
$tempParent = simplexml_load_string($parentNode->asXml());
$parentName = $parentNode->getName();
$matchedNode = $tempParent->xpath("//{$childNode}[@{$idAttribute}='{$attributes[$idAttribute]}']");
$newNode = $parentNode->addChild($childNode);
if (isset($matchedNode[0]) && $matchedNode[0] instanceof \SimpleXMLElement) {
$this->replaceChild($newNode, $matchedNode[0]);
}
} else {
$newNode = $parentNode->addChild($childNode);
}
foreach ($nodeValue as $nodeKey => $value) {
$this->addXmlNode($newNode, $nodeKey, $value);
}
} else {
if ($idAttribute) {
$parentName = $parentNode->getName();
$matchedNode = $parentNode->xpath("//{$childNode}[@{$idAttribute}='{$attributes[$idAttribute]}']");
$newNode = $parentNode->addChild($childNode, $nodeValue?:'');
if (isset($matchedNode[0]) && $matchedNode[0] instanceof \SimpleXMLElement) {
$this->replaceChild($newNode, $matchedNode[0]);
}
} else {
$newNode = $parentNode->addChild($childNode, $nodeValue?:'');
}
}
if (!empty($attributes)) {
foreach ($attributes as $attribute => $value) {
$newNode->addAttribute($attribute, $value);
}
}
return $newNode;
}
/**
* Remove xml node
*
* @param Element $removeNode
* @return void
*/
public function removeNode($removeNode)
{
$node = dom_import_simplexml($removeNode);
$node->parentNode->removeChild($node);
return $this;
}
/**
* replace a child Element with another Element
* @param SimpleXMLElement $newChild
* @param SimpleXMLElement $oldChild
* @return $this
*/
public function replaceChild(\SimpleXMLElement &$newChild, \SimpleXMLElement $oldChild)
{
list($oldChild, $_newChild) = $this->getSameTypeDomNodes($oldChild, $newChild);
$oldChild->parentNode->replaceChild($_newChild, $oldChild);
}
/**
* Utility method to get two dom elements
* And ensure that the second is part of the same document than the first given.
* @param SimpleXMLElement $node1
* @param SimpleXMLElement $node2
* @return array
*/
public function getSameTypeDomNodes(\SimpleXMLElement $newChild, \SimpleXMLElement $oldChild)
{
$newChild = dom_import_simplexml($newChild);
$oldChild = dom_import_simplexml($oldChild);
if(! $newChild->ownerDocument->isSameNode($oldChild->ownerDocument) ) {
$oldChild = $newChild->ownerDocument->importNode($oldChild, true);
}
return [$newChild, $oldChild];
}
/**
* Format xml string
*
* @param string $content
* @return string
*/
public function formatXml($content)
{
$dom = new \DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($content);
return $dom->saveXML();
}
}