PHP generates XML just like a tree, adding nodes one by one, adding multiple child nodes under a parent node.
function Madexml ()
{
Get template information
$strTempInfo = $this->modelcmsobj->gettemplate (2007);
$arrTemp = Explode ("#", $strTempInfo);
Array_shift ($arrTemp);
$arrContents = Array ();
foreach ($arrTemp as $k = $v)
{
$arrContents [$k]=explode (",", $v);
}
Parsing to an XML file
$objDom = new DOMDocument ("1.0");
Header ("Content-type:text/plain");
Adding elements and Text nodes
$root = $objDom->createelement ("recommend");
$objDom->appendchild ($root);
foreach ($arrContents as $k = $v)
{
$item = $objDom->createelement ("entry");
$root->appendchild ($item);
$nextitem 1 = $objDom->createelement ("Simgurl");
$nextitem 2 = $objDom->createelement ("Imgurl");
$nextitem 3 = $objDom->createelement ("FileURL");
$item->appendchild ($nextitem 1);
$item->appendchild ($nextitem 2);
$item->appendchild ($nextitem 3);
$text 1 = $objDom->createtextnode ($v [0]);
$text = $objDom->createtextnode ($v [1]);
$text 2 = $objDom->createtextnode ($v [2]);
$nextitem 2->appendchild ($text 1);
$nextitem 3->appendchild ($text 2);
$nextitem 1->appendchild ($text);
}
echo $objDom->savexml ();
}
Simplexml_load_string converting XML strings to Strings
$string = <<
Forty what?
Joe
Jane
I know thats the answer--but whats the question?
XML;
$xml = simplexml_load_string ($string);
Var_dump ($xml);
?>
This script would display:
SimpleXMLElement Object
(
[Title] = Forty What?
[From] = Joe
[To] = Jane
[Body] =
I know thats the answer--but whats the question?
)
If you want to convert the XML to an array, convert the string to a string containing the key value, and then loop it to become an arrays.
/**
* XML conversions to arrays
* @param unknown_type $xml
*/
Private Function Xml_to_array ($xml)
{
$array = (Array) (Simplexml_load_string ($xml, SimpleXMLElement, Libxml_nocdata));
foreach ($array as $key = = $item) {
$array [$key] = $this->struct_to_array ((array) $item);
}
return $array;
}
Private Function Struct_to_array ($item) {
if (!is_string ($item)) {
$item = (array) $item;
foreach ($item as $key = = $val) {
$item [$key] = Self::struct_to_array ($val);
}
}
return $item;
}
http://www.bkjia.com/PHPjc/486096.html www.bkjia.com true http://www.bkjia.com/PHPjc/486096.html techarticle PHP generates XML just like a tree, adding nodes one by one, you can add multiple child nodes under a parent node, function Madexml () {//get template information $strTempInfo = $this-modelcmsobj-get ...