This question already has an answer here:
Whats the best xml parser to use especially for XML that's technically invalid format with the <?xml
tag missing?
I cant use simpleXML because that gives a error for being invalid. I know i can manually add the tags I need after i get the xml but id prefer to see what you guys think is the best method for parsing a xml into an array in php.
</div>
You can use SimpleXML
and disable errors by using libxml_use_internal_errors(false);
. In case SimpleXML simply won't use your string, I've personally used DOM
to parse/fix broken XML before.
On the other hand, why don't you simply add the <?xml
characters before reading it?
You can try this
<?php
//invalid xml file
$xmlfile = 'test.xml';
$xmlparser = xml_parser_create();
// open a file and read data
$fp = fopen($xmlfile, 'r');
$xmldata = fread($fp, 4096);
xml_parse_into_struct($xmlparser,$xmldata,$values);
xml_parser_free($xmlparser);
print_r($values);
?>