2016-07-22 67 views
0

嗨,我一直試圖用PHP生成以下XML。具有多個根的XML

我設法生成除了位置部分以外的所有東西,因爲它似乎將它視爲另一個根和出錯。有沒有人知道產生這個的正確方法?

<?xml version="1.0" encoding="UTF-8"?> 
    <classifieds xmlns="http://www.bdjjobs.com/ClassifiedJobFromRecruiterFeed"> 
    <job> 
     <reference_id>20161114-72</reference_id> 
     <recruiter>Smiles R Us</recruiter> 
     <job_title>Denture Specialist</job_title> 
     <short_description><![CDATA[An exciting position in a dynamic dental practice]]></short_description> 
     <description><![CDATA[<p>Full description of post</p>]]></description> 
     <location> 
     <city>city</city> 
     <state>county</state> 
     <country>country</country> 
     </location> 
     <salary_description><![CDATA[Full Package]]></salary_description> 
     <organisations>Independent Dental Practice</organisations> 
     <job_type>Specialist Appointments</job_type> 
     <salary_band>£100,000 or more</salary_band> 
     <contract_type>Associate Permanent</contract_type> 
     <hours>Full time</hours> 
     <practice_type>Mixed (NHS/Private)</practice_type> 
     <start_date>2016-09-01</start_date> 
     <expiry_date>2016-10-30</expiry_date> 
     <application_email>[email protected]</application_email> 
    </job> 
    </classifieds> 

任何援助將不勝感激,謝謝:)

下面是代碼:

// Start Job Element 
$job_element = $xml_document->createElement("job"); 

// Job ID 
$rootreferencenumber = $xml_document->createElement("reference_id"); 
$rootreferencenumber->appendChild($xml_document->createCDATASection(get_the_ID())); 
$job_element->appendChild($rootreferencenumber); 

// Recruiter 
$recruiter = $xml_document->createElement("recruiter"); 
$recruiter->appendChild($xml_document->createCDATASection("MBR Dental Recruitment")); 
$job_element->appendChild($recruiter); 

// Job title 
$title = $xml_document->createElement("title"); 
$title->appendChild($xml_document->createCDATASection(get_the_title())); 
$job_element->appendChild($title); 

// Job Description 
$description = $xml_document->createElement("description"); 
$description->appendChild($xml_document->createCDATASection((strip_tags(str_replace("</p>", "\n\n", get_the_content()))))); 
$job_element->appendChild($description); 

// City 
$city = $xml_document->createElement("city"); 
$get_city = explode(',', get_post_meta(get_the_ID(), 'geolocation_city', true)); 
$city->appendChild($xml_document->createCDATASection($get_city[0])); 
$job_element->appendChild($city); 

// Region from Taxonomy 
$region = $xml_document->createElement("region"); 
$categories = wp_get_post_terms(get_the_ID(), 'job_listing_region', array("fields" => "names")); 
if ($categories && ! is_wp_error($categories)) { 
    $region->appendChild($xml_document->createCDATASection(implode(',', $categories))); 
} else { 
    $region->appendChild($xml_document->createCDATASection('')); 
} 
$job_element->appendChild($region); 


// Create Company Name 
$company_name = $xml_document->createElement("organisations"); 
$company_name->appendChild($xml_document->createCDATASection('xx')); 
$job_element->appendChild($company_name); 

// Create Phone Number 
$phone_number = $xml_document->createElement("phone_number"); 
$phone_number->appendChild($xml_document->createCDATASection('0000')); 
$job_element->appendChild($phone_number); 

// Job direct URL 
$url = $xml_document->createElement("application_url"); 
$url->appendChild($xml_document->createCDATASection(get_permalink(get_the_ID()))); 
$job_element->appendChild($url); 

// Category 
$phone_number = $xml_document->createElement("job_type"); 
$phone_number->appendChild($xml_document->createCDATASection('Job')); 
$job_element->appendChild($phone_number); 

// Subcategory 
$region = $xml_document->createElement("subcategory"); 
$categories = wp_get_post_terms(get_the_ID(), 'job_listing_category', array("fields" => "names")); 
if ($categories && ! is_wp_error($categories)) { 
    $region->appendChild($xml_document->createCDATASection(implode(',', $categories))); 
} else { 
    $region->appendChild($xml_document->createCDATASection('')); 
} 
$job_element->appendChild($region); 


// Job date based on todays date 
$date = $xml_document->createElement("start_date"); 
$date->appendChild($xml_document->createCDATASection(date(Ymd))); 
$job_element->appendChild($date); 

// Job date expire from original post date 
$expiry_date = $xml_document->createElement("expiry_date"); 
$wpDate = (date(Ymd)); 
$wpDate = new DateTime($wpDate); 
$wpDate->add(new DateInterval('P14D')); // P14D means a period of 14 days 
$wpDate = $wpDate->format('Ymd'); 
$expiry_date->appendChild($xml_document->createCDATASection($wpDate)); 
$job_element->appendChild($expiry_date); 

// Create Application Email Address 
$app_email = $xml_document->createElement("application_email"); 
$app_email->appendChild($xml_document->createCDATASection('[email protected]')); 
$job_element->appendChild($app_email); 

// End Job Element 
$root->appendChild($job_element); 
+1

你是什麼意思與「爲節」嗎?哪一節?還是我瞎了? – LuigiPower

+0

@ 1up道歉,我把這個單詞的位置包裹在括號內,並沒有顯示出來。我的意思是位置部分。我已經編輯了上面的帖子。謝謝 – Roger

+1

我不能說我過去做過這樣的事情,但是我可以看到代碼,以便我可以嘗試提供幫助嗎? – LuigiPower

回答

0

好了,所以我一直在處理這個完全錯誤..

我需要將子元素(City + Region)附加到新的$ location元素。而不是原來的$ job_element

這裏是正確的代碼:

// Start Job Element 
$job_element = $xml_document->createElement("job"); 

// Job ID 
$rootreferencenumber = $xml_document->createElement("reference_id"); 
$rootreferencenumber->appendChild($xml_document->createCDATASection(get_the_ID())); 
$job_element->appendChild($rootreferencenumber); 

// Recruiter 
$recruiter = $xml_document->createElement("recruiter"); 
$recruiter->appendChild($xml_document->createCDATASection("xx")); 
$job_element->appendChild($recruiter); 

// Job title 
$title = $xml_document->createElement("title"); 
$title->appendChild($xml_document->createCDATASection(get_the_title())); 
$job_element->appendChild($title); 

// Job Description 
$description = $xml_document->createElement("description"); 
$description->appendChild($xml_document->createCDATASection((strip_tags(str_replace("</p>", "\n\n", get_the_content()))))); 
$job_element->appendChild($description); 

//Create Location Element 
$location = $xml_document->createElement("location"); 
$job_element->appendChild($location); 

// City 
$city = $xml_document->createElement("city"); 
$get_city = explode(',', get_post_meta(get_the_ID(), 'geolocation_city', true)); 
$city->appendChild($xml_document->createCDATASection($get_city[0])); 
$location->appendChild($city); 

// Region from Taxonomy 
$region = $xml_document->createElement("region"); 
$categories = wp_get_post_terms(get_the_ID(), 'job_listing_region', array("fields" => "names")); 
if ($categories && ! is_wp_error($categories)) { 
    $region->appendChild($xml_document->createCDATASection(implode(',', $categories))); 
} else { 
    $region->appendChild($xml_document->createCDATASection('')); 
} 
$location->appendChild($region); 

// Country Code 
$country = $xml_document->createElement("country"); 
$country->appendChild($xml_document->createCDATASection('UK')); 
$location->appendChild($country); 

// Create Company Name 
$company_name = $xml_document->createElement("organisations"); 
$company_name->appendChild($xml_document->createCDATASection('xx')); 
$job_element->appendChild($company_name); 

// Create Phone Number 
$phone_number = $xml_document->createElement("phone_number"); 
$phone_number->appendChild($xml_document->createCDATASection('xx')); 
$job_element->appendChild($phone_number); 

// Job direct URL 
$url = $xml_document->createElement("application_url"); 
$url->appendChild($xml_document->createCDATASection(get_permalink(get_the_ID()))); 
$job_element->appendChild($url); 

// Category 
$job_type = $xml_document->createElement("job_type"); 
$job_type->appendChild($xml_document->createCDATASection('Job')); 
$job_element->appendChild($job_type); 

// Subcategory 
$region = $xml_document->createElement("subcategory"); 
$categories = wp_get_post_terms(get_the_ID(), 'job_listing_category', array("fields" => "names")); 
if ($categories && ! is_wp_error($categories)) { 
    $region->appendChild($xml_document->createCDATASection(implode(',', $categories))); 
} else { 
    $region->appendChild($xml_document->createCDATASection('')); 
} 
$job_element->appendChild($region); 


// Job date based on todays date 
$date = $xml_document->createElement("start_date"); 
$date->appendChild($xml_document->createCDATASection(date(Ymd))); 
$job_element->appendChild($date); 

// Job date expire from original post date 
$expiry_date = $xml_document->createElement("expiry_date"); 
$wpDate = (date(Ymd)); 
$wpDate = new DateTime($wpDate); 
$wpDate->add(new DateInterval('P14D')); // P14D means a period of 14 days 
$wpDate = $wpDate->format('Ymd'); 
$expiry_date->appendChild($xml_document->createCDATASection($wpDate)); 
$job_element->appendChild($expiry_date); 

// Create Application Email Address 
$app_email = $xml_document->createElement("application_email"); 
$app_email->appendChild($xml_document->createCDATASection('[email protected]')); 
$job_element->appendChild($app_email); 

// End Job Element 
$root->appendChild($job_element); 
+0

這是我找到我的解決方案的地方:http://stackoverflow.com/questions/21759632/creating-sub-element-in-xml – Roger