3

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
Yeak
  • 2,470
  • 9
  • 45
  • 71
  • if it's not valid xml, then technically it's not really xml... so don't look at an xml parser to handle what isn't really xml. expecting a chicken to be your banana isn't really useful. – Marc B Apr 02 '13 at 18:13
  • ...most times i got such errors it was because of the "forbidden" XML characters, check here: http://stackoverflow.com/questions/730133/invalid-characters-in-xml –  Apr 02 '13 at 18:14
  • What do you mean by "technically invalid"? Not having an ` – Francis Avila Apr 02 '13 at 18:25
  • The document doesnt contain – Yeak Apr 02 '13 at 19:23

2 Answers2

1

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?

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • thats what i was thinking but wasnt sure if that would be the best way. Also i tried using DOM but couldnt get it to work – Yeak Apr 02 '13 at 19:22
0

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);
?>
Jayesh Ambali
  • 211
  • 6
  • 24