php用什么方法解析xml文件?-創(chuàng)新互聯(lián)

本文實例講述了php 使用expat方式解析xml文件操作。分享給大家供大家參考,具體如下:

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務,包含不限于網(wǎng)站設計、成都做網(wǎng)站、郾城網(wǎng)絡推廣、重慶小程序開發(fā)、郾城網(wǎng)絡營銷、郾城企業(yè)策劃、郾城品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們大的嘉獎;創(chuàng)新互聯(lián)公司為所有大學生創(chuàng)業(yè)者提供郾城建站搭建服務,24小時服務熱線:13518219792,官方網(wǎng)址:muchs.cn

test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<notes>
 <note>
 <to>George</to>
 <from>John</from>
 <heading>Reminder</heading>
 <body>Don't forget the meeting!</body>
 </note>
 <note>
 <to>George2</to>
 <from>John2</from>
 <heading>Reminder2</heading>
 <body>Don't forget the meeting!2</body>
 </note>
 <instances>
 <instance st="192.168.234.121" />
 <instance st="192.168.234.28" />
 </instances>
</notes>

PHP文件:(免費學習視頻教程分享:php視頻教程)

<?php
// Initialize the XML parser
$parser = xml_parser_create();
// Function to use at the start of an element
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 to use at the end of an element
function stop($parser, $element_name)
{
  echo "<br />";
}
// 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, 10)) {
// 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)));
// }
// fclose($fp);
$data = file_get_contents("test.xml");
xml_parse($parser, $data) 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);
?>

以上就是php實現(xiàn)利用expat方式解析xml文件的詳細內(nèi)容,更多請關注創(chuàng)新互聯(lián)其它相關文章!

本文標題:php用什么方法解析xml文件?-創(chuàng)新互聯(lián)
當前URL:http://muchs.cn/article18/degcgp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、品牌網(wǎng)站設計響應式網(wǎng)站、網(wǎng)站改版網(wǎng)站導航、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設