【问题标题】:Creating attribute in domdocument在 domdocument 中创建属性
【发布时间】:2014-10-09 04:05:05
【问题描述】:

我必须制作这种类型的 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>

我为此编写了这段代码,

 $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");

一切正常,但由于某种原因,没有添加用于 urlset 的 xmlns。这里可能有什么问题? 任何建议都会有所帮助。

【问题讨论】:

    标签: php dom domdocument


    【解决方案1】:

    在转换为 simplexml 之前,您需要将根元素附加到文档中:

    $rootElement = $dom->createElementNS('http://www.sitemaps.org/schemas/sitemap/0.9', 'urlset');
    $dom->appendChild($rootElement);
    $sxe = simplexml_import_dom( $dom );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-25
      • 2011-01-15
      • 2014-04-06
      • 1970-01-01
      • 2014-07-07
      • 2015-04-27
      • 2012-12-04
      • 2014-07-13
      相关资源
      最近更新 更多