The PHP5 enhances XML support and uses the DOM to extend the energy resistance of XML operations. These functions are part of the PHP5 core without being installed.
The following example shows the operation of the DOM on XML, explained in detail, please see the comments in the code / ********************************** ***************************** USE XML in PHP5 ** Reference Site: ** http://cn.php.net/manual/zh/ref p om o 5 5 5 om om.. *********** / / First to create a DomDocument object $ DOM = new domdocument (); // then load XML file $ DOM -> LOAD ("test.xml"); // Output XML file // header ("Content-Type: Text / XML; CHARSET = GB2312"); // Echo $ DOM -> Savexml (); // Save XML file, return value Int (file size, byte Unit) // $ DOM -> Save ("newfile.xml"); echo "
1] / Title Returns all / RSS / Channel / Item / Title [@ID = '23'] returns all Articles with all Articles containing the ID attribute and the value of 23. Element below Title (Translator Note: & Folder & Represents Directory Depth) * / / Write new data to DOM $ item = $ DOM-> CreateElement ("item"); $ title = $ dom-> CreateElement ("Title "); $ titleText = $ DOM-> CreateTextNode (" Title Text "); $ Title-> Appendchild ($ Titletext); $ item-> appendchild ($ title); $ dom-> documentelement-> getElementsBytagname ('Channel' ) -> item (0) -> appendchild ($ item); // Remove node from DOM / / $ DOM-> DocumentElement-> RemoveChild ($ DOM-> DocumentElement-> getElementsBytagname ("CHANNEL") -> ITEM 0); // or use XPath to query the node and then delete / / $ dom-> DocumentElement-> RemoveChild ($ xpath-> query ("/ ts / channel") -> item (0)); // $ DOM -> Save ("newfile.xml"); // Modify Node Data / / Modify the first title data from DOM // This place is stupid, and create a node and then replace the old node. If a friend has other good way, please tell me $ firsttitle = $ xpath-> query ("/ rss / channel / item / title") -> item (0); $ newTITETELE = $ DOM-> CreateElement "Title"; $ newTitle-> appendchild (New Domtext ("This's The New Title Text !!!)); $ firstTitle-> ParentNode-> Replacechild ($ newTitle, $ firsttitle); // Modify Properties / / $ FirstTITITLE = $ XPATH-> Query ("/ RSS / Channel / Item / Title") -> Item (0); // $ firstTTITLE-> SetAttribute ("Orderby", "4"); $ DOM-> Save (" Newfile.xml "); echo"
/ * "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" /?> The following is Test.xml file code: XML Version = "1.0" encoding = "GB2312"?>