PHP reads and writes XML, php reads and writes xml_PHP tutorials

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

Related Article

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.