Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOMDocument::loadXML(): Start tag expected: "<"

Tags:

dom

php

xml

I have again a problem and at the moment XML isn't nice to me... Why I'm getting this error message and how can I solve it?

I get the error message by every XML file!

Error Message:

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

Code:

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('title', $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>

Greetings and Thank You!

like image 319
Jan Avatar asked Jan 03 '17 21:01

Jan


2 Answers

loadXML is expecting a string of XML but you are trying to give it a file path. Try load. Or, you can load the XML file into a string and then call loadXML.

like image 181
Josh Maag Avatar answered Sep 26 '22 08:09

Josh Maag


Some RSS are not fully compatible with the libxml which is used by DOM in PHP. To solve this issue, you can extend your function like this:

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 );
  }
}
like image 32
Lukas Avatar answered Sep 22 '22 08:09

Lukas