Is there a way to do a 'simple' check if a XML file has a valid syntax? I'm using PHP's XMLReader.
I'm in this situation: I have multiple XML files that change a lot. So I can't do a XMLReader::isValid() check with a DTD file. But this it not needed persé. I only want to know if the syntax is OK. Because sometimes a XML file is corrupted for example at the end. I would like to check this, before iterating over the nodes.
The other thing is that some files are over 2GB in size, so I can't do a simple DOM check without using heavy memory.
What should I do?
Of course I tried options like suggested in the comments and this works great, but only for small files:
$dom = new DOMDocument;
if(!@$dom->load('example.xml')){ die("syntax error"); }
Larger files eat up all the memory and crash.
When I open a large XML file in a simple XML reader program like "firstobject XML editor", it shows me the syntax error line within milliseconds (30GB xml file it takes 1.7 seconds to show the line with syntax error). Something like this should be possible for PHP XMLReader I guess?
Edit: For the moment I will use the option above, but do a filesize check first. If below a certain size (still testing what the max size is) the syntax is checked. For the bigger files I will build an option as @IMSoP suggested below with a third party tool and command line check. I will update this if I find a stable solution for this.
source https://stackoverflow.com/questions/70101956/php-xmlreader-check-large-xml-file-syntax
Comments
Post a Comment