Home >Backend Development >PHP Tutorial >Detailed explanation of php SimpleXML() function usage

Detailed explanation of php SimpleXML() function usage

怪我咯
怪我咯Original
2017-07-14 14:12:571613browse

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:

##addAttribute()Add an attribute to the SimpleXML element5addChild()Add a child element to the SimpleXML element5##asXML()Stringattributes()children()getDocNamespaces() namespace of the XML documentgetName()##getNamespaces()Get the namespace from XML data5registerXPathNamespace()Create for the next XPath Namespace contextsimplexml_import_dom()Get the SimpleXMLElement object from the DOM node5simplexml_load_file()Get the SimpleXMLElement object from the XML document5simplexml_load_string() Get a SimpleXMLElement object from an XML string5xpath()Run an XPath query on XML data5Example
Function Description PHP Version
construct() Create a new SimpleXMLElement object 5
Get XML from SimpleXML element 5
Get the attributes of the SimpleXML element 5
Get the specified node The child node 5
Get the 5
Get the name of the SimpleXML element 5
query5

<?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.&#39;-----&#39;.$word->trans.&#39;-----&#39;.$word->phonetic.&#39;<br/>&#39;; 
 }
 //simplexml结合xpath无所不能 
 $words = $lib->xpath("//word"); 
 //获取属性的方法 
 echo $words[0][&#39;add&#39;].&#39;<br/>&#39;; 
 echo $items[0][&#39;pp&#39;]; 
?>

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn