具有多个根的XML

时间:2016-07-22 14:06:45

标签: php xml

您好我一直在尝试使用PHP生成以下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>apply_here@email.com</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('xx@xx.com'));
$job_element->appendChild($app_email);

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

1 个答案:

答案 0 :(得分: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('x@xx.net'));
$job_element->appendChild($app_email);

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