Hearaman
Hearaman

Reputation: 8726

PHP Error: DOMDocument::loadXML() [domdocument.loadxml]: Start tag expected, '<' not found in Entity

Please check this bellow program.

::Program::

      <?php
      $xml='
      &lt;books&gt;
           &lt;book&gt;
                &lt;name&gt;Java complete reference&lt;/name&gt;
                &lt;cost&gt;256&lt;/cost&gt;
           &lt;/book&gt;

           &lt;book&gt;
                &lt;name&gt;Head First PHP and Mysql&lt;/name&gt;
                &lt;cost&gt;389&lt;/cost&gt;
           &lt;/book&gt;
      &lt;/books&gt;';


      $dom=new DOMDocument();
      $dom->loadXML($xml);
      foreach ($dom->getElementsByTagName('book') as $book)
      {
              foreach($book->getElementsByTagName('name') as $name)
              {
                   $names[]=$name->nodeValue;
              }

              foreach($book->getElementsByTagName('cost') as $cost)
              {
                   $costs[]=$cost->nodeValue;
              }
      }
      print_r($names);
      ?>

It is shows error:

DOMDocument::loadXML() [domdocument.loadxml]: Start tag expected, '<' not found in Entity

Is this correct way to do this?

If it is correct, Is there any way to get the proper result without changing this &lt; to < and &gt; to >?

Upvotes: 1

Views: 23433

Answers (4)

PeeHaa
PeeHaa

Reputation: 72682

DOMDocument expects the string to be VALID xml.

Your string isn't a valid XML string. You should just use < in stead of &lt;

Why would you have the htmlentities in that string?

Upvotes: 3

Hearaman
Hearaman

Reputation: 8726

Thank you all. Just now i have tried with the method "html_entity_decode()". It is worked for me for this example.

::Code::

      <?php
      $xml='
      &lt;books&gt;
           &lt;book&gt;
                &lt;name&gt;Java complete reference&lt;/name&gt;
                &lt;cost&gt;256&lt;/cost&gt;
           &lt;/book&gt;

           &lt;book&gt;
                &lt;name&gt;Head First PHP and Mysql&lt;/name&gt;
                &lt;cost&gt;389&lt;/cost&gt;
           &lt;/book&gt;
      &lt;/books&gt;';


      $xml=html_entity_decode($xml);




      $dom=new DOMDocument();
      $dom->loadXML($xml);
      foreach ($dom->getElementsByTagName('book') as $book)
      {
              foreach($book->getElementsByTagName('name') as $name)
              {
                   $names[]=$name->nodeValue;
              }

              foreach($book->getElementsByTagName('cost') as $cost)
              {
                   $costs[]=$cost->nodeValue;
              }
      }
      print_r($names);
      ?>

Upvotes: 2

Trott
Trott

Reputation: 70115

You should not be using character entities for < and > on things that are actually XML tags in the string that represents your XML. It should be this:

$xml='
  <books>
       <book>
...

Do that and the warning goes away.

You only need to use character entities for < and > when they are part of the actual data rather than delimiting an XML tag.

Upvotes: 3

Fredy31
Fredy31

Reputation: 2835

Aren't you supposed to start with something like this

<?xml version="1.0" encoding="UTF-8" ?>

to create valid XML? That might be the missing start tag your error is talking about.

Upvotes: 2

Related Questions