You can write and read XML in PHP. 1. use DOM to generate and read XML files. Example 1: Copy the code as follows :? PhpCreatesXMLstringandXMLdocumentusingtheDOM $ domnewDomDocument (1.0); addroot-bo 1. use DOM to generate and read XML files
Instance 1:
The code is as follows:
// Creates XML string and XML document using the DOM
$ Dom = new DomDocument ('1. 0 ');
// Add root-
$ Books = $ dom-> appendChild ($ dom-> createElement_x_x ('books '));
// Add Element
$ Book = $ books-> appendChild ($ dom-> createElement_x_x ('book '));
// Add Element to <book> <BR> $ title = $ book-> appendChild ($ dom-> createElement_x_x ('title ')); <BR> // add <title> text node element to <title> <BR> $ title-> appendChild ($ dom-> createTextNode ('great American Novel ')); <BR> // generate xml <BR> $ dom-> formatOutput = true; // set the formatOutput attribute of domDocument to true <BR> // save XML as string or file <BR> $ test1 = $ dom-> saveXML (); // put string in test1 <BR> $ Dom-> save ('test1. XML'); // save as file <BR >?> <BR> </p> <BR> instance 2: <BR> <p class = "codetitle"> <U> </U> the code is as follows: </p> <p class = "codebody" id = "code49077"> <BR> $ aa = "111 "; <BR> $ xmlstr = <XML <BR> <? Xml version = '1. 0'?> <BR> <document> <BR> <title> {$ aa}
Joe
Jane
I know that's the answer -- but what's the question?
XML;
$ Dom = new domDocument;
$ Dom-> loadXML ($ xmlstr );
$ Test1 = $ dom-> saveXML ();
$ Dom-> save ('test1. XML ');
Example 3:
Test1.xml:
The code is as follows:
Jack Herrington
PHP Hacks
O 'Reilly
Jack Herrington
Podcasting Hacks
O 'Reilly
Example. php:
The 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:
The code is as follows:
$ Xmlstr = <
Great American Novel
Cliff
Really great guy
Lovely Woman
Matchless beauty
Loyal Dog
Sleepy
Cliff meets Lovely Woman. Loyal Dog sleeps, but wakes up to bark
At mailman.
4
9
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
';
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:
The 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:
The code is as follows:
$ Sxe = simplexml_load_string (' Great American <BR> Novel ');
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:
The code is as follows:
$ Dom = new domDocument;
$ Dom-> loadXML (' Great American <BR> Novel ');
If (! $ Dom ){
Echo 'error while parsing the document ';
Exit;
}
$ S = simplexml_import_dom ($ dom );
Echo $ s-> book [0]-> title; // Great American Novel
?>
Example 1: The code is as follows :? Php // Creates XML string and XML document using the DOM $ dom = new DomDocument ('1. 0'); // add root-bo...