PHP XML Expat 解析器,該怎麼處理

來源:互聯網
上載者:User
PHP XML Expat 解析器
有兩種基本的 XML 解析器類型:

基於樹的解析器:這種解析器把 XML 文檔轉換為樹型結構。它分析整篇文檔,並提供了 API 來訪問樹種的元素,例如文件物件模型 (DOM)。

基於事件的解析器:將 XML 文檔視為一系列的事件。當某個具體的事件發生時,解析器會調用函數來處理。

Expat 解析器是基於事件的解析器。


XML Expat 解析器是 PHP 核心的組成部分。無需安裝就可以使用這些函數。


XML 檔案:


George
John
Reminder
Don't forget the meeting!


初始化 XML 解析器:
";
break;
case "TO":
echo "To: ";
break;
case "FROM":
echo "From: ";
break;
case "HEADING":
echo "Heading: ";
break;
case "BODY":
echo "Message: ";
}
}

//Function to use at the end of an element
function stop($parser,$element_name)
{
echo "
";
}

//Function to use when finding character data
function char($parser,$data)
{
echo $data;
}

//Specify element handler
xml_set_element_handler($parser,"start","stop");

//Specify data handler
xml_set_character_data_handler($parser,"char");

//Open XML file
$fp=fopen("test.xml","r");

//Read data
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)));
}

//Free the XML parser
xml_parser_free($parser);

?>
------解決方案--------------------
技術分析貼?
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.