PHP XML 到 JSON

WBOY
發布: 2024-08-29 13:00:46
原創
470 人瀏覽過

為了在PHP 中將XML 轉換為JSON,我們有一個名為json_encode 的函數,這是PHP 中的內建函數,將XML 轉換為JSON 的過程首先使用以下函數獲取XML 檔案的內容函數_file_get_contents( )_ 將XML 檔案的URL 作為參數傳遞給該函數,然後刪除回車符、製表符和換行符,然後將雙引號替換為單引號,然後將尾隨和前導空格替換為進行修剪以確保透過簡單的XML 函數正確解析XML,然後使用json_encode 函數進行最終轉換。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

在 PHP 中宣告 Zlib 模組的語法:

json_encode(URL_to_the_XML_file)
登入後複製

其中 URL_to_the_XML_file 是要轉換為 JSON 的 XML 檔案的 URL。

在 PHP 中將 XML 轉換為 JSON 的步驟

  • 使用函數 file_get_contents() 來取得 XML 檔案內容,該函數將 XML 檔案的 URL 作為參數傳遞給該函數。
  • 刪除製表符、回車符和換行符。
  • 單引號替換雙引號。
  • 尾部和前導空格被修剪,以確保簡單的 XML 函數正確解析 XML。
  • 呼叫 simplexml_load_string() 函數來載入 XML 檔案的內容。
  • XML 到 JSON 的最終轉換是透過呼叫 json_encode() 函數完成的。

PHP XML 到 JSON 的範例

下面給出了 PHP XML 到 JSON 的範例:

範例#1

PHP 程式來說明 XML 到 JSON 的轉換,其中我們將 XML 檔案的 URL 作為參數提供給 json_encode 函數,以將 XML 檔案的內容轉換為 JSON。

代碼:

<html>
<body>
<?php
class XmlToJson {
public function Parse ("C://Users/admin/Desktop/check.xml") {
# Getting the contents of the XML file by making use of the function file_get_contents() to which the URL of the XML file is passed as a paramter
$filepath= file_get_contents("C://Users/admin/Desktop/check.xml");
# Removing the tabs, returns and the newlines
$filechange = str_replace(array("\n", "\r", "\t"), '', $filepath);
# The trailing and leading spaces are trimmed to make sure the XML is parsed properly by a simple XML function.
$filetrim = trim(str_replace('"', "'", $filechange));
# The simplexml_load_string() function is called to load the contents of the XML file.
$resultxml = simplexml_load_string($filetrim);
# The final conversion of XML to JSON is done by calling the json_encode() function.
$resultjson = json_encode($resultxml);
return $resultjson;
}
}
?>
</body>
</html>
登入後複製

輸出:

PHP XML 到 JSON

在上面的程式中,我們透過使用函數 file_get_contents() 來取得 XML 檔案內容,該函數將 XML 檔案的 URL 作為參數傳遞給該函數。然後製表符、回車符和換行符將被刪除。然後雙引號被單引號替換。然後,尾部和前導空格被修剪,以確保簡單的 XML 函數能夠正確解析 XML。然後呼叫 simplexml_load_string() 函數來載入 XML 檔案的內容。然後透過呼叫 json_encode() 函數完成 XML 到 JSON 的最終轉換。

範例#2

PHP 程式來說明 XML 到 JSON 的轉換,其中我們將 XML 檔案的 URL 作為參數提供給 json_encode 函數,以將 XML 檔案的內容轉換為 JSON。

代碼:

<html>
<body>
<?php
class XmlToJson {
public function Parse ("C://Users/admin/Desktop/test.xml") {
# Getting the contents of the XML file by making use of the function file_get_contents() to which the URL of the XML file is passed as a paramter
$filepath= file_get_contents("C://Users/admin/Desktop/test.xml");
# Removing the tabs, returns and the newlines
$filechange = str_replace(array("\n", "\r", "\t"), '', $filepath);
# The trailing and leading spaces are trimmed to make sure the XML is parsed properly by a simple XML function.
$filetrim = trim(str_replace('"', "'", $filechange));
# The simplexml_load_string() function is called to load the contents of the XML file.
$resultxml = simplexml_load_string($filetrim);
# The final conversion of XML to JSON is done by calling the json_encode() function.
$resultjson = json_encode($resultxml);
return $resultjson;
}
}
?>
</body>
</html>
登入後複製

輸出:

PHP XML 到 JSON

在上面的程式中,我們透過使用函數 file_get_contents() 來取得 XML 檔案內容,該函數將 XML 檔案的 URL 作為參數傳遞給該函數。然後製表符、回車符和換行符將被刪除。然後雙引號被單引號替換。然後,修剪尾隨和前導空格,以確保簡單的 XML 函數能夠正確解析 XML。然後呼叫 simplexml_load_string() 函數來載入 XML 檔案的內容。然後透過呼叫 json_encode() 函數完成 XML 到 JSON 的最終轉換。

範例#3

PHP 程式來說明 XML 到 JSON 的轉換,其中我們將 XML 檔案的 URL 作為參數提供給 json_encode 函數,以將 XML 檔案的內容轉換為 JSON。

代碼:

<html>
<body>
<?php
class XmlToJson {
public function Parse ("C://Users/admin/Desktop/file.xml") {
# Getting the contents of the XML file by making use of the function file_get_contents() to which the URL of the XML file is passed as a paramter
$filepath= file_get_contents("C://Users/admin/Desktop/file.xml");
# Removing the tabs, returns and the newlines
$filechange = str_replace(array("\n", "\r", "\t"), '', $filepath);
# The trailing and leading spaces are trimmed to make sure the XML is parsed properly by a simple XML function.
$filetrim = trim(str_replace('"', "'", $filechange));
# The simplexml_load_string() function is called to load the contents of the XML file.
$resultxml = simplexml_load_string($filetrim);
# The final conversion of XML to JSON is done by calling the json_encode() function.
$resultjson = json_encode($resultxml);
return $resultjson;
}
}
?>
</body>
</html>
登入後複製

輸出:

PHP XML 到 JSON

在上面的程式中,我們透過使用函數 file_get_contents() 來取得 XML 檔案內容,該函數將 XML 檔案的 URL 作為參數傳遞給該函數。然後製表符、回車符和換行符將被刪除。然後雙引號被單引號替換。然後,修剪尾隨和前導空格,以確保簡單的 XML 函數能夠正確解析 XML。然後呼叫 simplexml_load_string() 函數來載入 XML 檔案的內容。然後透過呼叫 json_encode() 函數完成 XML 到 JSON 的最終轉換。

以上是PHP XML 到 JSON的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!