DOMDocument :: loadXML():期望开始标记:"<"

时间:2017-01-03 21:38:55

标签: php xml dom

我又遇到了一个问题,目前XML并不适合我...... 为什么我收到此错误消息以及如何解决?

我收到每个XML文件的错误消息!

错误讯息:

PHP Warning:  DOMDocument::loadXML(): Start tag expected, '<' not found in Entity, line: 1 in C:\Users\Jan\PhpstormProjects\censored\Matcher.php on line 36
Warning: DOMDocument::loadXML(): Start tag expected, '<' not found in Entity, line: 1 in C:\Users\Jan\PhpstormProjects\censored\Matcher.php on line 36

代码:

function loadTitlesIntoArray($tagName, $path){

$dom = new DOMDocument('1.0', 'utf-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($path);

$titels = array();
$marker = $dom->getElementsByTagName($tagName);

for ($i = $marker->length - 1; $i >= 0; $i--) {
    $new = $marker->item($i)->textContent;
    array_push($titels, $new);
}

print_r($titels);
}

loadTitlesIntoArray(&#39; title&#39;,$ kinguinPath);

XML:

<?xml version="1.0" encoding="UTF-8"?>
<rss>
  <channel xmlns:g="http://base.google.com/ns/1.0" version="2.0">
    <title><![CDATA[google_EUR_english_1]]></title>
    <link><![CDATA[http://cdn.kinguin.net/media/feedexport/google_EUR_english_1.xml]]></link>
    <item>
      <title><![CDATA[Anno 2070 Uplay CD Key]]></title>
      <link><![CDATA[http://www.kinguin.net/category/4/anno-2070/?nosalesbooster=1&country_store=1&currency=EUR]]></link>
      <g:price><![CDATA[3.27 EUR]]></g:price>
      <g:image_link><![CDATA[http://cdn.kinguin.net/media/catalog/category/anno_8.jpg]]></g:image_link>
    </item>
    <item>
      <title><![CDATA[Anno 2070: Deep Ocean DLC Uplay CD Key]]></title>
      <link><![CDATA[http://www.kinguin.net/category/5/anno-2070-deep-ocean-expansion-pack-dlc/?nosalesbooster=1&country_store=1&currency=EUR]]></link>
      <g:price><![CDATA[4.74 EUR]]></g:price>
      <g:image_link><![CDATA[http://cdn.kinguin.net/media/catalog/category/anno-2070-deep-ocean-releasing-this-spring-1089268_1.jpg]]></g:image_link>
    </item>
  </channel>
</rss>

问候,谢谢!

2 个答案:

答案 0 :(得分:11)

loadXML期待string of XML但你试图给它一个文件路径。试试load。或者,您可以将XML文件加载到字符串中,然后调用loadXML。

答案 1 :(得分:1)

某些RSS与PHP中的DOM使用的libxml不完全兼容。要解决此问题,您可以像这样扩展您的功能:

function loadTitlesIntoArray($tagName, $path){

  // load XML into simplexml
  $xml = simplexml_load_file( $path );

  // if the XML is valid
  if ( $xml instanceof SimpleXMLElement ) {

    $dom = new DOMDocument( '1.0', 'utf-8' );
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;

    // use it as a source
    $dom->loadXML( $xml->asXML() );

    $titels = array();
    $marker = $dom->getElementsByTagName( $tagName );

    for ( $i = $marker->length - 1; $i >= 0; $i-- ) {
      $new = $marker->item( $i )->textContent;
      array_push( $titels, $new );
    }

    print_r( $titels );
  }
}