Tutorial on PHP Generating XML source code _php

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.