用PHP删除XML中的标记值之间的空白 [英] Remove white spaces between tag values in xml with php

查看:146
本文介绍了用PHP删除XML中的标记值之间的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索信息,以了解如何在将PHP导出到XML时删除由PHP代码留下的标记值之间的空格,我将详细解释,首先加载和XML,然后使用xPath在文件上进行搜索,然后删除一些与某些品牌不匹配的元素,最后我将其重新导出为新XML,问题是此新XML充满了代码留下的空白。我试过修剪它,但它似乎不能正常工作。

I been searching information how to remove white spaces between tag values leaved by a PHP code when I export it to XML, I will explain detailed, first I load and XML then I do a search on the file with xPath, then I remove some elements that do not match some brands and finally I reexport it as a new XML, the problem is that this new XML is full of white spaces leaved by the code. I tried trim it but it doesn't seems to work correctly.

这是我的代码:

<?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>

脚本输出将发送以下内容:

And the output of the script sends this:

<?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之间有一个空白,我需要删除它。任何帮助将不胜感激。

As you can see on the output, there is a white space between product 2 and product 5 and that I need to remove it. Any help will be appreciate.

推荐答案

您可以强制SimpleXML在读取 all 空格时将其修剪掉文件,通过将 LIBXML_NOBLANKS 选项传递给 simplexml_load_file

You can force SimpleXML to trim all whitespace when it reads the file, by passing the LIBXML_NOBLANKS option to simplexml_load_file:

$sXML = simplexml_load_file('file.xml', null, LIBXML_NOBLANKS);

然后,当您调用-> asXML(),所有空白都将被删除,您将在一行上全部获得XML,如下所示:

Then when you call ->asXML(), all the whitespace will be removed, and you'll get XML all on one line, like this:

<?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而无需重新解析它。

To re-generate whitespace based on the remaining structure, you'll need to use DOM rather than SimpleXML - but that's easy to do without changing any of your existing code, because dom_import_simplexml simply "rewraps" the XML without reparsing it.

然后,您可以使用 DOMDocument formatOutput 属性 save()方法漂亮地打印文档:

Then you can use the DOMDocument formatOutput property and save() method to "pretty-print" the document:

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

这篇关于用PHP删除XML中的标记值之间的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆