<?php
class XMLMsgWriter
{
protected $XMLWriter = null;
function __construct()
{
$this->XMLWriter = new XMLWriter();
}
public function write($content)
{
if (is_scalar($content))
{
// (标量)string, integer, float, boolean
$this->XMLWriter->text((string) $content);
}
elseif (is_callable($content))
{
//a callback
$content($this);
}
elseif (is_array($content) && array_key_exists('name', $content))
{
$name = $content['name'];
$attributes = isset($content['attributes']) ? $content['attributes'] : [];
$value = isset($content['value']) ? $content['value'] : null;
$this->XMLWriter->startElement($name);
$this->writeAttributes($attributes);
$this->write($value);
$this->XMLWriter->endElement();
}
else if (is_array($content))
{
foreach ($content as $name => $item)
{
if (is_int($name))
{
$this->write($item);
}
else if (is_string($name) && is_array($item) && isset($item['attributes']))
{
$attributes = isset($item['attributes']) ? $item['attributes'] : [];
// The key is used for a name, but $item possibly has 'attributes' and 'value'
$this->XMLWriter->startElement($name);
$this->writeAttributes($attributes);
if (isset($item['value']))
{
$this->write($item['value']);
}
$this->XMLWriter->endElement();
}
elseif (is_string($name))
{
//key-value
$this->XMLWriter->startElement($name);
$this->write($item);
$this->XMLWriter->endElement();
}
}
}
}
private function writeAttributes($attributes = array())
{
if (!empty($attributes))
{
foreach ($attributes as $name => $value)
{
$this->XMLWriter->writeAttribute($name, $value);
}
}
}
public function openMemory()
{
$this->XMLWriter->openMemory();
}
public function startDocument()
{
$this->XMLWriter->startDocument('1.0', 'utf-8'); // xml文档开始
}
public function endDocument()
{
$this->XMLWriter->endDocument();
}
public function outputMemory()
{
$xml = $this->XMLWriter->outputMemory();
return $xml;
}
public function writeTest()
{
$this->write(array(
array(
'name' => 'title',
'value' => 'Apple iPhone 11 128GB'
),
array(
'name' => 'price',
'value' => '1399.00'
),
));
}
}
$xmlMsgWriter = new XMLMsgWriter();
$xmlMsgWriter->openMemory();
$xmlMsgWriter->startDocument();
$xmlMsgWriter->write(array(
'name' => 'root',
'attributes' => array(
'code' => '1',
'extension' => '1'
),
'value' => array(
'name' => 'products',
'value' => array(array(
'name' => 'product',
'value' => array(
array(
'name' => 'title',
'value' => 'Apple iPhone 11 64GB'
),
array(
'name' => 'price',
'value' => '699.00'
),
),
), array(
'name' => 'product',
'value' => function ($xmlMsgWriter)
{
$xmlMsgWriter->writeTest();
}
)
)
)
));
$xmlMsgWriter->endDocument();
$xml = $xmlMsgWriter->outputMemory();
print_r($xml);
修改属性
/**
*
* @param type $source 源XML字符串或文件
* @param type $path 要修改的标签所在的路径(/root/id)
* @param type $attr 属性键值对
* @param type $fillText 是否是对值进行修改
* @return type 返回目标XML字符串。若path路径不匹配,则返回源XML字符串
*/
function setSegAttribute($source, $path, $attr = array(), $fillText = false)
{
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
if (is_file($source))
{
$dom->load($source);
}
else
{
$dom->loadXML($source);
}
$xpath = new DOMXPath($dom);
$rootNamespace = $dom->documentElement->lookupNamespaceUri($dom->namespaceURI);
if (!is_null($rootNamespace))
{
$xpath->registerNamespace('x', $rootNamespace);
$arrNewPath = array();
$arrPath = explode('/', $path);
foreach ($arrPath as $v)
{
if (!empty($v))
{
$v = 'x:' . $v;
}
$arrNewPath[] = $v;
}
$newPath = implode('/', $arrNewPath);
}
else
{
$newPath = $path;
}
$nodeList = $xpath->query($newPath);
if (count($nodeList) == 1)
{
foreach ($attr as $key => $value)
{
if ($key == 'text' && $fillText == true)
{
$nodeList->item(0)->nodeValue = $value;
}
else
{
$attributes = $nodeList->item(0)->attributes;
foreach ($attributes as $attribute)
{
$attributeName = $attribute->nodeName;
if ($key == $attributeName)
{
$attribute->nodeValue = $value;
}
}
}
}
}
$xmlMsg = $dom->saveXML();
return $xmlMsg;
}
$xml2 = setSegAttribute($xml, '//id', array('id' => 'id'));
print_r($xml2);