Current Location: Home> Function Categories> simplexml_load_string

simplexml_load_string

Convert an XML string to an object.
Name:simplexml_load_string
Category:Uncategorized
Programming Language:php
One-line Description:Convert an XML string to an object.

Definition and usage

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

Example

Example 1

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

Example 2

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

Example 3

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

grammar

 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:

  • LIBXML_COMPACT - Activate node allocation optimization (may accelerate application)
  • LIBXML_DTDATTR - Set the default DTD attribute
  • LIBXML_DTDLOAD - Loading external subsets
  • LIBXML_DTDVALID - Verify using DTD
  • LIBXML_NOBLANKS - Remove blank nodes
  • LIBXML_NOCDATA - Merge CDATA into text nodes
  • LIBXML_NOEMPTYTAG - Extended empty tags (e.g. <br/> to <br></br>), available only in DOMDocument->save() and DOMDocument->saveXML() functions
  • LIBXML_NOENT - Replace entity
  • LIBXML_NOERROR - No error report displayed
  • LIBXML_NONET - Disable network access when loading a document
  • LIBXML_NOWARNING - Warning report not displayed
  • LIBXML_NOXMLDECL - Omit XML declaration when saving a document
  • LIBXML_NSCLEAN - Remove redundant namespace declarations
  • LIBXML_PARSEHUGE - Set the XML_PARSE_HUGE flag to relax the hard-coded limits of the parser. This affects the maximum depth of the document and the limits of the text node size
  • LIBXML_XINCLUDE - Implement XInclude replacement
  • LIBXML_ERR_ERROR - Get recoverable errors
  • LIBXML_ERR_FATAL - Get a fatal error
  • LIBXML_ERR_NONE - Get No Errors
  • LIBXML_ERR_WARNING - Get a simple warning
  • LIBXML_VERSION - Get the libxml version (for example 20605 or 20617)
  • LIBXML_DOTTED_VERSION - Get the dotted version of libxml (for example, 2.6.5 or 2.6.17)
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.

Similar Functions
Popular Articles