Analysis of simplexml function usage in PHP _php

Source: Internet
Author: User
Keywords Php simplexml function usage
Tags xpath
This example describes the use of the SimpleXML function in PHP, which allows you to convert XML to an object and share it with everyone for your reference. The specific analysis is as follows:

The SimpleXML function allows you to convert XML to an object, which can be handled by an ordinary property selector or an array iterator, just like any other object. Some of these functions require the latest version of PHP.

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

The code is as follows:

<?php
Return all content as an object
$lib = simplexml_load_file ("Cet4.xml");
Returns the retrieved item node as an array
$items = $lib->item;
$wordsLength = sqlserver/42852.htm Target=_blank >count ($items);
Gets the value of the node 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");
Methods to get Properties
echo $words [0][' Add ']. '
';
echo $items [0][' PP '];
?>


The related functions are described as follows:

Function Describe PHP version
__construct () To 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 () Getting an XML string from an SimpleXML element 5
Attributes () Gets the properties of the SimpleXML element 5
Children () Gets the child nodes of the specified node 5
Getdocnamespaces () Get the namespace of an XML document 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 () Get the SimpleXMLElement object from the DOM node 5
Simplexml_load_file () Getting SimpleXMLElement objects from an XML document 5
Simplexml_load_string () Get the SimpleXMLElement object from an XML string 5
XPath () Run an XPath query on XML data 5

I hope this article is helpful to everyone's PHP programming.

  • 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.