I xml_encode my array using this code.
public function xml_encode($mixed, $domElement=null, $DOMDocument=null) { if (is_null($DOMDocument)) { $DOMDocument =new DOMDocument; $DOMDocument->formatOutput = true; $this->xml_encode($mixed, $DOMDocument, $DOMDocument); echo $DOMDocument->saveXML(); } else { // To cope with embedded objects if (is_object($mixed)) { $mixed = get_object_vars($mixed); } if (is_array($mixed)) { foreach ($mixed as $index => $mixedElement) { if (is_int($index)) { if ($index === 0) { $node = $domElement; } else { $node = $DOMDocument->createElement($domElement->tagName); $domElement->parentNode->appendChild($node); } } else { $plural = $DOMDocument->createElement($index); $domElement->appendChild($plural); $node = $plural; if (!(rtrim($index, 's') === $index)) { $singular = $DOMDocument->createElement(rtrim($index, 's')); $plural->appendChild($singular); $node = $singular; } } $this->xml_encode($mixedElement, $node, $DOMDocument); } } else { $mixed = is_bool($mixed) ? ($mixed ? 'true' : 'false') : $mixed; $domElement->appendChild($DOMDocument->createTextNode($mixed)); } } } and it works well. Now, I want to set an attribute to a specific tag.
Let's say for example this is my xml.
<book> <title>PHP Programming</title> <description>Learn how to code in PHP</description> </book> I want to set an attribute to title tag and make it
<title name=attributeValue> PHP Programming </title> How can I achieve this?
Thanks!
No comments:
Post a Comment