PHP: simplexml_load_string - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

SNMP simplexml_load_file

Last updated: Sat, 27 Jan 2007

view this page in

# simplexml_load_string

(PHP 5)

simplexml_load_string -- Interprets a string of XML into an object

## Описание

object simplexml_load_string ( string data [, string class_name [, int options [, string ns [, bool is_prefix]]]] )

Takes a well-formed XML string and returns it as an object.

## Список параметров

`data`

  A well-formed XML string

`class_name`

  You may use this optional parameter so that simplexml_load_string() will return an object of the specified class. That class should extend the SimpleXMLElement class.

`options`

  Since PHP 5.1.0 and Libxml 2.6.0, you may also use the `options` parameter to specify additional Libxml parameters.

`ns`

`is_prefix`

## Возвращаемые значения

Returns an object of class SimpleXMLElement with properties containing the data held within the xml document. On errors, it will return `FALSE`.

## Примеры

Пример 1. Interpret an XML string

`<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
<title>Forty What?</title>
<from>Joe</from>
<to>Jane</to>
<body>
I know that's the answer -- but what's the question?
</body>
</document>
XML;
$xml = simplexml_load_string($string);
var_dump($xml);
?>`

Результат выполнения данного примера:

    SimpleXMLElement Object
    (
      [title] => Forty What?
      [from] => Joe
      [to] => Jane
      [body] =>
       I know that's the answer -- but what's the question?
    )

At this point, you can go about using `$xml->body` and such.

## Смотрите также

simplexml_load_file
SimpleXMLElement->__construct()

add a note User Contributed Notes
simplexml_load_string
m dot ament at mailcity dot com
06-Mar-2007 06:51
`Warning:
The parsing of XML-data will stop when reaching character 0.
Please avoid this character in your XML-data.`
mindpower
30-Jan-2007 03:03
`A simple extension that adds a method for retrieving a specific attribute:
<?php
class simple_xml_extended extends SimpleXMLElement
{
public function Attribute($name)
{
foreach($this->Attributes() as $key=>$val)
{
if($key == $name)
return (string)$val;
}
}
}
$xml = simplexml_load_string('
<xml>
<dog type="poodle" owner="Mrs Smith">Rover</dog>
</xml>', 'simple_xml_extended');
echo $xml->dog->Attribute('type');
?>
outputs 'poodle'
I prefer to use this technique rather than typecasting attributes.`
h
15-Nov-2006 10:44
`seems like simplexml has a line-length restriction - fails if a largeish XML doc with no linebreaks is passed as a string or file.
h`
paul at santasoft dot com
25-Jul-2006 08:22
`If you have PHP > 5.1 and LibXML > 2.6, use this function call to have simplexml convert CDATA into plain text.
simplexml_load_string($xmlstring, 'SimpleXMLElement', LIBXML_NOCDATA);
Too bad, so sad with PHP < 5.1.`
add a note
SNMP simplexml_load_file

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 10:36:09 2007 PST