PHP XML read-write creation

Source: Internet
Author: User
Tags cdata

One, XML read

1.1. First define the good one XML file with the directory:

Book.xml

<?XML version= "1.0" encoding= "Utf-8"?><Books>  < Book>    <ID>1</ID>    <name>Book 001</name>  </ Book>    < Book>    <ID>2</ID>    <name>Book 002</name>  </ Book>    < Book>    <ID>3</ID>        <name>Book 003</name>  </ Book>    <title>This is the title</title></Books>

1.2 Reading XML via getElementsByTagName

$xml=NewDOMDocument ();$xml->load ("Book.xml");//Read through getElementsByTagNameforeach($xml->getelementsbytagname (' book ') as $book){    $id=$book->getelementsbytagname ("id"); $name=$book->getelementsbytagname ("name"); Echo"ID:".$id->item (0)->nodevalue. ", Name:".$name->item (0)->nodevalue. " <br/> ";}

1.3 Direct Read Properties via Simplexml_import_dom

//   directly read the attribute $simplexmlsimplexml_import_dom($xml) via simplexml_import_dom; Echo "Sid->title:". $simplexml->title;
Echo  "The ID is:". $simplexml->book[0]->id;
Echo "The ID is:". $simplexml->title;

Ii. creation of XML

2.1. Creating an XML document element from a string

$xmlString=<<<XML<?xml version= "1.0" encoding= "Utf-8"?><books>  < book>    <id>1</id>    <name> book 001</name>  </book>    <book>    <id>2</id>    <name> book 002</name>  </book>    <book>    <id>3 </id>        <name> book 003</name>  </book>    <title> This is the title </title></ Books>XML; $dom=new  DomDocument; $dom->loadxml ($xmlString);

2.2 Creating XML objects through the DOMDocument API (child nodes, node properties, CDATA attribute value tags)

classbuildxml{/** Create an XML element **/    Private functionCreatexml () {$dom=NewDOMDocument ("1.0"); $books=$dom->createelement ("Books");  for($i= 0;$i< 4;$i++) {            $book=$dom->createelement ("book");//add a property to the book node            $price=$dom->createattribute ("Price"); $priceValue=$dom->createtextnode ($i* 10); $price->appendchild ($priceValue); $book->appendchild ($price);//add an ID contact element and assign a value            $id=$dom->createelement ("id"); $idValue=$dom->createtextnode ($i); $id->appendchild ($idValue); $book->appendchild ($id);//add a content to be CDATA identified            $title=$dom->createelement ("title"); $titleValue=$dom->createcdatasection ("This is a content with a CDATA label"); $title->appendchild ($titleValue); $book->appendchild ($title); $books->appendchild ($book); }        $dom->appendchild ($books); return $dom-SaveXML (); }    //Output XML     Public functionPrintxml () {Header("Content-type:text/xml"); Echo $this-Createxml (); }    //Save XML     Public functionSaveXML () {$result=false; Try {            //Open the file you want to write XML data to            $fp=fopen("Newxml.xml", "W"); //Writing XML Data            fwrite($fp,$this-createxml ()); //Close File            fclose($fp); $result=true; } Catch(Exception $e) {            Print $e-GetMessage (); Exit(); }        return $result; }}

2.3 HTTP Output XML

Require_once "buildxml.php";
$xml New  echo$xml->printxml ();

2.4 Saved XML in file form

require_once "buildxml.php"; $xml New buildXML; $xml->savexml ();

PHP XML read-write creation

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.