PHP simplexml_load_file() 함수
정의와 사용법
simplexml_load_file() 함수는 XML 문서를 객체에 로드합니다.
실패하면 false를 반환합니다.
문법
simplexml_load_file(file,class,options,ns,is_prefix)
파라미터 | 설명 |
---|---|
file | 필수. 사용할 XML 문서를 지정합니다. |
class | 선택 사항. 새 객체의 클래스를 지정합니다. |
options | 선택 사항. 추가 Libxml 파라미터를 지정합니다. |
ns | 선택 사항 |
is_prefix | 선택 사항 |
반환 값
SimpleXMLElement 클래스의 객체를 반환하며, 이 객체의 속성은 XML 문서에 포함된 데이터를 포함합니다. 실패하면 false를 반환합니다.
예제
XML 파일:
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
PHP 코드:
<?php if (file_exists('test.xml')) { $xml = simplexml_load_file('test.xml'); var_dump($xml); } else { exit('Error.'); } ?>
출력:
object(SimpleXMLElement)#1 (4) { ["to"]=> string(4) "George" ["from"]=> string(4) "John" ["heading"]=> string(8) "Reminder" ["body"]=> string(29) "Don't forget the meeting!" }