WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP SimpleXML Parser


SimpleXML is a PHP augmentation that enables us to effectively control and get XML data.


In this instructional exercise you investigation of XML Parser

The SimpleXML Parser

SimpleXML is a tree-based parser.

SimpleXML gives a simple method for getting a component's name, properties and literary content in the event that you realize the XML archive's structure or layout.

SimpleXML transforms a XML record into an information structure you can emphasize through like a gathering of clusters and objects.

Compared to DOM or the Expat parser, SimpleXML takes a less lines of code to peruse content information from an element.


Installation

As of PHP 5, the SimpleXML capacities are a piece of the PHP center. No establishment is required to utilize these functions.


PHP SimpleXML - Read From String

The PHP simplexml_load_string() work is utilized to peruse XML information from a string.

Assume we have a variable that contains XML information, as this:

$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't overlook me this weekend!</body>
</note>";

The precedent beneath tells the best way to utilize the simplexml_load_string() capacity to peruse XML information from a string:

Example

<?php
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't overlook me this weekend!</body>
</note>";

$xml=simplexml_load_string($myXMLData) or die("Error: Cannot make object");
print_r($xml);
?>

The yield of the code above will be:

SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don't overlook me this end of the week! )

Error Handling Tip: Use the libxml usefulness to recover all XML blunders when stacking the archive and after that repeat over the mistakes. The following precedent endeavors to stack a broken XML string:

Example

<?php
libxml_use_internal_errors(true);
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<document>
<user>John Doe</wronguser>
<email>john@example.com</wrongemail>
</document>";

$xml = simplexml_load_string($myXMLData);
if ($xml === false) {
    reverberation "Failed stacking XML: ";
    foreach(libxml_get_errors() as $error) {
        reverberation "<br>", $error->message;
    }
} else {
    print_r($xml);
}
?>

The yield of the code above will be:

Fizzled stacking XML:
Opening and completion label crisscross: client line 3 and wronguser
Opening and closure label crisscross: email line 4 and wrongemail

PHP SimpleXML - Read From File

The PHP simplexml_load_file() work is utilized to peruse XML information from a file.

Assume we have a XML record called "note.xml", that resembles this:

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't overlook me this weekend!</body>
</note>

The model beneath tells the best way to utilize the simplexml_load_file() capacity to peruse XML information from a file:

Example

<?php
$xml=simplexml_load_file("note.xml") or die("Error: Cannot make object");
print_r($xml);
?>

The yield of the code above will be:

SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don't overlook me this end of the week! )

Tip: The following part tells the best way to get/recover hub esteems from a XML document with SimpleXML!