<?php
$TEXTTPL = ' <xml>
<content><! [cdata[%s]]></content>
<news><! [cdata[%s]]></news>
<funcflag><! [cdata[%s]]></funcflag>
<body><! [cdata[addcon]]></body>
</xml> ';
$xmlobj =simplexml ()
$POSTOBJ = simplexml_load_string ($textTpl, ' simplexmlelement ', libxml_nocdata);//load XML, encapsulate XML as an object
Echo $POSTOBJ->content;//Get the value of an XML element
$POSTOBJ->body[0]->addchild ("date", "2016-01-01");//Add child nodes
foreach ($postObj->body->children () as $child) {
echo "Child node:". $child;//output newly added child nodes
}
or direct output with echo $POSTOBJ->body->date;
assigning values to XML elements
$string = "decimal:%s, floating-point type:%f, string:%s";
= "123";
$fnumber = "123.123";
$str = "qwee1231wqaqe3q";
$txt = sprintf ($string,, $fnumber, $str);//sprintf Replace the percent sign (%) with a variable that is passed as a parameter%s%f is the argument to replace with the variable
/*
Data format in sprintf
Percent percent-returns a percent semicolon%
%b-Binary number
The character that corresponds to the%C-ASCII value
%d-decimal number with sign (negative, 0, positive)
%e-Using the lowercase scientific notation (e.g. 1.2e+2)
%E-Use uppercase scientific notation (e.g. 1.2E+2)
%u-decimal number with no sign (greater than or equal to 0)
%f-Floating point number (local setting)
%F-Floating point number (non-local setting)
%g-Shorter%e and%f
%G-Shorter%E and%f
%o-Eight binary number
%s-String
%x-16 binary number (lowercase letters)
%x-16 binary number (uppercase
*/
$outstr =sprintf ($TEXTTPL,, $str, $fnumber);//put variables into XML
$POSTOBJ = simplexml_load_string ($outstr, ' simplexmlelement ', libxml_nocdata);//Load XML
echo $outstr;//output entire XML file
Echo $POSTOBJ->funcflag;//Get the value in XML
This article is from the IT technology blog, so be sure to keep this source http://linuxubuntu.blog.51cto.com/4964714/1721192
XML Learning Notes