php中怎么利用expat方式解析xml文件-創(chuàng)新互聯(lián)

我們提供的服務(wù)有:網(wǎng)站建設(shè)、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、馬關(guān)ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的馬關(guān)網(wǎng)站制作公司

這篇文章將為大家詳細(xì)講解有關(guān)php中怎么利用expat方式解析xml文件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

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
// 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);
?>

運行結(jié)果:

-- Note --
To: George
From: John
Heading: Reminder
Message: Don't forget the meeting!
-- Note --
To: George2
From: John2
Heading: Reminder2
Message: Don't forget the meeting!2

關(guān)于php中怎么利用expat方式解析xml文件就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

當(dāng)前文章:php中怎么利用expat方式解析xml文件-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://bm7419.com/article12/didjgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、搜索引擎優(yōu)化、小程序開發(fā)、品牌網(wǎng)站制作、網(wǎng)站設(shè)計公司、靜態(tài)網(wǎng)站

廣告

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

h5響應(yīng)式網(wǎng)站建設(shè)