使用PHP DOM-XML建立和解析XML檔案

來源:互聯網
上載者:User

使用PHP DOM-XML建立和解析XML檔案

<?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
*
*/
// 使用PHP DOM-XML建立和解析XML檔案

//建立XML文檔對象;以後的處理過程將在此基礎上進行
$doc = new_xmldoc("1.0" );

//建立根節點,並設定一個屬性
$root = $doc->add_root("faq" );
$root->setattr("page", "32" );

//子節點
$one = $root->new_child("question", "");
//為子節點設定屬性
$one->setattr("number", "1");
//question也建立子節點,並且給它賦值
$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?" );
// 建立一個不直接賦值的節點
$twoone = $two->new_child("answer", "");
// 然後給它單獨賦值
$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/>");
// 第二個text node對象
$foo = xpath_eval($ctx, "//text[2]");
print_r($foo);
print("<br/><br/>");
// 第三個answer對象
$foo = xpath_eval($ctx, "//answer[3]");
print_r($foo);
print("<br/><br/>");

//第三個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);

?>

需要說明,PHP DOM 只能在PHP PHP4.0.x + linux上運行

PHPDOM類庫請到http://www.zend.com/download下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.