0

I have this xml file:

<flats>
    <flat>

        <images1>http://www.example.com/image1.jpg</images1>
        <images2>http://www.example.com/image1.jpg</images2>

    </flat>
</flats>

Which I need to load using php and then replace some node names to get this (just change image1 and image2 to images:

<flats>
    <flat>

        <images>http://www.example.com/image1.jpg</images>
        <images>http://www.example.com/image1.jpg</images>

    </flat>
</flats>

I managed to load and save the file, but I don't know how to replace the node names.

$xml_external_path = 'http://external-site.com/original.xml';
$xml = simplexml_load_file($xml_external_path);

$xml->asXml('updated.xml');

UPDATE: PHP

    $xml_external_path = 'http://external-site.com/original.xml';
    $xml = simplexml_load_file($xml_external_path);
    print_r($xml);
    $newXml = str_replace( 'images1', 'image',$xml ) ;
    print_r($newXml);

2 Answers 2

1

If you want to just rename the <images1> and <images2> tags I suggest you go with the simplest solution, i.e. text replace using str_replace.

$xml_external_path = 'http://external-site.com/original.xml';
$xml = simplexml_load_file($xml_external_path);

$searches = ['<images1>', '<images2>', '</images1>', '</images2>'];
$replacements = ['<images>', '<images>', '</images>', '</images>'];

$newXml = simplexml_load_string( str_replace( $searches, $replacements, $xml->asXml() ) );
$newXml->asXml('updated.xml');

If you however need a more sophisticated way to handle the task you may want to take a look at the DOMDocument class and build your own new XML

Sign up to request clarification or add additional context in comments.

5 Comments

not working, $searches and $replacements are arrays? you do the saving before the str_replace function?¿
Yes, they're arrays. And can you be more specific how exactly is it not working? Btw if you're using PHP before 5.4 you can't use that short syntax and should use $searches = array('<images1>', '<images2>', '</images1>', '</images2>'); and the same for $replacements.
I updated my answer. See at the bottom. No errors, the replacement doesn't work at all.
I think a xml file is not a string where I can use str_replace.
I updated my answer to save the new XML into a file and btw from the manual If the filename isn't specified, this function returns a string on success and FALSE on error.
0

Parse the original xml and create a new xml:

$oldxml = simplexml_load_file("/path/to/file.xml");

// create a new XML trunk
$newxml = simplexml_load_string("<flats><flat/></flats>");

// iterate over child-nodes of <flat>
// check their name 
// if name contains "images", add a new child in $newxml

foreach ($oldxml->flat->children() as $key => $value) 
    if (substr($key, 0, 6) == "images") 
        $newxml->flat->addChild("images", $value);

// display new XML:

echo $newxml->asXML();

see it working: https://eval.in/301221

EDIT: If all children of <flat> are <imageX>, no need for checking $key:

foreach ($oldxml->flat->children() as $value) 
    $newxml->flat->addChild("images", $value); 

EDIT: Here's a short and sweet selection of nodes to be changed using xpath:

foreach ($oldxml->xpath("/flats/flat/*[starts-with(local-name(), 'images')]") as $value)
    $newxml->flat->addChild("images", $value);

The xpath statement above will select all children of <flat> starting with "images" and put them in an array.

see it working: https://eval.in/301233

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.