This is related to PHP Dom Document.
I have sample file like below
<?xml version="1.0" encoding="UTF-8"?>
<document>
<items>
<item class="pen">
<no>1</no>
<name>A</name>
<price>10</price>
</item>
<item class="book">
<no>2</no>
<name>B</name>
<price>20</price>
</item>
<item class="pen">
<no>2</no>
<name>C</name>
<price>30</price>
</item>
</items>
</document>
PHP code
$nodeCount = $oElement;
$limitCount = 1;
$nodeCount = $nodeCount->nextSibling;
while($nodeCount && !empty($nodeCount) && $nodeCount->nodeType == 1 && !preg_match("/pen/",$this->GetNodeClass($nodeCount)))
{
$limitCount++;
$nodeCount = $nodeCount->nextSibling;
}
Here, GetNodeClass()
checks for an attribute class
and this is the culprit point from where error occurs
function GetNodeClass($oElement)
{
//Error comes from below line when try to execute getAttribute() method on TextNode
$class = $oElement->getAttribute('class');
return $class;
}
When I traverse these elements, sometimes I receive errors like below
Fatal error: Call to undefined method DOMText::getAttribute() in
Then I have tried to solve it using below condition to prevent that error.
$node->nodeType == 1
In some cases it works properly but in some cases always it shows an error like explained. I knew that it comes because of some white space
between nodes. But is there any other way to ignore those white space at the time of XML file reading? Because I have so much code written already so if there is some pin point solution for that.
I use DomDocument object for all this.
Any help will be appreciated.