【问题标题】:PHP XMLReader - Reading not properly formated XML filesPHP XMLReader - 读取格式不正确的 XML 文件
【发布时间】:2018-07-17 22:06:40
【问题描述】:

这个问题与我在此处发布的另一篇帖子有关(针对reference)

我正在通过 FTP 从英国的一个开放数据铁路项目下载日志文件,每个日志文件大约 3Mb,并以这种方式呈现:

<?xml version="1.0" encoding="UTF-8"?><Pport xmlns="http://www.thalesgroup.com/rtti/PushPort/v12" ts="2018-02-05T21:33:59.8558288Z" version="12.0"><uR updateOrigin="Darwin"><deactivated rid="201802058015464"/></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-02-05T21:33:59.8558288Z" version="12.0"><uR updateOrigin="Darwin"><TS rid="201802058709918" ssd="2018-02-05" uid="W09918"><ns3:Location tpl="DARTFD" wta="07:36"><ns3:arr delayed="true" et="21:34" src="Darwin"/><ns3:plat cisPlatsup="true" platsup="true">2</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-02-05T21:33:59.8558288Z" version="12.0"><uR updateOrigin="Darwin"><TS rid="201802058771469" ssd="2018-02-05" uid="W71469"><ns3:Location tpl="WLWYCSD" wtd="13:16"><ns3:dep delayed="true" et="21:34" src="Darwin"/></ns3:Location><ns3:Location tpl="WLWYNGC" wtp="13:18"><ns3:pass delayed="true" et="21:36" src="Darwin"/><ns3:plat cisPlatsup="true" platsup="true">3</ns3:plat></ns3:Location><ns3:Location tpl="HATFILD" wtp="13:21:30"><ns3:pass delayed="true" et="21:39" src="Darwin"/><ns3:plat cisPlatsup="true" platsrc="A" platsup="true">1</ns3:plat></ns3:Location><ns3:Location tpl="POTRSBR" wtp="13:26"><ns3:pass delayed="true" et="21:44" src="Darwin"/><ns3:plat cisPlatsup="true" platsup="true">1</ns3:plat></ns3:Location><ns3:Location tpl="ALEXNDP" wtp="13:36:30"><ns3:pass delayed="true" et="21:51" src="Darwin"/><ns3:plat cisPlatsup="true" platsup="true">2</ns3:plat></ns3:Location><ns3:Location tpl="HRGYURV" wta="13:43" wtd="13:48"><ns3:arr delayed="true" et="21:57" src="Darwin"/><ns3:dep delayed="true" et="21:58" src="Darwin"/></ns3:Location><ns3:Location tpl="HRNSYMD" wta="13:50"><ns3:arr delayed="true" et="22:00" src="Darwin"/></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-02-05T21:34:52.2569006Z" version="12.0"><uR updateOrigin="Trust"><TS rid="201802056757064" ssd="2018-02-05" uid="C57064"><ns3:Location pta="21:34" ptd="21:34" tpl="DEVNPRT" wta="21:34" wtd="21:34:30"><ns3:arr at

我已使用此处给出的建议并尝试使用 XMLReader 实现 PHP 解决方案,但是 XML 日志文件的设置方式,XMLReader 出现错误。

这是我正在使用的基本代码:

$xmlReader->open($filename);

// While there is something to read continue reading
    while ($xmlReader->read()) { 

    // check to ensure nodeType is an Element not attribute or #Text  
        if ($xmlReader->nodeType == XMLReader::ELEMENT) {

            if ($xmlReader->hasAttributes) {
//Do something here
                }
            }
        }
    }

我认为的一个解决方案是因为日志文件中的每个条目都是单行,所以我认为我可以打开文件,读取并加载到 XMLReader 中,但我无法做到这一点,如下所示:

if ($filename = fopen("./pPortData.log", "r")) {
       while (!feof($filename)) {
            $xmlstr = fgets($filename);
            # do same stuff with the $line
            $address = new SimpleXMLElement($xmlstr) or die("Error: Cannot create object");
            echo $address->getName(), PHP_EOL;
            foreach($address as $name => $part) {
                echo "$name: $part" . "/n/r", PHP_EOL;
            }
        }    
        fclose($xmlstr);
    }

但没有快乐。所以...

1) 你知道实现这个的方法吗?

2) 或者你知道如何从文件中逐行加载到 XMLReader 中吗?

3) 如何修复 XML 文件?

谢谢

卢西奥

【问题讨论】:

    标签: php xml xmlreader


    【解决方案1】:

    您已经完成了最后的工作,一次加载每一行,然后使用 SimpleXML 处理它们应该没问题。

    我做了一些更改,我添加了一些错误捕获,它会拾取最后一条可能不完整的记录并仅显示一条消息。另一部分只是关于如何处理 XML 数据,所以目前我只是从加载的 XML 中输出数据。

    if ($file = fopen("./pPortData.log", "r")) {
        while (!feof($file)) {
            $xmlstr = fgets($file);
            libxml_use_internal_errors(true);
            try {
                $xml = new SimpleXMLElement($xmlstr);
                echo $xml->getName(), PHP_EOL;
                foreach($xml->children() as $part) {
                    echo $part->asXML() . PHP_EOL;
                }
            }
            catch ( Exception $e )  {
                echo "Last part unreadable.".PHP_EOL;
            }
        }
        fclose($file);
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    相关资源
    最近更新 更多