用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之间存在一个空白,我需要将其删除。任何帮助都会得到欣赏。

您可以通过将 LIBXML_NOBLANKS选项传递给 simplexml_load_file

时将读取文件时的简称为trim all whitespace。
$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而不是单纯词 - 但这很容易在不更改任何现有代码的情况下进行,因为dom_import_simplexml只是"重新包装" XML而不对其进行重新进行。

然后,您可以使用DOMDocument formatOutput属性和save()方法来"漂亮印刷"文档:

$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');

另一种可能性是使用 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声明)。

最新更新