PHP simplexml_load_string () 함수
예
변환 문자열은 잘 구성된 XML SimpleXMLElement 개체이며, 다음 개체의 핵심 요소를 출력한다 :
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
print_r($xml);
?>
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
print_r($xml);
?>
»인스턴스를 실행
정의 및 사용
() 함수를 simplexml_load_string 것은 SimpleXMLElement 개체에 잘 구성된 XML 문자열의 형태를 변환합니다.
문법
simplexml_load_string( data,classname,options,ns,is_prefix );
参数 | 描述 |
---|---|
data | 必需。规定形式良好的 XML 字符串。 |
classname | 可选。规定新对象的 class。 |
options | 可选。规定附加的 Libxml 参数。通过指定选项为 1 或 0(TRUE 或 FALSE,例如 LIBXML_NOBLANKS(1))进行设置。 可能的值:
|
ns | 可选。规定命名空间前缀或 URI。 |
is_prefix | 可选。规定一个布尔值。如果 ns 是前缀则为 TRUE,如果 ns 是 URI 则为 FALSE。默认是 FALSE。 |
기술 세부 사항
반환 값 : | 성공적인 SimpleXMLElement 개체 경우 경우는 FALSE 반환되지 않습니다. |
---|---|
PHP 버전 : | 5+ |
더 많은 예제
예 1
데이터의 각 요소에 대한 출력 XML 문자열 :
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
»인스턴스를 실행
예 2
엘리먼트 이름과 데이터의 각 자식 노드의 출력 XML 스트링 :
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
»인스턴스를 실행
PHP SimpleXML을 참조 설명서