PHP simplexml_load_file() 함수
예시
XML 파일을 개체로 변환한 다음 개체의 키와 요소를 출력합니다.
<?php
$xml=simplexml_load_file("note.xml");
print_r($xml);
?>
정의 및 사용
simplexml_load_file() 함수는 XML 문서를 객체로 변환합니다.
통사론
simplexml_load_file(file, class, options, ns, is_prefix)
매개변수 값
Parameter | Description |
---|---|
file | Required. Specifies the path to the XML file |
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)) Possible values:
|
ns | Optional. Specifies a namespace prefix or URI |
is_prefix | Optional. Specifies a Boolean value. TRUE if ns is a prefix. FALSE if ns is a URI. Default is FALSE |
기술적 세부 사항
반환 값: | 성공 시 SimpleXMLElement 개체입니다. 실패시 FALSE |
---|---|
PHP 버전: | 5+ |
더 많은 예
다음 XML 파일 " note.xml " 이 있다고 가정합니다 .
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
예시
XML 파일의 각 요소에서 데이터를 출력합니다.
<?php
$xml=simplexml_load_file("note.xml");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
예시
XML 파일의 각 자식 노드에 대한 요소의 이름과 데이터를 출력합니다.
<?php
$xml=simplexml_load_file("note.xml");
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
❮ PHP SimpleXML 참조