umairmasood Posted September 7, 2010 Share Posted September 7, 2010 I have a file named example.xml I want to know how to write to the 3rd line on this file? What function to use? I want to be able to write to this xml file right after the <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> tag. Please someone tell me how? Much appreciated. <?xml version="1.0" encoding="UTF-8"?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap> <loc>http://www.example.com/sitemap1.xml.gz</loc> <lastmod>2004-10-01T18:23:17+00:00</lastmod> </sitemap> <sitemap> <loc>http://www.example.com/sitemap2.xml.gz</loc> <lastmod>2005-01-01</lastmod> </sitemap> </sitemapindex> Quote Link to comment https://forums.phpfreaks.com/topic/212810-writing-in-the-middle-of-a-xml-file/ Share on other sites More sharing options...
btherl Posted September 7, 2010 Share Posted September 7, 2010 There is no way to directly insert data into a file. Instead you need to write the extra lines you want and then write ALL the following data back to the file again. The simplest would be to read the entire file into an array, one item per line, then add the extra lines and write it all back. You could also parse the xml, edit the resulting structure and rewrite it, but that can be tricky to get right. Quote Link to comment https://forums.phpfreaks.com/topic/212810-writing-in-the-middle-of-a-xml-file/#findComment-1108479 Share on other sites More sharing options...
umairmasood Posted September 8, 2010 Author Share Posted September 8, 2010 thanks for your response... So there is no easy way to this? I would have tried the simplest way you mentioned (using an array) but my xml file is supposed to have more than 50,000 lines at least. So this is hugely strain the server. So can you please confirm if there is no other way of achieving this other than the 2 methods you have proposed? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/212810-writing-in-the-middle-of-a-xml-file/#findComment-1108493 Share on other sites More sharing options...
btherl Posted September 8, 2010 Share Posted September 8, 2010 If the file is large, you could take an approach like this: 1. Read the first few lines, write them into a new file 2. Add your extra lines into the new file 3. Read the rest of the lines from the original file, write them into the new file 4. Replace the old file with the new file. That's about the best I can think of. If you do it that way, line by line, then you will have no memory issues when dealing with a large file. You do have to read every line, but there's no way around that if you're inserting text. Quote Link to comment https://forums.phpfreaks.com/topic/212810-writing-in-the-middle-of-a-xml-file/#findComment-1108535 Share on other sites More sharing options...
umairmasood Posted September 8, 2010 Author Share Posted September 8, 2010 Thanks alot... i'll try to implement your suggestion. I'll post here if I encounter any problems with that. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/212810-writing-in-the-middle-of-a-xml-file/#findComment-1108959 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.