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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Shipping\Model\Simplexml;
/**
* Extends SimpleXML to add valuable functionality to \SimpleXMLElement class
*
*/
class Element extends \Magento\Framework\Simplexml\Element
{
/**
* Adds an attribute to the SimpleXML element
*
* @param string $name The name of the attribute to add.
* @param string $value If specified, the value of the attribute.
* @param string $namespace If specified, the namespace to which the attribute belongs.
* @return void
*/
public function addAttribute($name, $value = null, $namespace = null)
{
if ($value !== null) {
$value = $this->xmlentities($value);
}
parent::addAttribute($name, $value, $namespace);
}
/**
* Adds a child element to the XML node
*
* @param string $name The name of the child element to add.
* @param string $value If specified, the value of the child element.
* @param string $namespace If specified, the namespace to which the child element belongs.
* @return \Magento\Shipping\Model\Simplexml\Element
*/
public function addChild($name, $value = null, $namespace = null)
{
if ($value !== null) {
$value = $this->xmlentities($value);
}
return parent::addChild($name, $value, $namespace);
}
/**
* Converts meaningful xml characters to xml entities
*
* @param string|null $value
* @return string
*/
public function xmlentities($value = null)
{
$value = str_replace('&', '&', $value);
$value = str_replace('&', '&', $value);
return $value;
}
}