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
XML; ?> 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
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;?>
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.
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
movie->{'great-lines'}->line;?>
PHP solves all my web problems
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 InteroperabilityPHP 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 documentSimpleSimplec
SimpleXMLElement::registerXPathNamespace — 為下一個XPath 查詢建立前綴/ns 上下文
SimpleXMLElement::saveXML — 別名SimpleXMLElement
SimpleXMLIterator 類別
SimpleXMLIterator::current