使用php生成多级xml

时间:2015-08-10 11:13:18

标签: php xml

我在物业网站上工作。我想生成属性,设施和属性图像值的属性xml文件。 xml的格式应如下所示: -

<Listings>
<listing>
<count>1</count>
<Ad_Type>Rent</Ad_Type>
<Unit_Type/>
<Unit_Model/>
<Primary_View/>
<Unit_Builtup_Area>240.00</Unit_Builtup_Area>
<No_of_Bathroom>1</No_of_Bathroom>
<Property_Title>Small office with attached toilet</Property_Title>
<Web_Remarks/>
<Emirate/>
<Community>Deira</Community>
<Property_Name>Gold Souq</Property_Name>
<Property_Ref_No>AMB-R-1238</Property_Ref_No>
<Listing_Agent>Agent Name</Listing_Agent>
<Listing_Agent_Phone>Agent Phone</Listing_Agent_Phone>
<Listing_Date>2015-08-07 06:31:37</Listing_Date>
<Last_Updated/>
<Bedrooms/>
<Listing_Agent_Email>Agent Email</Listing_Agent_Email>
<Price>60000</Price>
<Unit_Reference_No>AMB-R-1238</Unit_Reference_No>
<No_of_Rooms/>
<Latitude>25.275016</Latitude>
<Longitude>55.307751</Longitude>
<unit_measure>Sq.Ft.</unit_measure>
<Featured>0</Featured>
<Images>
<image>
Image Url
</image>
<image>
Image Url
</image>
<image>
Image Url
</image>
</Images>
<company_name/>
<Web_Tour/>
<Threesixty_Tour/>
<Audio_Tour/>
<Virtual_Tour/>
<QR_Code/>
<company_logo>My Site Logo URL</company_logo>
<Parking>0</Parking>
<Strno/>
<PreviewLink/>
</listing>
</Listings>

我使用以下代码生成了该文件: -

$property_detail = $db->Select("property","*"); 
                        $xml = new DOMDocument("1.0");
                        $root = $xml->createElement("Listings");
                        $xml->appendChild($root);
                        foreach($property_detail as $key => $propval){
                            $AgentDet = $db->GetRow("user_details", "first_name,last_name,mobile_number,email_address","user_id = :user_id",array("user_id"=>$propval['user_id']));
                            $Agentname = $AgentDet["first_name"]." ".$AgentDet["last_name"];

                            if($key == 0){ 
                                $key = 1; 
                            }else{ 
                                $key = $key + 1; 
                            }

                            if($propval["status"] == "0"){
                                $adTypeVal = "Rent";
                            }else if($propval["status"] == "1"){
                                $adTypeVal = "Commercial";
                            }else if($propval["status"] == "2"){
                                $adTypeVal = "Sale";
                            }else if($propval["status"] == "3"){
                                $adTypeVal = "Sublet";
                            }else if($propval["status"] == "4"){
                                $adTypeVal = "rooms";
                            }else if($propval["status"] == "5"){
                                $adTypeVal = "Land";
                            }

                            $count = $xml->createElement("count");
                            $countText = $xml->createTextNode($key);
                            $count->appendChild($countText);

                            $Ad_Type = $xml->createElement("Ad_Type");
                            $Ad_TypeText = $xml->createTextNode($adTypeVal);
                            $Ad_Type->appendChild($Ad_TypeText);

                            $Unit_Type = $xml->createElement("Unit_Type");
                            $Unit_TypeText = $xml->createTextNode("");
                            $Unit_Type->appendChild($Unit_TypeText);

                            ... // a lot of other properties

                            $book = $xml->createElement("listing");
                            $book->appendChild($count);
                            $book->appendChild($Ad_Type);
                            $book->appendChild($Unit_Type);
                            $book->appendChild($Unit_Model);
                            $book->appendChild($Primary_View);
                            $book->appendChild($Unit_Builtup_Area);
                            $book->appendChild($No_of_Bathroom);
                            $book->appendChild($Property_Title);
                            $book->appendChild($Web_Remarks);
                            $book->appendChild($Emirate);
                            $book->appendChild($Community);
                            $book->appendChild($Property_Name);
                            $book->appendChild($Property_Ref_No);
                            $book->appendChild($Listing_Agent);
                            $book->appendChild($Listing_Agent_Phone);
                            $book->appendChild($Listing_Date);
                            $book->appendChild($Last_Updated);
                            $book->appendChild($Bedrooms);
                            $book->appendChild($Listing_Agent_Email);
                            $book->appendChild($Price);
                            $book->appendChild($Unit_Reference_No);
                            $book->appendChild($No_of_Rooms);
                            $book->appendChild($Latitude);
                            $book->appendChild($Longitude);
                            $book->appendChild($unit_measure);
                            $book->appendChild($Featured);
                            $book->appendChild($company_name);
                            $book->appendChild($Web_Tour);
                            $book->appendChild($Threesixty_Tour);
                            $book->appendChild($Audio_Tour);
                            $book->appendChild($Virtual_Tour);
                            $book->appendChild($QR_Code);
                            $book->appendChild($company_logo);
                            $book->appendChild($Parking);
                            $book->appendChild($Strno);
                            $book->appendChild($PreviewLink);
                            $root->appendChild($book);
                            $xml->formatOutput = true;
                            echo "<xmp>". $xml->saveXML() ."</xmp>";
                            $xml->save("../propertyxml/mybooks.xml") or die("Error");
                            exit;
                        }

一切都很好。除了属性图像细节,我得到相同的输出。如何在此xml代码中添加Image的另一个节点?我正在使用以下代码:

//This is where i am forming the imagenames
foreach($propimageDetail as $p => $image){
    $img = $xml->createElement("image");
    $imgText = $xml->createTextNode($image["property_imagename"]);
    $img->appendChild($imgText);
}

//This is where i am appending the imagenames
foreach($propimageDetail as $p => $image){
    $Images->appendChild($img);
} 

1 个答案:

答案 0 :(得分:1)

你不应该使用两个foreach循环 - 你每次都要覆盖$img。您需要先创建Images节点,然后只有然后遍历$propImageDetail

$Images = $xml->createElement("images");
foreach($propimageDetail as $p => $image){
    $img = $xml->createElement("image");
    $imgText = $xml->createTextNode($image["property_imagename"]);
    $img->appendChild($imgText);
    $Images->appendChild($img);
}