Xml crud operations for xml operations in PHP

Source: Internet
Author: User
Refer code page, the code is as follows: htmlgt; headgt; nbsp... This article introduces a PHP crud operation for operating xml programming. for details, refer.

The html code page is as follows:

      
   Add word  

The wordpress. php file has the following code:

 Load ("words. xml "); // Determine if ($ type =" query ") {// obtain the user input value $ enword = $ _ REQUEST ['enword']; // determine whether to query $ isEnter = false; // Obtain all word nodes $ words = $ doc-> getElementsByTagName ("word "); // traverse the word node for ($ I = 0; $ I <$ words-> length; $ I ++) {$ word_node = $ words-> item ($ I ); // get different languages $ en_word = getNodeVal ($ word_node, "en"); $ zh_word = getNodeVal ($ word_node, "zh "); // query if ($ enword = $ en_word) {$ isEnter = true; echo $ enword. :". getNodeVal ($ word_node, "zh"); echo"
Return to continue query ";} else if ($ enword = $ zh_word) {$ isEnter = true; echo $ enword. :". getNodeVal ($ word_node, "en"); echo"
Return to continue querying ";}} if (! $ IsEnter) {echo "cannot be queried"; echo"
Return to continue querying ";}} else if ($ type =" Add ") {// receive $ enword =$ _ REQUEST ['enword']; $ zhword = $ _ REQUEST ['zhword']; if (! Emptyempty ($ enword )&&! Emptyempty ($ zhword) {// get the root node $ root = $ doc-> getElementsByTagName ("words")-> item (0 ); $ word = $ doc-> createElement ("word"); $ en = $ doc-> createElement ("en", $ enword ); $ zh = $ doc-> createElement ("zh", $ zhword); // Mount $ root-> appendChild ($ word ); $ word-> appendChild ($ en); $ word-> appendChild ($ zh); // save the xml file $ doc-> save ("words. xml "); echo" added successfully
Return to continue operation ";} else {echo" Enter the word "; echo"
Return to continue operation "; exit () ;}} else if ($ type =" delete ") {$ word =$ _ REQUEST ['word']; // get all word nodes $ words = $ doc-> getElementsByTagName ("word"); $ isEnter = false; // traverse the word node for ($ I = 0; $ I <$ words-> length; $ I ++) {$ word_node = $ words-> item ($ I ); // get different languages $ en_word = getNodeVal ($ word_node, "en"); $ zh_word = getNodeVal ($ word_node, "zh "); // query if ($ word = $ en_word | $ word = $ zh_word) {$ isEnter = true; // find the parent node $ word_node-> parentNode-> removeChild ($ word_node); $ doc-> save ("words. xml "); echo" deleted successfully
Return to continue operation ";}} if (! $ IsEnter) {echo "operation failed"; echo"
Return to continue operation ";}} else if ($ type =" modify ") {// receive $ enword =$ _ REQUEST ['enword']; $ zhword = $ _ REQUEST ['zhword']; if (! Emptyempty ($ enword )&&! Emptyempty ($ zhword) {// get all word nodes $ words = $ doc-> getElementsByTagName ("word"); // traverse word nodes $ isEnter = false; for ($ I = 0; $ I <$ words-> length; $ I ++) {$ word_node = $ words-> item ($ I ); // get different languages $ en_word = getNodeVal ($ word_node, "en"); $ zh_word = getNodeVal ($ word_node, "zh "); // query if ($ enword ==$ en_word & $ zhword! = $ Zh_word) {// modify Chinese $ isEnter = true; // Obtain zh node $ zh = $ word_node-> getElementsByTagName ("zh")-> item (0 ); $ zh-> nodeValue = $ zhword; $ doc-> save ("words. xml "); echo" modified successfully "; echo"
Return to continue operation ";} else if ($ enword! = $ En_word & $ zhword = $ zh_word) {// modify because $ isEnter = true; $ en = $ word_node-> getElementsByTagName ("en ") -> item (0); $ en-> nodeValue = $ enword; $ doc-> save ("words. xml "); echo" modified successfully "; echo"
Return to continue operation ";}} if (! $ IsEnter) {echo "no changes made"; echo"
Return to continue operation ";}} else {echo" Enter the word to be modified "; echo"
Return to continue operation "; exit () ;}/// open-source phprm.com // Obtain the node's text value function getNodeVal (& $ MyNode, $ tagName) {return $ MyNode-> getElementsByTagName ($ tagName)-> item (0)-> nodeValue ;}

The code is as follows:

 
 
  
   
Boy
  
  
   
Boy
  
 
 
  
   
Girl
  
  
   
Girl
  
 
 
  
   
Fire
  
  
   
Fire
  
 
 
  
   
Word
  
  
   
Lexicon
  
  

Permanent address:

Reprint at will ~ Please bring the tutorial URL ^

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.