Php 如何添加嵌套的xml元素

Php 如何添加嵌套的xml元素,php,mysql,xml,multidimensional-array,nested-loops,Php,Mysql,Xml,Multidimensional Array,Nested Loops,我正在准备一个xml属性列表提要,辛迪加将使用它发布我们的列表 我使用的是Cakephp,但不是Cakephp xml工具。只是无法将输出格式化为我需要的格式 所以我有一个非常简单的查询。(为清晰起见,此处未包括许多字段) 然后做一些清理,添加一些数据库中没有的属性 for($i=0;$i<count($properties);$i++){ $properties[$i]['Listing']['StreetAddress'] = '<![CDATA['.$properti

我正在准备一个xml属性列表提要,辛迪加将使用它发布我们的列表

我使用的是Cakephp,但不是Cakephp xml工具。只是无法将输出格式化为我需要的格式

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

然后做一些清理,添加一些数据库中没有的属性

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'].']]>';
}
创建了一个近乎完美的:

 <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>
但这在教堂里也像放屁一样

更新: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;

“它不喜欢空格或引号”——当然不喜欢,因为
createElement
只需要一个标记名。您需要使用
setAttribute
在创建的DOM节点上设置属性。对于
Caption
Description
DescriptionLang
的嵌套,使用当前的方法,您需要创建一个额外的数组维度并使用一个额外的循环。感谢您的小贴士。这是我第一次尝试xml。现在至少我知道该找什么了。
<DescriptionLang value="en">
&quot; 
<DescriptionLang value="en">
    <Caption><![CDATA[ captionhere ]]></Caption>
    <Description><![CDATA[ descriptionhere ]]></Description> 
</DescriptionLang>
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;