simplexml_load_string
Convert an XML string to an object.
The simplexml_load_string()
function converts a well-formed XML string into an object.
Convert an XML string to an object, and output the object's keys and elements:
<?php $note = <<< XML <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Do not forget the meeting!</body> </note> XML ; $xml = simplexml_load_string ( $note ) ; print_r ( $xml ) ; ?>
Run the instance
Output data for each element in the XML string:
<?php $note = <<< XML <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Do not forget the meeting!</body> </note> XML ; $xml = simplexml_load_string ( $note ) ; echo $xml -> to . "<br>" ; echo $xml -> from . "<br>" ; echo $xml -> heading . "<br>" ; echo $xml -> body ; ?>
Run the instance
Output the element name and data of each child node in the XML string:
<?php $note = <<< XML <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Do not forget the meeting!</body> </note> XML ; $xml = simplexml_load_string ( $note ) ; echo $xml -> getName ( ) . "<br>" ; foreach ( $xml -> children ( ) as $child ) { echo $child -> getName ( ) . ": " . $child . "<br>" ; } ?>
Run the instance
simplexml_load_string ( data , class , options , ns , is_prefix )
parameter | describe |
---|---|
data | Required. Specifies a well-formed XML string. |
class | Optional. Specifies the class name of the new object. |
options |
Optional. Specify additional Libxml parameters. Set by specifying options and 1 or 0 (TRUE or FALSE, such as LIBXML_NOBLANKS(1)). Possible values include:
|
ns | Optional. Specifies a namespace prefix or URI. |
is_prefix |
Optional. Specifies a boolean value. If ns is a prefix, it is TRUE; if ns is a URI, it is FALSE. The default is FALSE. |