When we create XML files and parse them, we usually use
PHP DOM-XML application code example:
- < ?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
- *
- */
- // Use PHP DOM-XML to create and parse XML files
- //Create XML document object; Future processing will be carried out on this basis
- $doc = new_xmldoc("1.0" );
- //Create a root node and set an attribute
- $root = $doc->add_root("faq" );
-
$root-> setattr("page", "32" );
- //child node
-
$one = $root->new_child("question", "");
- //Set attributes for child nodes
-
$one-> setattr("number", "1");
- //question also creates a child node and assigns a value to it
-
$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 without direct assignment
-
$twoone = $two->new_child("answer" , "");
- // Then assign it a separate value
-
$twoone->set_content("DIR is the libxml install directory
- (if you 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 to use DOM XML function ?" );
-
$three->new_child("answer", "Read this document source for
- a simple example." );
- //输出到Browser
-
print("< pre>".htmlspecialchars($doc->dumpmem() )."< /pre>" );
- // write to file
- //写回到文件
-
$fp = fopen("test_dom.xml", "w+" );
-
fwrite($fp, $doc->dumpmem(), strlen($doc->dumpmem() ));
- fclose($fp);
- //现在使用xpath从XML文档中得到内容
-
$doc = xmldoc(join("", file("test_dom.xml")) );
-
$ctx = xpath_new_context($doc );
- //所有对象
-
$foo = xpath_eval($ctx, "//child::*");
- print_r($foo);
-
print("< br/>< br/>");
- //text node 对象
-
$foo = xpath_eval($ctx, "//text");
- print_r($foo);
-
print("< br/>< br/>");
- // 第一个text node对象
-
$foo = xpath_eval($ctx, "//text[1]");
- print_r($foo);
-
print("< br/>< br/>");
- // The second text node object
-
$foo = xpath_eval($ctx, "//text[2]");
-
print_r($foo ); 🎜>br
- />< br/>"); // The third answer object $foo =
xpath_eval-
($ctx, "// answer[3]");
- print_r($foo); print("<
- br
- />< br/>"); //The type, name and content of the third text node $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) ;
- ?> Need explanation, PHP DOM-XML only runs on PHPPHP4.0.x + linux
-
http://www.bkjia.com/PHPjc/446182.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446182.html
TechArticle
When we create XML files and parse them, we usually use PHP DOM-XML application code examples : ?php /** *Topic:CreateandparseXMLfilesusingPHPDOM-XML *Source:http://www.p...