Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Automatically Chane URLs

    I have an XML file, I want every url that point to example1.com (in that XML file) to be rewritten as example2.com... I do not want the original xml to be edited... what I want is that My server automatically make a New xml file (that is the same as the original one) and in this xml the urls will be pointing to example2.com (and not like the original xml that has example1.com), So how can I do that?
    (any suggestions are helpfully, and XML solutions is also acceptable...)
    Last edited by Tony M; 12-01-2010 at 03:12 PM.

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Consider this pseudo code, an example of a way of doing this, but I've never used SimpleXMLIterator before...

    PHP Code:
    $xml simplexml_load_file('myfile.xml');
    $iterator = new SimpleXMLIterator($xml);
    while (
    $node $iterator->next()) {
        if (
    $node == 'example1.com') {
            
    $node 'example2.com';
        }
    }
    file_put_contents('newfile.xml'$xml->asXML()); 
    Something like that, anyway.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #3
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Lamped View Post
    Consider this pseudo code, an example of a way of doing this, but I've never used SimpleXMLIterator before...

    PHP Code:
    $xml simplexml_load_file('myfile.xml');
    $iterator = new SimpleXMLIterator($xml);
    while (
    $node $iterator->next()) {
        if (
    $node == 'example1.com') {
            
    $node 'example2.com';
        }
    }
    file_put_contents('newfile.xml'$xml->asXML()); 
    Something like that, anyway.
    Ok, When the newfile.xml be created? (when I access it or when I access the original file...)

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Quote Originally Posted by Tony M View Post
    Ok, When the newfile.xml be created? (when I access it or when I access the original file...)
    Every time you run that code. Also note: You don't have to write it to a file. You can run that code and omit the file_put_contents, and just continue to use the $xml object with the new values.

    ... assuming my pseudo code works...
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    Regular Coder
    Join Date
    Jun 2010
    Posts
    132
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Ok, I tested Your code and it didn't worked (for the reason that I do not know why), what is wrong? (SimpleXML s enabled on the server)
    Last edited by Tony M; 12-01-2010 at 04:28 PM.

  • #6
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Quote Originally Posted by Tony M View Post
    Ok, I tested Your code and it didn't worked (for the reason that I do not know why), what is wrong? (SimpleXML s enabled on the server)
    I did say it was more of rough pseudo code than a working example

    I'll have a proper look at it now.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #7
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    PHP Code:
    $xml simplexml_load_file('myfile.xml');

    function 
    processxml($xml$find$replace) {
        foreach(
    $xml as $key => $node) {
            if (
    count($node->children())) {
                
    processxml($node$find$replace);
            }
            if (
    $xml->{$key}) {
                @
    $xml->{$key} = str_replace($find$replace$xml->{$key});
            }
        }
    }

    processxml($xml'example1''example2');
    echo(
    $xml->asXML()); 
    There, a find and replace in every node of the xml, excludes attributes.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •