php - Creating attribute in domdocument -
php - Creating attribute in domdocument -
i have create type of xml :-
<?xml version="1.0" encoding="utf-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://www.example.com/</loc> <lastmod>2005-01-01</lastmod> <changefreq>monthly</changefreq> <priority>0.8</priority> </url> <url> <loc>http://www.example.com/catalog?item=12&desc=vacation_hawaii</loc> <changefreq>weekly</changefreq> </url> </urlset>
for have written code,
$dom = new domdocument('1.0', 'utf-8'); $dom->formatoutput = true; $rootelement = $dom->createelementns('http://www.sitemaps.org/schemas/sitemap/0.9', 'urlset'); $sxe = simplexml_import_dom( $dom ); $urlmain = $sxe->addchild("url"); $loc = $urlmain->addchild("loc","http://www.example.com"); $lastmod = $urlmain->addchild("lastmod","$date"); $changefreq = $urlmain->addchild("changefreq","daily"); $priority = $urlmain->addchild("priority","1");
everything works fine, reason xmlns
urlset not getting added. might wrong here? suggestion helpful.
you need append root element document prior conversion simplexml:
$rootelement = $dom->createelementns('http://www.sitemaps.org/schemas/sitemap/0.9', 'urlset'); $dom->appendchild($rootelement); $sxe = simplexml_import_dom( $dom );
php dom domdocument
Comments
Post a Comment