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

黄舟
發布: 2018-05-17 09:38:18
原創
5112 人瀏覽過


一、簡介及安裝

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: Behind the Parser   Ms. Coder Onlivia Actora   Mr. Coder El ActÓr    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.   PHP solves all my web problems  7 5   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.

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.goExample #8 Using XPath

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

movie->{'great-lines'}->line;?>
登入後複製
對oo'化 /owionable / 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 更多程式會.輸出:

 node, we echo a separate . */foreach ($movies->movie->characters->character as $character) { echo $character->name, ' played by ', $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
登入後複製
以上例程會輸出:
 nodes of the first movie.* Output the rating scale, too. */ foreach ($movies->movie[0]->rating as $rating) { switch((string) $rating['type']) { // Get attributes as element indicescase 'thumbs':echo $rating, ' thumbs up'; break;case 'stars':echo $rating, ' stars';break;}} ?>
登入後複製

三、相關函數

plexmedm_import_obm_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)!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!