PHP xml_set_element_handler()ฟังก์ชั่น
❮ การอ้างอิงตัวแยกวิเคราะห์ PHP XML
ตัวอย่าง
ระบุฟังก์ชันที่จะเรียกเมื่อเริ่มต้นและสิ้นสุดองค์ประกอบในเอกสาร XML ( note.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;
}
// Specify functions to be called at the start and end of an
element in the XML document
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);
?>
ความหมายและการใช้งาน
ฟังก์ชัน xml_set_element_handler() ระบุฟังก์ชันที่จะเรียกใช้เมื่อเริ่มต้นและสิ้นสุดองค์ประกอบในเอกสาร XML
หมายเหตุ: พารามิเตอร์ เริ่มต้นและสิ้นสุดยังสามารถเป็นอาร์เรย์ที่มีการอ้างอิงอ็อบเจ็กต์และชื่อเมธอด
ไวยากรณ์
xml_set_element_handler(parser, start, end)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
parser | Required. Specifies the XML parser to use |
start | Required. Specifies a function to be called at the start of an element. The function must have three parameters:
|
end | Required. Specifies a function to be called at the end of an element.
The function must have two parameters:
|
รายละเอียดทางเทคนิค
คืนมูลค่า: | จริงกับความสำเร็จ FALSE เมื่อล้มเหลว |
---|---|
รุ่น PHP: | 4.0+ |
❮ การอ้างอิงตัวแยกวิเคราะห์ PHP XML