PHP reads and writes XML, and php reads and writes xml. PHP reads and writes XML. php reads and writes xml. what is XML? XML is a data storage format. It does not define what data to save or define the data format. XML only defines PHP reading and writing XML, and php reading and writing xml
What is XML?
XML is a data storage format. It does not define what data to save or define the data format. XML only defines the attributes of tags and these tags. Well-formatted XML markup looks like this:
The code is as follows:
Jack Herrington
DOM reading XML
The code is as follows:
<? Php
$ Doc = new DOMDocument ();
$ Doc-> load ('books. XML ');
$ Books = $ doc-> getElementsByTagName ("book ");
Foreach ($ books as $ book)
{
$ Authors = $ book-> getElementsByTagName ("author ");
$ Author = $ authors-> item (0)-> nodeValue;
$ Publishers = $ book-> getElementsByTagName ("publisher ");
$ Publisher = $ publishers-> item (0)-> nodeValue;
$ Titles = $ book-> getElementsByTagName ("title ");
$ Title = $ titles-> item (0)-> nodeValue;
Echo "$ title-$ author-$ publisher \ n ";
}
?>
Write XML using DOM
The code is as follows:
<? Php
$ Books = array ();
$ Books [] = array (
'Title' => 'php hacks ',
'Author' => 'Jack Herrington ',
);
$ Doc = new DOMDocument (); // Create a dom object
$ Doc-> formatOutput = true;
$ R = $ doc-> createElement ("books"); // create a tag
$ Doc-> appendChild ($ r); // add the $ r tag to the xml format.
Foreach ($ books as $ book)
{
$ B = $ doc-> createElement ("book"); // create a tag
$ Author = $ doc-> createElement ("author ");
$ Author-> appendChild ($ doc-> createTextNode ($ book ['author']); // add content to the tag
$ B-> appendChild ($ author); // add the child tag to the parent tag
$ R-> appendChild ($ B); // add it to the parent tag!
}
Echo $ doc-> saveXML ();
?>
The above are the DOM code for reading and writing XML. do you know it? if you have any questions, please leave a message for me.
Http://www.bkjia.com/PHPjc/914062.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/914062.htmlTechArticlePHP reading and writing XML, php reading and writing xml what is XML? XML is a data storage format. It does not define what data to save or define the data format. XML only defines...