简体   繁体   中英

XML Parsing Error… No Element Found

I have a script generating an XML document that has suddenly stopped working. When I run it I get this error:

XML Parsing Error: no element found Location: http://www.mydomain.com/indeed-general-xml/ Line Number 1, Column 1:

Here's the code and loop that generate the node structure:

    header('Content-Type: text/xml');
    //create DOMDocument object and set char encoding
    $doc = new DOMDocument('1.0','utf-8');
    $doc->formatOutput = true;

    //root element
    $r = $doc->createElement( "source" );
    //append root element to our document
    $doc->appendChild( $r ); 

    $publisher = $doc->createElement("publisher");
    $publisher->appendChild($doc->createTextNode("mydomain.com"));
    $r->appendChild($publisher);

    $publisherurl = $doc->createElement("publisherurl");
    $publisherurl->appendChild($doc->createTextNode("http://mydomain.com"));
    $r->appendChild($publisherurl);

    $job = array();
    //set args for query
    $args = array(
        'post_author' => -1,
        'post_type' => 'job_listing',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'orderby' => 'date',
        'order' => 'DESC'
        );
    //run query
    $feed_jobs = new WP_Query($args);while($feed_jobs->have_posts()) : $feed_jobs->the_post(); 
    $b = $doc->createElement( "job" );

    //begin loop
    (while $feed_jobs->have_posts()) : $feed_jobs->the_post();

    $title = $doc->createElement("title");    
    $title->appendChild($doc->createCDATASection( $post->post_title));
    $b->appendChild( $title );

    $company_name = "Sample Company Name";
    $company = $doc->createElement("company");
    $company->appendChild($doc->createCDATASection($company_name));
    $b->appendChild($company); 

    $date = $doc->createElement("date");
    $date->appendChild($doc->createCDATASection($post->post_date));
    $b->appendChild($date);

    $referencenumber = $doc->createElement("referencenumber");
    $referencenumber->appendChild($doc->createCDATASection($post->ID));
    $b->appendChild($referencenumber);

    $url = $doc->createElement("url");
    $url->appendChild($doc->createCDATASection($post->guid));
    $b->appendChild($url);

    $description = $doc->createElement("description");
    $description = createCDATASection($post->post_content);
    $description->appendChild($description);
    $b->appendChild($description);

    //query postmeta table for city and state// then parse it into an array 
    $location = get_post_meta($post->ID,'geo_address',true);
    $location = explode(',',$location);

    $city = $doc->createElement("city");
    $city->appendChild($doc->createCDATASection($location[0]));
    $b->appendChild($city);

    $state = $doc->createElement("state");
    $state->appendChild($doc->createCDATASection($location[1]));
    $b->appendChild($state);

    $r->appendChild( $b );     

endwhile;  

I don't think this a problem with empty lines in my headers, because I have an identical script that runs perfect, only difference is the user data pulled from the database. And when I swap the queries out, the broken script runs no problem.

This error is driving me crazy. Big thanks for any help.

EDIT: Here is a sample of the intended output I was asked for

<source>
  <publisher>mydomain.com</publisher>
  <publisherurl>http://mydomain.com</publisherurl>
    <job>
        <company_name>ABC Company</company_name>
        <date>3-4-2011 10:54</date>
        <referencenumber>1234</referencenumber> 
        <url>http://www.mydomain.com/extension</url>
        <description>Block of of content, likely ranging several thousand characters. HTML tags included, but should be sanitized by use of createCDATASection() method</description>
        <city>Oakland</city>
        <state>California</state>
  </job>
</source>
$description = $doc->createElement("description");
$description = createCDATASection($post->post_content);
$description->appendChild($description);

???

Did you mean to append something to itself?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM