如何添加嵌套的xml元素

时间:2015-02-22 01:12:32

标签: 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);

然后进行一些清理并添加一些不在db中的属性。

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>

我尝试过更多疯狂的东西,我可以在这里添加。好像我应该能够在清洁步骤中添加另一个级别,但不是。

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

1 个答案:

答案 0 :(得分:0)

我完成了这个项目,以为我会分享最终的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;
相关问题