首頁 >後端開發 >XML/RSS教程 >PHP擴充之XML操作(一)-SimpleXML

PHP擴充之XML操作(一)-SimpleXML

黄舟
黄舟原創
2018-05-17 09:38:185235瀏覽


一、簡介及安裝

SimpleXML 擴充功能提供了一個非常簡單且易於使用的工具集,能將 XML 轉換成一個帶有一般屬性選擇器和陣列迭代器的物件。

此擴充功能需要 libxml PHP 擴充。這表示需要使用 --enable-libxml ,儘管這將隱式完成因為 libxml 是缺省開啟的。

需要PHP5以上版本。

此擴充功能預設為啟用,編譯時可透過下列選項停用: --disable-simplexml

二、使用範例

基本使用

Example #1 Include 基本使用

Example #1 Include

基本使用 The simplicity of SimpleXML appears most clearly when one extracts a string or number from a basic XML document.

Example #2 Getting 

Example #2 Getting 

an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.例程會輸出:

<?php
$xmlstr = <<<XML
<?xml version=&#39;1.0&#39; standalone=&#39;yes&#39;?>
<movies>
<movie>
<title>PHP: Behind the Parser</title>
<characters>
<character>
<name>Ms. Coder</name>
<actor>Onlivia Actora</actor>
</character>
<character>
<name>Mr. Coder</name>
<actor>El Act&#211;r</actor>
</character>
</characters>
<plot>
So, this language. It&#39;s like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
</plot>
<great-lines>
<line>PHP solves all my web problems</line>
</great-lines>
<rating type="thumbs">7</rating>
<rating type="stars">5</rating>
</movie>
</movies>
XML;
?>

Example #4 Accessing non-unique elements in SimpleXML

When multiple instances of an element exist as children of a 以上reee

Note:

Properties ($movies->movie in previous example) are not arrays. They are iterable and accessible objects.

only covered the work of reading element names and their values. SimpleXML can also access element attributes. Access attributes of an element just as you would elements of an array.

<?phpinclude &#39;example.php&#39;;
$movies = new SimpleXMLElement($xmlstr);
echo $movies->movie[0]->plot;?>

r

To compare an element or attribute with a string or pass it into a function that requires a string, you must cast it to a string using (string).上述程式會輸出:

   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.

Example #7 Comparing Two Elements

Two SimpleXMLElements are considered different even if they point to the same element since PHP 5.2.0.go Example #8 Using XPath

SimpleXML includes built-in  support. To find all  elements:

<?phpinclude &#39;example.php&#39;;
$movies = new SimpleXMLElement($xmlstr);
echo $movies->movie->{&#39;great-lines&#39;}->line;?>
對oo'化 /owio​​nable / m slashes .

以上程式會輸出:

PHP solves all my web problems
Example #9 Setting values

Data in SimpleXML doesn't have to be constant. The srrect allows for 更多程式會.輸出:

<?phpinclude &#39;example.php&#39;;
$movies = new SimpleXMLElement($xmlstr);
/* For each <character> node, we echo a separate <name>. */foreach ($movies->movie->characters->character as $character) {
echo $character->name, &#39; played by &#39;, $character->actor, PHP_EOL;}
?>

Example #10 Adding elements and attributes

Since PHP 5.1.3, SimpleXML has had the ability to easily add children and attrir.3, SimpleXML has had the ability to easily add children and attrib. DOM Interoperability

PHP has a mechanism to convert XML nodes between SimpleXML and DOM formats. This example shows how one might change a DOM element 輸出Simample #1 Loading broken XML string

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
以上例程會輸出:
<?phpinclude &#39;example.php&#39;;
$movies = new SimpleXMLElement($xmlstr);
/* Access the <rating> nodes of the first movie.* Output the rating scale, too. */
foreach ($movies->movie[0]->rating as $rating) {
switch((string) $rating[&#39;type&#39;]) { // Get attributes as element indicescase &#39;thumbs&#39;:echo $rating, &#39; thumbs up&#39;;
break;case &#39;stars&#39;:echo $rating, &#39; stars&#39;;break;}}
?>

三、相關函數

plexmedm_import_ob​​m_Motot​​. simplexml_load_file — Interprets an XML file into an object

simplexml_load_string — Interprets a string of XML into an object

四、相關類別及成員函數Attribute — Adds an attribute to the SimpleXML element

SimpleXMLElement::addChild — Adds a child element to the XML node

SimpleXMLElement::asXML  a well-formed XML string pML tributes — Identifies an element's attributes

SimpleXMLElement::children — Finds children of given node

SimpleXMLElement::construct —— an element

SimpleXMLElement ::getDocNamespaces — Returns namespaces declared in document


SimpleXMLElement::getName — Gets the name of the XML element

  • SimpleSimplec

  • SimpleXMLElement::registerXPathNamespace — 為下一個XPath 查詢建立前綴/ns 上下文

  • SimpleXMLElement::saveXML — 別名SimpleXMLElement

  • SimpleXMLElement ::xpath —對XML 資料執行XPath 查詢

  • SimpleXMLIterator 類別

SimpleXMLIterator::current

  • SimpleXMLIterator ::hasChildren — 檢查目前元素是否有子元素。

  • SimpleXMLIterator::key — 返回目前鍵

  • SimpleXMLIterator::next — 移動到下一個元素

  • Simple鞋: :valid — 檢查目前元素是否有效

  • 以上是PHP擴充之XML操作(一)-SimpleXML的內容,更多相關內容請關注PHP中文網(m.sbmmt.com)!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:讓Asp與XML交互下一篇:讓Asp與XML交互