Enjoy an ad free experience by logging in. Not a member yet?
Register .
12-01-2010, 03:10 PM
PM User |
#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 ..
12-01-2010, 03:56 PM
PM User |
#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.
12-01-2010, 04:06 PM
PM User |
#3
Regular Coder
Join Date: Jun 2010
Posts: 132
Thanks: 8
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
Lamped
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...)
12-01-2010, 04:14 PM
PM User |
#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
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...
12-01-2010, 04:21 PM
PM User |
#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 ..
12-01-2010, 04:29 PM
PM User |
#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
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.
12-01-2010, 04:56 PM
PM User |
#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.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 06:01 AM .
Advertisement
Log in to turn off these ads.