php - 如何添加嵌套 xml 元素

标签 php mysql xml multidimensional-array nested-loops

我正在整理一个 xml 属性列表提要,供辛迪加者用来发布我们的列表。

我正在使用 Cakephp,但没有使用 Cakephp xml 工具。只是无法按照我需要的方式格式化输出。

所以我有一个非常简单的查询。 (为了清楚起见,此处未包含许多字段)

$sql = "select 
        Listing.id as PropertyId, 
        Listing.address as StreetAddress,
        Listing.title as Caption,
        Listing.description as Description
        from properties as Listing
        limit 2";

$properties = $this->Property->query($sql);

然后进行一些清理并添加一些数据库中没有的属性。

for($i=0;$i<count($properties);$i++){
     $properties[$i]['Listing']['StreetAddress'] = '<![CDATA['.$properties[$i]['Listing']['StreetAddress'].']]>';
     $properties[$i]['Listing']['DescriptionLang'] = "x";     
     $properties[$i]['Listing']['Caption'] = '<![CDATA['.$properties[$i]['Listing']['Caption'].']]>';
     $properties[$i]['Listing']['Description'] = '<![CDATA['.$properties[$i]['Listing']['Description'].']]>';
}

我正在使用这个函数来处理数组...

header("Content-type: text/xml; charset=utf-8");
$domtree = new DOMDocument('1.0', 'UTF-8');
$xmlRoot = $domtree->createElement("Listings");
$xmlRoot = $domtree->appendChild($xmlRoot);
foreach($properties as $p){
     foreach ($p as $key=>$value){
         $currentElement= $domtree->createElement($key);
         $currentElement= $xmlRoot->appendChild($currentElement);
         if(is_array($value))
         {
             foreach ($value as $k=>$v)
             {
                 $currentElement->appendChild($domtree->createElement($k,$v));
             }
         }
     }
 }   
 echo $domtree->saveXML();
 exit;

创建了这个,这几乎是完美的:

 <Listings>
  <Listing>
    <PropertyId>2</PropertyId>
    <StreetAddress><![CDATA[243 E 7th Ave]]></StreetAddress>
    <Caption><![CDATA[Wholesale Deal]]></Caption>
    <Description><![CDATA[]]></Description>
    <DescriptionLang>x</DescriptionLang>
  </Listing>
  <Listing>
    <PropertyId>3</PropertyId>
    <StreetAddress><![CDATA[3724 W Glenn Dr]]></StreetAddress>
    <Caption><![CDATA[Wholesale Deal]]></Caption>
    <Description><![CDATA[]]></Description>
    <DescriptionLang>x</DescriptionLang>
   </Listing>
  </Listings>

输出看起来很棒...除了我需要做两件事。

1.) DescriptionLang 的标签需要如下所示,但会导致问题...

<DescriptionLang value="en">

它不喜欢空格或引号。对于我尝试过的报价...

&quot; 

但这在教堂里也像放屁一样。

更新:xml 规范不允许空格。因此,我正在与辛迪加人核实,看看他是否可以告诉我他们如何逃脱无法完成的事情。

2.) 但最重要的是 Caption 和 Description 元素需要嵌套在 DescriptionLang 元素中,如下所示...

<DescriptionLang value="en">
    <Caption><![CDATA[ captionhere ]]></Caption>
    <Description><![CDATA[ descriptionhere ]]></Description> 
</DescriptionLang>

我尝试过更多疯狂的东西,我可以将它们包含在这里。似乎我应该能够在清洁步骤中添加另一个级别,但没有。

当然可以在这里使用一些指导。

最佳答案

我完成了这个项目,所以我想分享最终的 xml 数组构建器。在数据规范中,只有少数元素需要设置一些属性或嵌套元素。我不知道是否有更简单的方法来做到这一点,因为这是我第一次尝试 xml。非常适合我的需要。

header("Content-type: text/xml; charset=utf-8");
        $domtree = new DOMDocument('1.0', 'UTF-8');
        $xmlRoot = $domtree->createElement("Listings");
        $xmlRoot = $domtree->appendChild($xmlRoot);
        foreach($properties as $p){
            foreach ($p as $key=>$value){
                $currentElement= $domtree->createElement($key);
                $currentElement= $xmlRoot->appendChild($currentElement);
                if(is_array($value))
                {
                    foreach ($value as $k=>$v)
                    {
                        if(!in_array($k,array('Caption','Description','DetailsURL','PhotoURL')))
                        $level = $currentElement->appendChild($domtree->createElement($k,$v));

                        if($k == "DescriptionLang"){//create nested elements
                            $level->setAttribute('value', 'en');
                            foreach($value as $k1=>$v1){
                                if(in_array($k1,array('Caption','Description','DetailsURL'))){
                                    $level->appendChild($domtree->createElement($k1,$v1));
                                }
                            }
                        }

                        if($k == 'PhotoURL'){//create photo elements
                            $images = $this->grab_pics($v);
                            if(!empty($images))
                            foreach($images as $i){
                               $url =  FULL_BASE_URL.'/property_images/'.$v.'/'.$i.'.jpg'; 
                              $currentElement->appendChild($domtree->createElement($k,$url));  
                            }
                        }
                    }
                }
            }
        }   
        echo $domtree->saveXML();
        exit;

关于php - 如何添加嵌套 xml 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28653377/

相关文章:

php - 扩展php注册系统

mysql - 使用查询而不使用 mysqldump 备份数据库

xml - 从youtube api XSLT中获取观看次数

iphone - 属性文本中的 NSXMLParser 引号

php - php中的用户图片

php - WordPress 插件内子页面的 url 解析

c# - 从数据库加载后获取黑色图像

php - MySQL 按类别输出并分页

xml - 如何从 Xml 字符串创建文档

php - 防止 Laravel 中的用户帐户同时提款