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&amp;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

Popular posts from this blog

java - Bypassing "final local variable defined in an enclosing type" -

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -