Php simplexml load string as xml

PHP simplexml_load_string() Function

Convert an XML string into an object, then output keys and elements of the object:

Definition and Usage

The simplexml_load_string() function converts a well-formed XML string into an object.

Syntax

Parameter Values

Parameter Description
data Required. Specifies a well-formed XML string
class Optional. Specifies the class of the new object
options Optional. Specifies additional Libxml parameters. Is set by specifying the option and 1 or 0 (TRUE or FALSE, e.g. LIBXML_NOBLANKS(1))
  • LIBXML_COMPACT — Activate nodes allocation optimization (may speed up application)
  • LIBXML_DTDATTR — Set default DTD attributes
  • LIBXML_DTDLOAD — Load external subset
  • LIBXML_DTDVALID — Validate with the DTD
  • LIBXML_NOBLANKS — Remove blank nodes
  • LIBXML_NOCDATA — Merge CDATA as text nodes
  • LIBXML_NOEMPTYTAG — Expand empty tags (e.g.
    to ), only available in the DOMDocument->save() and DOMDocument->saveXML() functions
  • LIBXML_NOENT — Substitute entities
  • LIBXML_NOERROR — Do not show error reports
  • LIBXML_NONET — Disable network access while loading documents
  • LIBXML_NOWARNING — Do not show warning reports
  • LIBXML_NOXMLDECL — Drop the XML declaration when saving a document
  • LIBXML_NSCLEAN — Remove redundant namespace declarations
  • LIBXML_PARSEHUGE — Sets XML_PARSE_HUGE flag, which relaxes any hardcoded limit from the parser. This affects limits like maximum depth of a document and limits of the size of text nodes
  • LIBXML_XINCLUDE — Implement XInclude substitution
  • LIBXML_ERR_ERROR — Get recoverable errors
  • LIBXML_ERR_FATAL — Get fatal errors
  • LIBXML_ERR_NONE — Get no errors
  • LIBXML_ERR_WARNING — Get simple warnings
  • LIBXML_VERSION — Get libxml version (e.g. 20605 or 20617)
  • LIBXML_DOTTED_VERSION — Get dotted libxml version (e.g. 2.6.5 or 2.6.17)

Technical Details

More Examples

Example

Output the data from each element in the XML string:

$xml=simplexml_load_string($note);
echo $xml->to . «
«;
echo $xml->from . «
«;
echo $xml->heading . «
«;
echo $xml->body;
?>

Example

Output the element’s name and data for each child node in the XML string:

foreach($xml->children() as $child)
echo $child->getName() . «: » . $child . «
«;
>
?>

Источник

PHP SimpleXML Parser

SimpleXML is a PHP extension that allows us to easily manipulate and get XML data.

The SimpleXML Parser

SimpleXML is a tree-based parser.

SimpleXML provides an easy way of getting an element’s name, attributes and textual content if you know the XML document’s structure or layout.

SimpleXML turns an XML document into a data structure you can iterate through like a collection of arrays and objects.

Compared to DOM or the Expat parser, SimpleXML takes a fewer lines of code to read text data from an element.

Installation

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

PHP SimpleXML — Read From String

The PHP simplexml_load_string() function is used to read XML data from a string.

Assume we have a variable that contains XML data, like this:

The example below shows how to use the simplexml_load_string() function to read XML data from a string:

Example

$xml=simplexml_load_string($myXMLData) or die(«Error: Cannot create object»);
print_r($xml);
?>

The output of the code above will be:

SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don’t forget me this weekend! )

Error Handling Tip: Use the libxml functionality to retrieve all XML errors when loading the document and then iterate over the errors. The following example tries to load a broken XML string:

Example

$xml = simplexml_load_string($myXMLData);
if ($xml === false) echo «Failed loading XML: «;
foreach(libxml_get_errors() as $error) echo «
«, $error->message;
>
> else print_r($xml);
>
?>

The output of the code above will be:

Failed loading XML:
Opening and ending tag mismatch: user line 3 and wronguser
Opening and ending tag mismatch: email line 4 and wrongemail

PHP SimpleXML — Read From File

The PHP simplexml_load_file() function is used to read XML data from a file.

Assume we have an XML file called «note.xml», that looks like this:

The example below shows how to use the simplexml_load_file() function to read XML data from a file:

Example

The output of the code above will be:

SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don’t forget me this weekend! )

Tip: The next chapter shows how to get/retrieve node values from an XML file with SimpleXML!

More PHP SimpleXML

For more information about the PHP SimpleXML functions, visit our PHP SimpleXML Reference.

Источник

PHP simplexml_load_string() Function

XML is a mark-up language to share the data across the web, XML is for both human read-able and machine read-able. The simple XML parser is used to parse Name, attributes and textual content.

The simplexml_load_string() accepts an (well-formed) XML string as a parameter converts it into an object of the SimpleXMLElement class and returns it.

Syntax

simplexml_load_string($data, [$class_name, $options, $ns, $is_prefix]);

Parameters

This is a string value representing a XML string which is to be interpreted as an object.

This is a string value to representing the name of the class (sub class of the SimpleXMLElement).

If you pass this value, the given XML string is returned as the object of the specified class.

This is an integer value used to specify the additional Libxml parameters.

This is a string value representing the namespace prefix or URI.

This is a boolean value representing whether the previous option is a prefix or an URI.

Return Values

This function returns an object of the SimpleXMLElement class in case of success and returns the boolean value FALSE in case of failure.

PHP Version

This function was first introduced in PHP Version 5 and works in all the later versions.

Example

Following example demonstrates the usage of the simplexml_load_string() function.

     Raju 25 2000 "; $xml = simplexml_load_string($data); print_r($xml); ?>   

This will produce following result −

SimpleXMLElement Object ( [Name] => Raju [Age] => 25 [Salary] => 2000 )

Example

Following is another example of this function, in here we are trying to interpret an XML sting which has multiple records −

      JavaFX 535 Krishna 11  CoffeeScript 235 Kasyap 2.5.1  "; $xml = simplexml_load_string($str); print("
"); foreach($xml->children() as $tut) < print($tut->Name ."
"); print($tut->Pages ."
"); print($tut->Author ."
"); print($tut->Version ."
"); print("
"); > ?>

This will produce the following output −

JavaFX 535 Krishna 11 CoffeeScript 235 Kasyap 2.5.1

Example

Following example demonstrates the usage of this method with options −

    JavaFX 535 Krishna 11 "; $xml = simplexml_load_string($str, "SimpleXMLElement", LIBXML_BIGLINES, FALSE); print("
"); print($xml->Name ."
"); print($xml->Pages ."
"); print($xml->Author ."
"); print($xml->Version); ?>

This will produce following result −

Example

 Gopal CEO Reminder Don't forget to send a file to me  XML; $xml = simplexml_load_string($note); echo $xml->to . "
"; echo $xml->from . "
"; echo $xml->heading . "
"; echo $xml->body; ?>

This will produce following result −

Gopal CEO Reminder Don't forget to send a file to me

Источник

Читайте также:  Javascript case this or this
Оцените статью