php 如何修复 XML“开始和结束标记不匹配错误”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25493728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:51:29  来源:igfitidea点击:

How to fix XML "Opening and ending tag mismatch error"?

phpxmlxml-parsing

提问by rguzmanrosales

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

我正在尝试修复一个包含数千行错误的 XML 文件:

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:

我现在正在使用 simpleXML 来解析这个文件,所以在用这个库解析之前,我需要修复 XML 文件:

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.*/', '', $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?

任何的想法?

回答by Michael Kay

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.

如果文件中唯一的错误是不匹配的结束标记,那么修复策略可能是完全忽略结束标记中的内容,因为出现在 XML 结束标记中的名称是多余的。您可能会发现现有的工具(例如 TagSoup 或 validator.nu)会以您想要的方式处理此问题;或者您可能会发现这样的工具输出的 XML 可以转换为您想要的形式。这比为这种非 XML 语法编写自己的解析器更好。

回答by Faris Rayhan

I think this is simple solution.

我认为这是一个简单的解决方案。

Please check on your ending tag.

请检查您的结束标记。

For example this should be correct.

例如,这应该是正确的。

$xml.="</childelement>";

Instead of

代替

$xml.="<childelement/>";