I'm using SimpleXML to load in some xml files (which I didn't write/provide and can't really change the format of).
Occasionally (eg one or two files out of every 50 or so) they don't escape any special characters (mostly &, but sometimes other random invalid things too). This creates and issue because SimpleXML with php just fails, and I don't really know of any good way to handle parsing invalid XML.
My first idea was to preprocess the XML as a string and put ALL fields in as CDATA so it would work, but for some ungodly reason the XML I need to process puts all of its data in the attribute fields. Thus I can't use the CDATA idea. An example of the XML being:
<Author v="By Someone & Someone" />
Whats the best way to process this to replace all the invalid characters from the XML before I load it in with SimpleXML?
i think workaroung for creating compute_position function will be make xml string flat before processing. Rewrite code posted by Josh:
I've added encoding stuff becose i've had problems with simply array[index] way of getting character from string.
This all should work but, dont know why, i've seen that $error->column gives me a different number than it should. Trying to debug this by simply add some invalid characters inside xml and check what value it would return, but no luck with it. Hope someone could tell me what is wrong with this approach.
What you need is something that will use libxml's internal errors to locate invalid characters and escape them accordingly. Here's a mockup of how I'd write it. Take a look at the result of
libxml_get_errors()
for error info.