Writing in the middle of a xml file

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>