我一直在搜索信息,以了解如何在将PHP导出到XML时删除PHP代码留下的标记值之间的空格,我将详细说明,首先加载XML,然后使用xPath在文件上进行搜索,然后删除一些与某些品牌不匹配的元素,最后我将其重新导出为新XML,问题是此新XML充满了代码留下的空白。我尝试修整它,但似乎无法正常工作。
这是我的代码:
<?php
$sXML = simplexml_load_file('file.xml'); //First load the XML
$brands = $sXML->xPath('//brand'); //I do a search for the <brand> tag
function filter(string $input) { //Then I give it a list of variables
switch ($input) {
case 'BRAND 3':
case 'BRAND 4':
return false;
default:
return true;
}
}
array_walk($brands, function($brand) { //I remove all elements do not match my list
$content = (string) $brand;
if (filter($content)) {
$item = $brand->xPath('..')[0];
unset($item[0]);
}
});
$sXML->asXML('filtred.xml'); // And finally export a new xml
?>
这是原始XML:
<?xml version="1.0" encoding="utf-8"?>
<products>
<item>
<reference>00001</reference>
<other_string>PRODUCT 1</other_string>
<brand>BRAND 1</brand>
</item>
<item>
<reference>00002</reference>
<other_string>PRODUCT 2</other_string>
<brand>BRAND 2</brand>
</item>
<item>
<reference>00003</reference>
<other_string>PRODUCT 3</other_string>
<brand>BRAND 3</brand>
</item>
<item>
<reference>00004</reference>
<other_string>PRODUCT 4</other_string>
<brand>BRAND 4</brand>
</item>
<item>
<reference>00005</reference>
<other_string>PRODUCT 5</other_string>
<brand>BRAND 5</brand>
</item>
</products>
脚本的输出发送以下消息:
<?xml version="1.0" encoding="utf-8"?>
<products>
<item>
<reference>00001</reference>
<other_string>PRODUCT 1</other_string>
<brand>BRAND 1</brand>
</item>
<item>
<reference>00002</reference>
<other_string>PRODUCT 2</other_string>
<brand>BRAND 2</brand>
</item>
<item>
<reference>00005</reference>
<other_string>PRODUCT 5</other_string>
<brand>BRAND 5</brand>
</item>
</products>
正如您在输出中看到的那样,产品2和产品5之间有空白,我需要将其删除。任何帮助将不胜感激。
答案 0 :(得分:2)
通过将LIBXML_NOBLANKS
选项传递给simplexml_load_file
,可以强制SimpleXML在读取文件时修剪 all 空格:
$sXML = simplexml_load_file('file.xml', null, LIBXML_NOBLANKS);
然后,当您调用->asXML()
时,所有空格都将被删除,并且XML都将显示在一行上,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<products><item><reference>00003</reference><other_string>PRODUCT 3</other_string><brand>BRAND 3</brand></item><item><reference>00004</reference><other_string>PRODUCT 4</other_string><brand>BRAND 4</brand></item></products>
要基于其余结构重新生成空白,您将需要使用DOM而不是SimpleXML-但这很容易,而无需更改任何现有代码,因为dom_import_simplexml
只是“重新包装” XML无需重新修复。
然后,您可以使用the DOMDocument
formatOutput
property和save()
method来“漂亮地打印”文档:
$sXML = simplexml_load_file('file.xml', null, LIBXML_NOBLANKS);
// ...
// process $sXML as before
// ...
$domDocument = dom_import_simplexml($sXML)->ownerDocument;
$domDocument->formatOutput = true;
echo $domDocument->save('filtered.xml');
答案 1 :(得分:0)
另一种可能性是使用preg_replace
:
// Get simpleXml as string
$xmlAsString = $yourSimpleXmlObject->asXML();
// Remove newlines
$xmlAsString = preg_replace("/\n/", "", $xmlAsString);
// Remove spaces between tags
$xmlAsString = preg_replace("/>\s*</", "><", $xmlAsString);
var_dump($xmlAsString);
现在,您可以将XML作为字符串显示在一行中(包括XML声明)。