PHP xml_parse() 함수
예시
XML 파서를 만들고 XML 문서( note.xml )를 파싱합니다.
<?php
// Create an XML parser
$parser=xml_parser_create();
function char($parser,$data) {
echo $data;
}
xml_set_character_data_handler($parser,"char");
$fp=fopen("note.xml","r");
while ($data=fread($fp,4096)) {
// Parse XML data
xml_parse($parser,$data,feof($fp)) or
die (sprintf("XML Error: %s
at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
fclose($fp);
?>
정의 및 사용
xml_parse() 함수는 XML 문서를 구문 분석합니다.
팁: XML 파서를 생성하려면 xml_parser_create() 함수를 사용하십시오.
통사론
xml_parse(parser, data, end)
매개변수 값
Parameter | Description |
---|---|
parser | Required. Specifies the XML parser to use |
data | Required. Specifies the data to parse |
end | Optional. If set to TRUE, the data in the data parameter is the last piece of data sent in this parse. Note: Entity errors are reported at the end of the parse - and will only show if the end parameter is TRUE |
기술적 세부 사항
반환 값: | 성공 시 TRUE입니다. 실패시 FALSE |
---|---|
PHP 버전: | 4.0+ |
더 많은 예
예시
동일한 XML 파일을 사용하지만 XML 데이터를 다른 방식으로 표시:
<?php
$parser=xml_parser_create();
function start($parser,$element_name,$element_attrs)
{
switch($element_name) {
case "NOTE":
echo "NOTE<br>";
break;
case "TO":
echo "To: ";
break;
case "FROM":
echo "From: ";
break;
case "HEADING":
echo "Heading: ";
break;
case "BODY":
echo "Message: ";
}
}
function stop($parser,$element_name) {
echo "<br>";
}
function char($parser,$data) {
echo $data;
}
xml_set_element_handler($parser,"start","stop");
xml_set_character_data_handler($parser,"char");
$fp=fopen("note.xml","r");
while ($data=fread($fp,4096)) {
xml_parse($parser,$data,feof($fp)) or
die (sprintf("XML Error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
fclose($fp);
?>
❮ PHP XML 파서 참조