簡體   English   中英

使用PHP生成多層XML

[英]Generate multilevel xml using php

我正在房地產網站上工作。 我想生成具有屬性,便利性和屬性圖像值的屬性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);
} 

您不應該使用兩個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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM