duanhuancong1969 2012-12-20 11:34
浏览 21
已采纳

使用SimpleXML编写XML文件时如何使用命名空间

I’m writing a Google products RSS feed with SimpleXML in PHP. I’ve got my products coming from the database and creating the RSS file fine, but having problems when it comes to namespaces.

I’ve Googled and search Stack Overflow and come across dozens of posts of how to parse XML feeds containing namespaces, but my issue is actually authoring an XML file with a namespace.

Here is what the file should look like:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version ="2.0" xmlns:g="http://base.google.com/ns/1.0">
    <!-- content -->
</rss>

And here is my code:

<?php

$xml = new SimpleXMLElement('<rss></rss>');
$xml->addAttribute('version', '2.0');

$xml->addChild('channel');
$xml->channel->addChild('title', 'Removed');
$xml->channel->addChild('description', 'Removed');
$xml->channel->addChild('link', 'Removed');

foreach ($products as $product) {
    $item = $xml->channel->addChild('item');
    $item->addChild('title', htmlspecialchars($product['title']));
    $item->addChild('description', htmlspecialchars($product['title']));
    $item->addChild('link', $product['url']);
    $item->addChild('id', $product['product_id']);
    $item->addChild('price', $product['price_latest']);
    $item->addChild('brand', $product['range']);
    $item->addChild('condition', 'new');
    $item->addChild('image_link', $product['image']);
}

How do I introduce the g namespace, both the xmlns declaration in the root rss element, and then as a prefix for id, price, brand, condition and image_link in each item element?

  • 写回答

3条回答 默认 最新

  • douxian1770 2012-12-20 12:10
    关注

    Here is an example of how to do this using DOM:

    <?php
    
        $nsUrl = 'http://base.google.com/ns/1.0';
    
        $doc = new DOMDocument('1.0', 'UTF-8');
    
        $rootNode = $doc->appendChild($doc->createElement('rss'));
        $rootNode->setAttribute('version', '2.0');
        $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:g', $nsUrl);
    
        $channelNode = $rootNode->appendChild($doc->createElement('channel'));
        $channelNode->appendChild($doc->createElement('title', 'Removed'));
        $channelNode->appendChild($doc->createElement('description', 'Removed'));
        $channelNode->appendChild($doc->createElement('link', 'Removed'));
    
        foreach ($products as $product) {
            $itemNode = $channelNode->appendChild($doc->createElement('item'));
            $itemNode->appendChild($doc->createElement('title'))->appendChild($doc->createTextNode($product['title']));
            $itemNode->appendChild($doc->createElement('description'))->appendChild($doc->createTextNode($product['title']));
            $itemNode->appendChild($doc->createElement('link'))->appendChild($doc->createTextNode($product['url']));
            $itemNode->appendChild($doc->createElement('g:id'))->appendChild($doc->createTextNode($product['product_id']));
            $itemNode->appendChild($doc->createElement('g:price'))->appendChild($doc->createTextNode($product['price_latest']));
            $itemNode->appendChild($doc->createElement('g:brand'))->appendChild($doc->createTextNode($product['range']));
            $itemNode->appendChild($doc->createElement('g:condition'))->appendChild($doc->createTextNode('new'));
            $itemNode->appendChild($doc->createElement('g:image_link'))->appendChild($doc->createTextNode($product['image']));
        }
    
        echo $doc->saveXML();
    

    See it working

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路