1

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 >?

1
  • 3
    This begs the question why you have &lt;books&gt; instead of <books> in the first place? Commented Nov 25, 2011 at 14:00

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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?

Comments

2

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.

1 Comment

That part (XML Declaration) is optional.
2

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);
      ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.