xml:19558: parser error : XML declaration allowed only at the start of the document
any solutions? i am using php XMLReader to parse a large XML file, but getting this error. i know the file is not well formatted but i think its not possible to go through the file and remove these extra declarations. so any idea, PLEASE HELP
If you have multiple XML declarations, you likely have a concatenation of many XML files, and also more than one root element. It's not clear how you would meaningfully parse them.
Try really hard to get the source of the XML to give you real XML first. If that doesn't work, see if you can do some preprocessing to fix the XML before you parse it.
Make sure there isn't any white space before the first tag. Try this:
Set the $file variable to the file you want. Note I don't know how well this will work for a 4gb file. Tell me if it doesn't.
EDIT: Here is another solution, it should work better with the larger file (parses as it is reading the file).
Another possible cause to this problem is unicode file head. If your XML's encoding is UTF-8, the file content will always start with these 3 bytes "EF BB BF". These bytes may be interpreted incorrectly if one attempts to convert from byte array to string. The solution is to write byte array to file directly without reading getString from the byte array.
ASCII has no file head Unicode: FF FE UTF-8: EF BB BF UTF-32: FF FE 00 00
Just open the file in ultraedit and you can see these bytes.