Home  >  Article  >  Backend Development  >  Introduction to SimpleXML functions in PHP_PHP tutorial

Introduction to SimpleXML functions in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:03:00759browse

This article briefly introduces the usage of the SimpleXML function in PHP. This function allows you to convert XML into objects. Friends who need to know more can refer to it. ​

SimpleXML function allows you to convert XML to objects.

This object can be processed just like any other object via ordinary property selectors or array iterators.

Some of these functions require the latest PHP version.
Install
SimpleXML functions are an integral part of PHP core. No installation is required to use these functions.

The code is as follows Copy code
//Return all content in object form
 代码如下 复制代码
 //以对象形式返回所有内容
 $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.'
';
 }
 //simplexml结合xpath无所不能
 $words = $lib->xpath("//word");
 //获取属性的方法
 echo $words[0]['add'].'
';
 echo $items[0]['pp'];
?>
$lib = simplexml_load_file("cet4.xml");

//Return the obtained item node in the form of an array

          $items=$lib->item;

          $wordsLength = sqlserver/42852.htm target=_blank >count($items);

//Get the node values ​​under all nodes

for($i=0;$i<$wordsLength;$i++){
$word=$items[$i];
echo $word->word.'-----'.$word->trans.'-----'.$word->phonetic.'
';
}
//simplexml combined with xpath is omnipotent
$words = $lib->xpath("//word");
//Method to get attributes
echo $words[0]['add'].'
';
echo $items[0]['pp'];
?>


Related functions

PHP: Indicates the earliest PHP version that supports this function.
Function Description PHP __construct() creates a new SimpleXMLElement object. 5

addAttribute() adds an attribute to a SimpleXML element. 5

addChild() adds a child element to the SimpleXML element. 5 attributes() Gets the attributes of a SimpleXML element. 5 children() gets the children of the specified node. 5 getDocNamespaces() Gets the namespace of an XML document. 5 getName() Gets the name of a SimpleXML element. 5 getNamespaces() Gets namespaces from XML data. 5 registerXPathNamespace() creates a namespace context for the next XPath query. 5 simplexml_import_dom() Gets a SimpleXMLElement object from a DOM node. 5 simplexml_load_file() Gets a SimpleXMLElement object from an XML document. 5 simplexml_load_string() Gets a SimpleXMLElement object from an XML string. 5 xpath() runs an XPath query on XML data. 5
http://www.bkjia.com/PHPjc/445310.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445310.htmlTechArticleThis article briefly introduces the usage of the SimpleXML function in php. This function allows you to convert XML into objects. , friends who need to know can refer to it. SimpleXML functions allow you to put...
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