i was trying to check the validity of a string as xml using this simplexml_load_string()
Docs function but it displays a lot of warning messages.
How can I check whether a string is a valid XML without suppressing (@
at the beginning) the error and displaying a warning function that expec
Case
Occasionally check availability of a Google Merchant XML feed.
The feed is without DTD, so
validate()
won't work.Solution
length
above contains a number of how many products are actually listed in the file. You can use your tag names instead.Logic
You can call
getElementsByTagName()
on any other tag names (item
I used is for Google Merchant, your case may vary), or read other properties on the$dom
object itself. The logic stays the same: instead of checking if there were errors when loading the file, I believe actually trying to manipulate it (or specifically check if it contains the values you actually need) would be more reliable.Most important: unlike
validate()
, this won't require your XML to have a DTD.From the documentation:
Reference:
libxml_use_internal_errors
My version like this:
Tests:
Here a small piece of class I wrote a while ago:
It works fine with streams and vfsStream as well for testing purposes.
try this one
Simple XML loading string