我的 PHP
系统上有一个压缩的 XML
文件。
如何解压缩它以将其加载到 XMLDOM
中?
每个 zip
存档中只有一个 XML
文件。
提前致谢。
最佳答案
您可以使用 php 类 ZipArchive和 DOMDocument即:
<?php
//set the correct xml headers to output to browser
header("Content-type: text/xml");
$zipFile = "file.zip";
$zip = new ZipArchive;
if ($zip->open($zipFile))
{
//get the xml filename inside the zip
$xmlFile = $zip->getNameIndex(0); //you only have 1 xml file iside of the zip
$zip->close();
//read the xml file inside the zip without extracting it to disk (memory)
$xml = file_get_contents("zip://$zipFile#$xmlFile");
//create a new document
$dom = new DOMDocument('1.0', "UTF-8");
//load teh xml file
$dom->loadXML($xml);
//Here you can manipulate the XML dom , add, remove nodes, etc.
//save and echo the XML
echo $dom->saveXML();
} else {
echo 'zip open failed failed';
}
注释:
- How do I ask a good question?
- 我已经使用 this xml file 测试了代码它按预期工作。
关于php - 如何使用 PHP 解压 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37094630/