Filter language from an XML file in PHP

0

I wanted to know if it is possible to remove certain tags from an XML in PHP.

I need the data only in Spanish (0) and I would like to obtain the data in a new XML without the other languages. I do not know what type of filtering I should use exactly. And I'm a little lost.

If I use xPatch:

//adComments[language="0"]

I would only filter and get the comments, and I also need the other references.

The structure is as follows:

<ads>
    <comments>
      <adComments>
        <propertyComment>
          Extraordinaria oportunidad.
        </propertyComment>
        <language>0</language>
      </adComments>
      <adComments>
        <propertyComment>
          Extraordinaire opportunité.
        </propertyComment>
        <language>2</language>
      </adComments>
      <adComments>
        <propertyComment>
          Oportunidade de investimento.
        </propertyComment>
        <language>4</language>
        </adComments>
        <adComments>
          <propertyComment>
            Extraordinary investment.
          </propertyComment>
          <language>1</language>
        </adComments>
    </comments>
    <multimedias>
    </multimedias>
    <services>
    </services>
</ads>

I update:

I've tried like this:

<?php
header('Content-Type: application/json');

$respuestaFTP = 
file_get_contents("xml.url");

$xml = simplexml_load_string($respuestaFTP); 
$items = $xml->xpath("/ads/comments/adComments[language > 0]");
foreach ($items as $i) unset($i[0]);

$convertido = $xml->asXML();

$str = convertido;
echo trim($str, '"');

And I get the following:

<ads>
    <comments>
      <adComments>
        <propertyComment>
          Extraordinaria oportunidad.
        </propertyComment>
        <language>0</language>
      </adComments>



    </comments>
    <multimedias>
    </multimedias>
    <services>
    </services>
</ads>

I could only eliminate those remaining spaces.

Does anyone have any idea how to do it?

Thank you very much everyone.

    
asked by Maso 17.11.2018 в 16:09
source

1 answer

-1

Solved. These confusions kill me.

header('Content-Type: application/xml');
    
answered by 18.11.2018 в 10:20