Home >Backend Development >PHP Tutorial >Detailed explanation of php SimpleXML() function usage
SimpleXML The function allows you to convert XML to an object, which can be processed through ordinary property selectorsor array iterators, Just like any other object. Some of these functions require the latest PHP version.
INSTALLATION: The SimpleXML functions are part of the PHP core, no installation is required to use these Function
Related functions are introduced as follows:
Function | Description | PHP Version |
construct() | Create a new SimpleXMLElement object | 5 |
Add an attribute to the SimpleXML element | 5 | |
Add a child element to the SimpleXML element | 5 | |
Get XML from SimpleXML element | String5 | |
Get the attributes of the SimpleXML element | 5 | |
Get the specified node The child node | 5 | |
Get the | namespace of the XML document5 | |
Get the name of the SimpleXML element | 5 | ##getNamespaces() |
5 | registerXPathNamespace() | |
query | Namespace context5 | simplexml_import_dom() |
5 | simplexml_load_file() | |
5 | simplexml_load_string() | |
5 | xpath() | |
5 |
<?php //以对象形式返回所有内容 $lib = simplexml_load_file("cet4.xml"); //以数组形式返回获取的item节点 $items=$lib->item; $wordsLength = sqlserver/42852.htm target=_blank >count($items); //获取所有节点的下的节点值 for($i=0;$i<$wordsLength;$i++){ $word=$items[$i]; echo $word->word.'-----'.$word->trans.'-----'.$word->phonetic.'<br/>'; } //simplexml结合xpath无所不能 $words = $lib->xpath("//word"); //获取属性的方法 echo $words[0]['add'].'<br/>'; echo $items[0]['pp']; ?>
The above is the detailed content of Detailed explanation of php SimpleXML() function usage. For more information, please follow other related articles on the PHP Chinese website!