Hàm xml_set_processing_instruction_handler () trong PHP
❮ Tham chiếu trình phân tích cú pháp XML PHP
Thí dụ
Tạo trình phân tích cú pháp XML, thiết lập trình xử lý dữ liệu ký tự, thiết lập trình xử lý PI và phân tích cú pháp một tài liệu XML ( note_pi.xml ):
<?php
$parser=xml_parser_create();
function char($parser,$data) {
echo $data;
}
function pi_handler($parser, $target, $data) {
echo "Target: $target<br />";
echo "Data: $data<br />";
}
xml_set_character_data_handler($parser,"char");
// Set up PI
handler
xml_set_processing_instruction_handler($parser, "pi_handler");
$fp=fopen("note_pi.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);
?>
Định nghĩa và Cách sử dụng
Hàm xml_set_processing_instruction_handler () chỉ định một hàm được gọi khi trình phân tích cú pháp tìm thấy một lệnh xử lý (PI) trong tài liệu XML.
Một PI được bao gồm trong <? và?> và chứa aa target theo sau là dữ liệu.
Ví dụ: Trong trường hợp này, PI liên kết biểu định kiểu với tài liệu XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="default.xsl" type="text/xml"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Lưu ý: Tham số trình xử lý cũng có thể là một mảng chứa tham chiếu đối tượng và tên phương thức.
Cú pháp
xml_set_processing_instruction_handler(parser, handler)
Giá trị tham số
Parameter | Description |
---|---|
parser | Required. Specifies the XML parser to use |
handler | Required. Specifies a function to be used as an event handler. The function must
accept three parameters:
|
Chi tiết kỹ thuật
Giá trị trả lại: | ĐÚNG về thành công. FALSE khi thất bại |
---|---|
Phiên bản PHP: | 4.0+ |
❮ Tham chiếu trình phân tích cú pháp XML PHP