Home  >  Article  >  Backend Development  >  PHP SimpleXML_PHP Tutorial

PHP SimpleXML_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:32:18888browse

SimpleXML can read text data from elements in just a few lines of code compared to DOM or Expat parsers.

SimpleXML can convert XML documents into objects , such as:

Element - a single property converted to a SimpleXMLElement object. When there are multiple elements at the same level, they are placed in an array.

Properties - accessed using an associative array, where the subscripts correspond to the property names.

Element Data - Text data from the element is converted to a string. If an element has multiple text nodes, they are arranged in the order in which they are found.

SimpleXML is very fast to use when performing basic tasks like:

Read XML file

Extract data from XML string

Edit text nodes or attributes

However, when dealing with advanced XML, such as namespaces, it is better to use the Expat parser or the XML DOM.


SimpleXML functions are part of the core of PHP. No installation is required to use these functions.


XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>


PHP:
<?php
$xml = simplexml_load_file("test.xml");

echo $xml->getName() . "<br />";

foreach($xml->children() as $child)
  {
  echo $child->getName() . ": " . $child . "<br />";
  }
?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/755781.htmlTechArticleCompared with DOM or Expat parser, SimpleXML can read text data from elements with just a few lines of code . SimpleXML can convert XML documents into objects, such as: Element - converted...
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