Several ways to write and read XML in PHP

Source: Internet
Author: User
Tags addchild

1. Use DOM to generate and read XML files
Instance 1: Copy codeThe Code is as follows: <? Php
// Creates XML string and XML document using the DOM
$ Dom = new DomDocument ('1. 0 ');
// Add root-<books>
$ Books = $ dom-> appendChild ($ dom-> createElement_x_x ('books '));
// Add <book> element to <books>
$ Book = $ books-> appendChild ($ dom-> createElement_x_x ('book '));
// Add <title> element to <book>
$ Title = $ book-> appendChild ($ dom-> createElement_x_x ('title '));
// Add <title> text node element to <title>
$ Title-> appendChild ($ dom-> createTextNode ('great American novel '));
// Generate xml
$ Dom-> formatOutput = true; // set the formatOutput attribute of domDocument to true
// Save XML as string or file
$ Test1 = $ dom-> saveXML (); // put string in test1
$ Dom-> save ('test1. xml'); // save as file
?>

Example 2:Copy codeThe Code is as follows: $ aa = "111 ";
$ Xmlstr = <XML
<? Xml version = '1. 0'?>
<Document>
<Title >{$ aa} </title>
<From> Joe </from>
<To> Jane </to>
<Body>
I know that's the answer -- but what's the question?
</Body>
</Document>
XML;
$ Dom = new domDocument;
$ Dom-> loadXML ($ xmlstr );
$ Test1 = $ dom-> saveXML ();
$ Dom-> save ('test1. xml ');

Example 3:
Test1.xml:Copy codeThe Code is as follows: <? Xml version = "1.0"?>
<Books>
<Book>
<Author> Jack Herrington </author>
<Title> PHP Hacks </title>
<Publisher> O 'Reilly </publisher>
</Book>
<Book>
<Author> Jack Herrington </author>
<Title> Podcasting Hacks </title>
<Publisher> O 'Reilly </publisher>
</Book>
</Books>

Example. php:Copy codeThe Code is as follows: $ doc = new DOMDocument ();
$ Doc-> load ('test1. 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 ";
}

Ii. Use simple to generate and read xml files
Instance 1:Copy codeThe Code is as follows: <?
$ Xmlstr = <XML
<? Xml version = '1. 0' standalone = 'Yes'?>
<Books>
<Book>
<Title> Great American Novel </title>
<Characters>
<Character>
<Name> Cliff </name>
<Desc> really great guy </desc>
</Character>
<Character>
<Name> Lovely Woman </name>
<Desc> matchless beauty </desc>
</Character>
<Character>
<Name> Loyal Dog </name>
<Desc> sleepy </desc>
</Character>
</Characters>
<Plot>
Cliff meets Lovely Woman. Loyal Dog sleeps, but wakes up to bark
At mailman.
</Plot>
<Success type = 'descrier'> 4 </success>
<Success type = 'bookclubs '> 9 </success>
</Book>
</Books>
XML;

// Extract node content
$ Xml = new SimpleXMLElement ($ xmlstr );
Foreach ($ xml-> book [0]-> success as $ success ){
Switch (string) $ success ['type']) {// Get attributes as element indices
Case 'descrier ':
Echo $ success. 'months on bestseller list <br> ';
Break;
Case 'bookclubs ':
Echo $ success. 'bookclub listings ';
Break;
}
}

// Modify the content of a text node
$ Xml = new SimpleXMLElement ($ xmlstr );
$ Xml-> book [0]-> characters-> character [0]-> name = 'Big cliff ';
Echo $ xml-> asXML ();

// Add the text node of the child element
$ Xml = new SimpleXMLElement ($ xmlstr );
$ Character = $ xml-> book [0]-> characters-> addChild ('character ');
$ Character-> addChild ('name', 'Yellow cat ');
$ Character-> addChild ('desc', 'aloof ');
$ Success = $ xml-> book [0]-> addChild ('success ', '2 ');
$ Success-> addattrits ('type', 'reprints ');
Echo $ xml-> asXML ();

?>

Example 2:Copy codeThe Code is as follows: if (file_exists ('test1. xml') {// read the xml file
$ Xml = simplexml_load_file ('test1. xml ');
Var_dump (xml );
} Else {
Exit ('failed' to open test1.xml .');
}

Iii. DOM and simple interoperability
DOM import simpleXML:Copy codeThe Code is as follows: <? Php
$ Sxe = simplexml_load_string ('<books> <book> <title> Great American
Novel </title> </book> </books> ');
If ($ sxe ===false ){
Echo 'error while parsing the document ';
Exit;
}
$ Dom_sxe = dom_import_simplexml ($ sxe );
If (! $ Dom_sxe ){
Echo 'error while converting xml ';
Exit;
}
$ Dom = new DOMDocument ('1. 0 ');
$ Dom_sxe = $ dom-> importNode ($ dom_sxe, true );
$ Dom_sxe = $ dom-> appendChild ($ dom_sxe );
$ Test2 = $ dom-> saveXML (); // put string in test2
$ Dom-> save ('test2. xml'); // save as file
?>

SimpleXML import DOM:Copy codeThe Code is as follows: <? Php
$ Dom = new domDocument;
$ Dom-> loadXML ('<books> <book> <title> Great American
Novel </title> </book> </books> ');
If (! $ Dom ){
Echo 'error while parsing the document ';
Exit;
}
$ S = simplexml_import_dom ($ dom );
Echo $ s-> book [0]-> title; // Great American Novel
?>

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.