Explore how to use the SimpleXML function to load and parse XML documents

  • 2020-06-07 04:07:57
  • OfStack

A large number of SmipleXML functions can be used to load and parse a large number of XML documents.
--------------------------------------------------------------------------------
1.simpleXML_load_file() function to load the specified XML file to the object. If you encounter problems loading the file, return FLASE. Ex. :
book. xml file:

<?xml version="1.0" standalone="yes"?>
<library>
 <book>
  <title>Pride and Prejudice</title>
  <author gender="female">Jane Austen</author>
  <description>Jane Austen's most popular work.</description>
 </book>
 <book>
  <title>The Conformist</title>
  <author gender="male">Alberto Moravia</author>
  <description>Alberto Moravia's classic psyhcological novel.</description>
 </book>
 <book>
  <title>The Sun Also Rises</title>
  <author gender="male">Ernest Hemingway</author>
  <description>The masterpiece that launched Hemingway's career.</description>
 </book>
</library>

php file:

<?php
$xml=simplexml_load_file("book.xml");echo "<pre>";
var_dump($xml);
?>

Output results:

object(SimpleXMLElement)#1 (1) {
  ["book"]=>
  array(3) {
    [0]=>
    object(SimpleXMLElement)#2 (3) {
      ["title"]=>
      string(19) "Pride and Prejudice"
      ["author"]=>
      string(11) "Jane Austen"
      ["description"]=>
      string(32) "Jane Austen's most popular work."
    }
    [1]=>
    object(SimpleXMLElement)#3 (3) {
      ["title"]=>
      string(14) "The Conformist"
      ["author"]=>
      string(15) "Alberto Moravia"
      ["description"]=>
      string(46) "Alberto Moravia's classic psyhcological novel."
    }
    [2]=>
    object(SimpleXMLElement)#4 (3) {
      ["title"]=>
      string(18) "The Sun Also Rises"
      ["author"]=>
      string(16) "Ernest Hemingway"
      ["description"]=>
      string(49) "The masterpiece that launched Hemingway's career."
    }
  }
}


Related articles: