simplexml function Usage Analysis _php techniques in PHP

Source: Internet
Author: User
Tags xpath

This example describes the use of the SimpleXML function in PHP, which allows you to transform XML into objects and share them for your reference. The specific analysis is as follows:

The SimpleXML function allows you to convert XML to an object, and you can work with the object as you would any other object by using a normal property selector or an array iterator. Some of these functions require the latest PHP version.

Installation: The SimpleXML function is part of the PHP core and can be used without installation, and the code is as follows:

Copy Code code as follows:
<?php
Return all content as an object
$lib = simplexml_load_file ("Cet4.xml");
Returns the acquired item node as an array
$items = $lib->item;
$wordsLength = sqlserver/42852.htm Target=_blank >count ($items);
Gets the next node value of all nodes
for ($i =0; $i < $wordsLength; $i + +) {
$word = $items [$i];
echo $word->word. '-----' $word->trans. '-----'. $word->phonetic. ' <br/> ';
}
SimpleXML combining XPath is omnipotent
$words = $lib->xpath ("//word");
Methods to get Properties
echo $words [0][' Add ']. ' <br/> ';
echo $items [0][' PP '];
?>

Related functions are described below:

Function Describe PHP version
__construct () Create a new SimpleXMLElement object 5
AddAttribute () Add a property to the SimpleXML element 5
AddChild () Add a child element to a SimpleXML element 5
Asxml () To get an XML string from the SimpleXML element 5
Attributes () Gets the properties of the SimpleXML element 5
Children () Gets the child nodes of the specified node 5
Getdocnamespaces () Getting namespaces for XML documents 5
GetName () Gets the name of the SimpleXML element 5
GetNamespaces () Getting namespaces from XML data 5
Registerxpathnamespace () Create a namespace context for the next XPath query 5
Simplexml_import_dom () Getting SimpleXMLElement objects from a DOM node 5
Simplexml_load_file () Getting SimpleXMLElement objects from an XML document 5
Simplexml_load_string () To get a SimpleXMLElement object from an XML string 5
XPath () Run an XPath query on XML data 5

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.