Reputation: 8726
Please check this bellow program.
::Program::
<?php
$xml='
<books>
<book>
<name>Java complete reference</name>
<cost>256</cost>
</book>
<book>
<name>Head First PHP and Mysql</name>
<cost>389</cost>
</book>
</books>';
$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 <
to <
and >
to >
?
Upvotes: 1
Views: 23433
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 <
Why would you have the htmlentities in that string?
Upvotes: 3
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='
<books>
<book>
<name>Java complete reference</name>
<cost>256</cost>
</book>
<book>
<name>Head First PHP and Mysql</name>
<cost>389</cost>
</book>
</books>';
$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
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
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