Create and parse XML files using PHP dom-xml _xml/rss

Source: Internet
Author: User



uses PHP dom-xml to create and parse XML files <?php


  /**


* Topic:create and parse XML files using PHP dom-xml


* Source:http://www.php.net/domxml


* reference:http://www.zugeschaut-und-mitgebaut.de/php/extension.domxml.html


* author:urs@circle.ch, 16-1-2001


  *


  */  


//using PHP dom-xml to create and parse XML files





//Create an XML Document object, and subsequent processing will be done on this basis


$doc = New_xmldoc ("1.0");





//Create the root node and set a property


$root = $doc->add_root ("FAQ");


$root->setattr ("page", "32");





//child node


$one = $root->new_child ("question", "");


//Set properties for child nodes


$one->setattr ("number", "1");


//question also creates a child node and assigns it a value


$one->new_child ("text", "1.")   Where to get libxml-2.0.0? ");


$one->new_child ("Answer", "You can download the latest


release of Libxml either as a source archive or


RPM package from http://www.xmlsoft.org.


the current version is libxml2-2.2.1. ");





$two = $root->new_child ("question", "");


$two->setattr ("number", "2");


$two->new_child ("text", "2.")   How to configure PHP4? ");


//Create a node that does not directly assign value


$twoone = $two->new_child ("Answer", "");


//Then assign the value to it individually


$twoone->set_content ("DIR is" libxml install directory


(if you are just use--with-dom it defaults


to/usr), I needed to use--with-dom=/usr/local ");





$three = $root->new_child ("question", "");


$three->setattr ("number", "7");


$three->new_child ("text", "7.")   How do I use DOM XML function? ");


$three->new_child ("Answer", "Read this document source for


a simple example. ");





//Output to browser


print ("<pre>" Htmlspecialchars ($doc->dumpmem ()). "   </pre> ");





Write to File


/write back to file


$fp = fopen ("Test_dom.xml", "w+");


fwrite ($fp, $doc->dumpmem (), strlen ($doc->dumpmem ()));


fclose ($FP);





  // ------------------------------------------------------


//Now use XPath to get content from XML documents





$doc = xmldoc (Join ("", File ("Test_dom.xml"));


$ctx = Xpath_new_context ($doc);





//All Objects


$foo = Xpath_eval ($ctx, "//child::*");


Print_r ($foo);


print ("<br/><br/>");


//text Node Object


$foo = Xpath_eval ($ctx, "//text");


Print_r ($foo);


print ("<br/><br/>");


//First text node object


$foo = Xpath_eval ($ctx, "//text[1]");


Print_r ($foo);


print ("<br/><br/>");


//Second Text node object


$foo = Xpath_eval ($ctx, "//text[2]");


Print_r ($foo);


print ("<br/><br/>");


//Third Answer object


$foo = Xpath_eval ($ctx, "//answer[3]");


Print_r ($foo);


print ("<br/><br/>");





//Third text node type, name and content


$foo = Xpath_eval ($ctx, "//text[3]");


$tmp = $foo->nodeset;


Print_r ($tmp);


print ("<br/>");


print ($tmp [0]->type).   "; ";


print ($tmp [0]->name).   "; ";


print ($tmp [0]->content);





?>





needs to be explained that the PHP DOM can only run on PHP php4.0.x + linux





Phpdom class Library please go to http://www.zend.com/download download


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.