How to fix XML “Opening and ending tag mismatch error”?

馋奶兔 提交于 2021-01-27 04:10:50

问题


I'm trying to fix an XML file with thousands of lines that have the error:

Opening and ending tag mismatch error

I'm using right now simpleXML to parse this file, so before parse with this librarie I need to fix the XML file:

Right now I'm trying with this solution but it's not enough:

libxml_use_internal_errors(true);
$xml = @simplexml_load_file($temp_name);
     $errors = libxml_get_errors();
     foreach ($errors as $error) {
         if (strpos($error->message, 'Opening and ending tag mismatch')!==false) {
             $tag   = trim(preg_replace('/Opening and ending tag mismatch: (.*) line.*/', '$1', $error->message));
             $lines = file($temp_name, FILE_IGNORE_NEW_LINES);
             $line  = $error->line+1;
             echo $line;
             echo "<br>";
             $lines[$line] = '</'.$tag.'>'.$lines[$line];
             file_put_contents($temp_name, implode("\n", $lines));
         }
     }

Any idea?


回答1:


First, if you've got corrupt data then fixing the program that generated it is usually more important than repairing the data.

If the only errors in the file are mismatched end tags, then presumably the repair strategy is to ignore what's in the end tag entirely, given that the name appearing in an XML end tag is redundant. You might find that an existing tool such as TagSoup or validator.nu handles this the way you want; or you might find that such a tool outputs XML which can be transformed into the form you want. That's a better prospect than writing your own parser for this non-XML grammar.




回答2:


I think this is simple solution.

Please check on your ending tag.

For example this should be correct.

$xml.="</childelement>";

Instead of

$xml.="<childelement/>";


来源:https://stackoverflow.com/questions/25493728/how-to-fix-xml-opening-and-ending-tag-mismatch-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!