【问题标题】:XML & PHP issueXML & PHP 问题
【发布时间】:2018-01-06 13:23:13
【问题描述】:

我正在尝试回显一个 4MB 的 XML 文件,但该文件无效:

我的 PHP

<?php

$str = file_get_contents('log.xml');
$xml = simplexml_load_string($str);
$c = 0;
foreach($xml->uR->TS->Location as $loc) 
{
    echo (++$c) . "\n";
    echo (string)$loc->attributes()['tpl'] . "\n";
    echo (string)$loc->arr->attributes()['at'] . "\n";
    echo (string)$loc->dep->attributes()['et'] . "\n\n";
    echo (string)$loc->plat . "\n\n\n";
}
?>

它与示例 xml 文件完美配合

<?xml version="1.0" encoding="UTF-8"?>
<Pport xmlns="http://www.thalesgroup.com/rtti/PushPort/v12" xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" ts="2018-01-01T21:58:48.2213864Z" version="12.0">
    <uR updateOrigin="Trust">
        <TS>
            <Location pta="21:59" ptd="21:59" tpl="ROBY" wta="21:59" wtd="21:59:30">
                <arr at="21:59" src="TRUST" srcInst="Auto" />
                <dep et="21:59" src="Darwin" />
                <plat conf="true" platsrc="A">4</plat>
            </Location>
            <Location pta="22:06" ptd="22:06" tpl="PRESCOT" wta="22:05:30" wtd="22:06">
                <arr et="22:06" src="Darwin" wet="22:05" />
                <dep et="22:06" src="Darwin" />
                <plat>1</plat>
            </Location>
        </TS>
    </uR>
</Pport>

但添加原始XML文件后,文件本身有多个标题,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Pport xmlns="http://www.thalesgroup.com/rtti/PushPort/v12" xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" ts="2018-01-03T01:31:28.3036616Z" version="12.0">
    <uR requestID="AM02050384" requestSource="AM02" updateOrigin="CIS">
        <TS rid="201801037171519" ssd="2018-01-03" uid="G71519">
            <ns3:Location tpl="GLYNDE" wtp="01:25:08">
                <ns3:pass at="01:31" src="TD" />
                <ns3:plat conf="true" platsrc="A" platsup="true">2</ns3:plat>
                <ns3:length>8</ns3:length>
            </ns3:Location>
        </TS>
    </uR>
</Pport>
<?xml version="1.0" encoding="UTF-8"?>
<Pport xmlns="http://www.thalesgroup.com/rtti/PushPort/v12" xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" ts="2018-01-03T01:31:29.1772672Z" version="12.0">
    <uR requestID="0000000000046386" requestSource="at21" updateOrigin="CIS">
        <TS rid="201801038706030" ssd="2018-01-03" uid="W06030">
            <ns3:Location pta="01:25" ptd="01:26" tpl="DARTFD" wta="01:25" wtd="01:26">
                <ns3:arr at="01:31" src="TD" />
                <ns3:dep et="01:32" etmin="01:27" src="Darwin" />
                <ns3:plat conf="true" platsrc="A">4</ns3:plat>
            </ns3:Location>
        </TS>
    </uR>
</Pport>
<?xml version="1.0" encoding="UTF-8"?>
<Pport xmlns="http://www.thalesgroup.com/rtti/PushPort/v12" xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" ts="2018-01-03T01:31:30.1912737Z" version="12.0">
    <uR updateOrigin="TD">
        <TS rid="201801027160109" ssd="2018-01-02" uid="G60109">
            <ns3:Location tpl="BRINKLW" wtp="01:34:30">
                <ns3:pass at="01:31" src="TD" /></ns3:Location>
        </TS>
    </uR>
</Pport>
<?xml version="1.0" encoding="UTF-8"?>
<Pport xmlns="http://www.thalesgroup.com/rtti/PushPort/v12" xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" ts="2018-01-03T01:31:31.2052802Z" version="12.0">
    <uR updateOrigin="TD">
        <TS rid="201801036763188" ssd="2018-01-03" uid="C63188">
            <ns3:Location tpl="AMBERGJ" wtp="02:04:30">
                <ns3:pass et="01:38" src="TD" /></ns3:Location>
        </TS>
    </uR>
</Pport>

有没有办法:

  1. 忽略多个标头以避免错误。
  2. 回显里面的所有数据。
  3. 将回显值发布到 Bootstrap 表。

干杯!

【问题讨论】:

  • 用任何内容替换额外的标题,添加根元素 '' .$s 。 '' 并将一个标题移动到字符串的开头
  • 如果这是其他人提供的文件,您能否要求他们提供正确的 XML,而不是在一个文件中提供一堆 XML 数据。如果它是一次性文件,我建议手动对其进行编辑以使其有效并从那里开始工作。

标签: php xml xslt bootstrap-4


【解决方案1】:

你有多种方法。

首先您始终可以直接输出文件 - 无需将其加载为 XML 文档

$handle = fopen('log.xml'), "r") or die("Couldn't get handle");
if ($handle) {
    while (!feof($handle)) {
        echo fgets($handle, 4096);
    }
    fclose($handle);
}

第二您可以修改原始文件,以便简单的 XML 可以处理它 - 这是 @splash58 建议的方法:

用任何内容替换多余的标题,添加根元素 '' .$s 。 '' 并将一个标题移动到字符串的开头

为此,您可以使用 preg_replace 和其他 str_replace 函数,但从长远来看,如果发生任何新的不良情况可能会有些乏味,您必须添加新的替换语句才能使代码再次工作。

第三,首先获得格式良好的 XML - 如果您尝试解析的 XML 来自另一个服务,那么该服务可能首先提供一些格式良好的 XML!问问他们吧。

第四种使用容错标记解析器 - 参见 DOMDocument::$recoverlibxml_use_internal_errors(true) (example) 如这里的答案所示:How to parse invalid (bad / not well-formed) XML?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 2023-03-26
    • 2012-01-12
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多