2012-02-10 99 views
0

我有一個腳本生成突然停止工作的XML文檔。當我運行它,我得到這個錯誤:XML分析錯誤...找不到元素

XML解析錯誤:沒有的元素中找到 地點:http://www.mydomain.com/indeed-general-xml/ 1號線,1列:

下面是生成節點結構的代碼和循環:

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; 

我不認爲這是我的標題中的空行問題,因爲我有一個完全相同的腳本,唯一不同的是從數據庫中提取的用戶數據。當我將查詢交換出來時,破損的腳本沒有任何問題。

這個錯誤讓我瘋狂。非常感謝您的幫助。

編輯:這裏是預期的輸出的一個示例我被要求爲

<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> 
+2

XML文檔是什麼樣的? – 2012-02-10 18:19:56

+0

像什麼?我想你錯過了一些東西:) – 2012-02-10 19:27:28

+0

是的,我試圖編輯我的評論,但它超時。對不起,這裏的註釋格式不利於打印出結構良好的XML文檔。儘管通過查看代碼,您應該可以看到,,<company>,<etc..>的樹結構。您無法從代碼中查看的唯一一件事是正確打開和關閉的根節點<source>。 – <span class="text-secondary"> <small> <span>2012-02-10 19:34:37</span> </small> </span> </p> </div> </div> </div> </div> </div> </article> </div> <div class="answer-title"> <span class="text-logo margin-top-sm">A</span> <h2 class="title h4">回答</h2> </div> <div class="item-description text-md markdown-body margin-bottom-40 voidso"> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">0<i class="fa fa-thumbs-up"></i></span> </div> <div class="post-offset"> <div class="answer fmt"> <pre><code class="prettyprint-override">$description = $doc->createElement("description"); $description = createCDATASection($post->post_content); $description->appendChild($description); </code></pre> <p>???</p> <p>你是不是要追加東西給自己?</p> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/9235132">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2012-02-10 21:35:06</span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> <!-- comments --> <div class="comments"> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">所有內容都會附加到我的根標記<source>。我將編輯代碼以顯示我如何使用DOMDocument類來完成此操作。至於你詢問的<description>節點,代碼中的以下行是$ b-> appendChild($ description),因爲$ b是$ description的父節點。 – <span class="text-secondary"> <small> <span>2012-02-10 21:55:54</span> </small> </span> </p> </div> </div> </div> </div> </div> </article> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1038284119" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="clearfix"> </div> <div class="relative-box"> <div class="relative">相關問題</div> <ul class="relative_list"> <li> 1. <a href="http://hk.uwenku.com/question/p-pyadvyri-bmh.html" target="_blank" title="XML分析錯誤:找不到元素"> XML分析錯誤:找不到元素 </a> </li> <li> 2. <a href="http://hk.uwenku.com/question/p-qxecbzou-sz.html" target="_blank" title="XML解析錯誤:找不到元素?"> XML解析錯誤:找不到元素? </a> </li> <li> 3. <a href="http://hk.uwenku.com/question/p-yvoebzxg-bnc.html" target="_blank" title="什麼可能導致XML分析錯誤:找不到元素?"> 什麼可能導致XML分析錯誤:找不到元素? </a> </li> <li> 4. <a href="http://hk.uwenku.com/question/p-ncevupcv-nm.html" target="_blank" title="是什麼導致XML分析錯誤:找不到根元素錯誤?"> 是什麼導致XML分析錯誤:找不到根元素錯誤? </a> </li> <li> 5. <a href="http://hk.uwenku.com/question/p-bqdbgbdg-ta.html" target="_blank" title="XML解析錯誤:找不到根元素"> XML解析錯誤:找不到根元素 </a> </li> <li> 6. <a href="http://hk.uwenku.com/question/p-frcslykl-bkt.html" target="_blank" title="XML解析器錯誤:找不到元素"> XML解析器錯誤:找不到元素 </a> </li> <li> 7. <a href="http://hk.uwenku.com/question/p-kqwftfqg-mu.html" target="_blank" title="錯誤:(28)解析XML時出錯:沒有找到元素"> 錯誤:(28)解析XML時出錯:沒有找到元素 </a> </li> <li> 8. <a href="http://hk.uwenku.com/question/p-knphnoaf-bek.html" target="_blank" title="ajax響應錯誤(XML解析錯誤:找不到元素位置:moz-nullprincipal)"> ajax響應錯誤(XML解析錯誤:找不到元素位置:moz-nullprincipal) </a> </li> <li> 9. <a href="http://hk.uwenku.com/question/p-emcorsbr-ub.html" target="_blank" title="XML解析錯誤:沒有的元素中找到"> XML解析錯誤:沒有的元素中找到 </a> </li> <li> 10. <a href="http://hk.uwenku.com/question/p-nhsliurh-hs.html" target="_blank" title="XML解析錯誤:沒有根元素中找到位置"> XML解析錯誤:沒有根元素中找到位置 </a> </li> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-6208739752673518" data-ad-slot="4606349252"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <li> 11. <a href="http://hk.uwenku.com/question/p-xddsufqs-du.html" target="_blank" title="XML解析錯誤:找不到元素位置:moz-nullprincipal(與ajax不兼容)"> XML解析錯誤:找不到元素位置:moz-nullprincipal(與ajax不兼容) </a> </li> <li> 12. <a href="http://hk.uwenku.com/question/p-hrtvaozl-ww.html" target="_blank" title="解析XML時出錯:找不到元素"> 解析XML時出錯:找不到元素 </a> </li> <li> 13. <a href="http://hk.uwenku.com/question/p-zridscbo-bdw.html" target="_blank" title="ASP.NET XML解析錯誤:沒有找到元素1號線,第1欄錯誤"> ASP.NET XML解析錯誤:沒有找到元素1號線,第1欄錯誤 </a> </li> <li> 14. <a href="http://hk.uwenku.com/question/p-unhyuwur-mm.html" target="_blank" title="XML解析錯誤:在rails中使用rjs時找不到元素"> XML解析錯誤:在rails中使用rjs時找不到元素 </a> </li> <li> 15. <a href="http://hk.uwenku.com/question/p-tafjrxam-ts.html" target="_blank" title="XML解析錯誤:找不到根元素在控制檯中的位置FF"> XML解析錯誤:找不到根元素在控制檯中的位置FF </a> </li> <li> 16. <a href="http://hk.uwenku.com/question/p-dvomfugt-bma.html" target="_blank" title="我無法弄清楚這個錯誤解析XML:找不到元素"> 我無法弄清楚這個錯誤解析XML:找不到元素 </a> </li> <li> 17. <a href="http://hk.uwenku.com/question/p-ufnmmfrp-zx.html" target="_blank" title="XML HTTP響應問題:XML解析錯誤:未找到元素位置:moz-nullprincipal"> XML HTTP響應問題:XML解析錯誤:未找到元素位置:moz-nullprincipal </a> </li> <li> 18. <a href="http://hk.uwenku.com/question/p-ohluydwl-bbv.html" target="_blank" title="cakephp元素錯誤(元素未找到)"> cakephp元素錯誤(元素未找到) </a> </li> <li> 19. <a href="http://hk.uwenku.com/question/p-vdarsvxq-bg.html" target="_blank" title="錯誤元素// .....未找到"> 錯誤元素// .....未找到 </a> </li> <li> 20. <a href="http://hk.uwenku.com/question/p-uprkaazc-ug.html" target="_blank" title="XML錯誤aws-mturk-clt-1.3.1 - 找不到元素聲明"> XML錯誤aws-mturk-clt-1.3.1 - 找不到元素聲明 </a> </li> <li> 21. <a href="http://hk.uwenku.com/question/p-tmtnocqa-bks.html" target="_blank" title="Selenium IDE 1.5.0「元素...找不到」錯誤"> Selenium IDE 1.5.0「元素...找不到」錯誤 </a> </li> <li> 22. <a href="http://hk.uwenku.com/question/p-hzvrmbpz-bdp.html" target="_blank" title="Firefox錯誤'找不到元素'"> Firefox錯誤'找不到元素' </a> </li> <li> 23. <a href="http://hk.uwenku.com/question/p-vjznyfbb-bep.html" target="_blank" title="XSD錯誤:找不到元素"> XSD錯誤:找不到元素 </a> </li> <li> 24. <a href="http://hk.uwenku.com/question/p-nmkqkuku-wx.html" target="_blank" title="svn「找不到元素」錯誤"> svn「找不到元素」錯誤 </a> </li> <li> 25. <a href="http://hk.uwenku.com/question/p-urvdchhw-bah.html" target="_blank" title="解析XML時出錯:未找到元素"> 解析XML時出錯:未找到元素 </a> </li> <li> 26. <a href="http://hk.uwenku.com/question/p-nknumrfr-bev.html" target="_blank" title="解析xml元素到類"> 解析xml元素到類 </a> </li> <li> 27. <a href="http://hk.uwenku.com/question/p-zoouysea-wm.html" target="_blank" title="XML解析錯誤:沒有根元素中找到位置(PHP +阿賈克斯)"> XML解析錯誤:沒有根元素中找到位置(PHP +阿賈克斯) </a> </li> <li> 28. <a href="http://hk.uwenku.com/question/p-rokqhzlm-oh.html" target="_blank" title="Ajax響應中的「XML解析錯誤:未找到元素位置:moz-nullprincipal」"> Ajax響應中的「XML解析錯誤:未找到元素位置:moz-nullprincipal」 </a> </li> <li> 29. <a href="http://hk.uwenku.com/question/p-dvnwtwdg-bam.html" target="_blank" title="在返回404結果:XML解析錯誤:沒有的元素中找到"> 在返回404結果:XML解析錯誤:沒有的元素中找到 </a> </li> <li> 30. <a href="http://hk.uwenku.com/question/p-feeyvjyf-vk.html" target="_blank" title="Jenkins JobConfigHistory插件 - XML解析錯誤:未找到元素 - 未顯示Diffs"> Jenkins JobConfigHistory插件 - XML解析錯誤:未找到元素 - 未顯示Diffs </a> </li> </ul> </div> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1575177025"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="padding-top-10"></div> </div> </div> <script type="text/javascript" src="http://img.uwenku.com/uwenku/script/side.js?t=1644592048261"></script> <script type="text/javascript" src="http://img.uwenku.com/uwenku/plugin/highlight/highlight.pack.js"></script> <link href="http://img.uwenku.com/uwenku/plugin/highlight/styles/docco.css" media="screen" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $('pre').each(function(i, e) { hljs.highlightBlock(e, "<span class='indent'> </span>", false) }); </script> <div class="col-lg-3 col-md-4 col-sm-5"> <div id="rightTop"> <div class="row"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="5415218910" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="row sidebar panel panel-default"> <div class="panel-heading font-bold"> 最新問題 </div> <div class="m-b-sm m-t-sm clearfix"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-csmlyttv-tb.html" target="_blank" title="在角度工人中導入模塊?"> 在角度工人中導入模塊? </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-qrtnwcvg-tn.html" target="_blank" title="Python布爾方法命名約定"> Python布爾方法命名約定 </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-qvpcimgh-py.html" target="_blank" title="jquery background-image"> jquery background-image </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-eyghaugq-qh.html" target="_blank" title="從s3檢索音頻時出錯"> 從s3檢索音頻時出錯 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-szhcyset-qt.html" target="_blank" title="如何將QR碼自動添加到圖像"> 如何將QR碼自動添加到圖像 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-oscplsex-rc.html" target="_blank" title="16個任務(1048.5 MB)的序列化結果的總大小大於spark.driver.maxResultSize(1024.0 MB)"> 16個任務(1048.5 MB)的序列化結果的總大小大於spark.driver.maxResultSize(1024.0 MB) </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-sfvehlqe-rx.html" target="_blank" title="打字稿,使用類沒有構造"> 打字稿,使用類沒有構造 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-khjthsaj-pd.html" target="_blank" title="PHP通過除去空鍵"> PHP通過除去空鍵 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-nihddvmm-ne.html" target="_blank" title="Jest TypeError:無法讀取未定義的'商店'"> Jest TypeError:無法讀取未定義的'商店' </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-fsfdvsjn-mm.html" target="_blank" title="在C++中有沒有一種慣用的方法來防止運行一組操作導致集合發生變化的情況?"> 在C++中有沒有一種慣用的方法來防止運行一組操作導致集合發生變化的情況? </a> </li> </ul> </div> </div> </div> <p class="article-nav-bar"></p> <div class="row sidebar article-nav"> <div class="row box_white visible-sm visible-md visible-lg margin-zero"> <div class="top"> <h3 class="title"><i class="glyphicon glyphicon-th-list"></i> 相關問題</h3> </div> <div class="article-relative-content"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-pyadvyri-bmh.html" target="_blank" title="XML分析錯誤:找不到元素"> XML分析錯誤:找不到元素 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-qxecbzou-sz.html" target="_blank" title="XML解析錯誤:找不到元素?"> XML解析錯誤:找不到元素? </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-yvoebzxg-bnc.html" target="_blank" title="什麼可能導致XML分析錯誤:找不到元素?"> 什麼可能導致XML分析錯誤:找不到元素? </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-ncevupcv-nm.html" target="_blank" title="是什麼導致XML分析錯誤:找不到根元素錯誤?"> 是什麼導致XML分析錯誤:找不到根元素錯誤? </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-bqdbgbdg-ta.html" target="_blank" title="XML解析錯誤:找不到根元素"> XML解析錯誤:找不到根元素 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-frcslykl-bkt.html" target="_blank" title="XML解析器錯誤:找不到元素"> XML解析器錯誤:找不到元素 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-kqwftfqg-mu.html" target="_blank" title="錯誤:(28)解析XML時出錯:沒有找到元素"> 錯誤:(28)解析XML時出錯:沒有找到元素 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-knphnoaf-bek.html" target="_blank" title="ajax響應錯誤(XML解析錯誤:找不到元素位置:moz-nullprincipal)"> ajax響應錯誤(XML解析錯誤:找不到元素位置:moz-nullprincipal) </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-emcorsbr-ub.html" target="_blank" title="XML解析錯誤:沒有的元素中找到"> XML解析錯誤:沒有的元素中找到 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-nhsliurh-hs.html" target="_blank" title="XML解析錯誤:沒有根元素中找到位置"> XML解析錯誤:沒有根元素中找到位置 </a> </li> </ul> </div> </div> </div> </div> </div> </div> </div><!-- wrap end--> <!-- footer --> <footer id="footer"> <div class="bg-simple lt"> <div class="container"> <div class="row padder-v m-t"> <div class="col-xs-8"> <ul class="list-inline"> <li><a href="http://hk.uwenku.com/contact">聯系我們</a></li> <li>© 2020 HK.UWENKU.COM</li> <li><a target="_blank" href="https://beian.miit.gov.cn/">沪ICP备13005482号-4</a></li> <li><script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1280101193&web_id=1280101193"></script></li> <li><a href="http://www.uwenku.com/" target="_blank" title="优文库">简体中文</a></li> <li><a href="http://hk.uwenku.com/" target="_blank" title="優文庫">繁體中文</a></li> <li><a href="http://ru.uwenku.com/" target="_blank" title="поле вопросов и ответов">Русский</a></li> <li><a href="http://de.uwenku.com/" target="_blank" title="Frage - und - antwort - Park">Deutsch</a></li> <li><a href="http://es.uwenku.com/" target="_blank" title="Preguntas y respuestas">Español</a></li> <li><a href="http://hi.uwenku.com/" target="_blank" title="कार्यक्रम प्रश्न और उत्तर पार्क">हिन्दी</a></li> <li><a href="http://it.uwenku.com/" target="_blank" title="IL Programma di chiedere Park">Italiano</a></li> <li><a href="http://ja.uwenku.com/" target="_blank" title="プログラム問答園区">日本語</a></li> <li><a href="http://ko.uwenku.com/" target="_blank" title="프로그램 문답 단지">한국어</a></li> <li><a href="http://pl.uwenku.com/" target="_blank" title="program o park">Polski</a></li> <li><a href="http://tr.uwenku.com/" target="_blank" title="Program soru ve cevap parkı">Türkçe</a></li> <li><a href="http://vi.uwenku.com/" target="_blank" title="Đáp ứng viên">Tiếng Việt</a></li> <li><a href="http://fr.uwenku.com/" target="_blank" title="Programme interrogation Park">Française</a></li> </ul> </div> </div> </div> </div> </div> </footer> <!-- / footer --> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f78a970f17b19a79fc477a3378096f29"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>