admin管理员组文章数量:1277397
I have a data file from an external source which contains multiple XML snippets (up to 350). Each one starts with a bit of text and then some XML output like this :
2025-02-21 16:45:55,760 - Transaction RUN04-merchtranid1 - Success: <?xml version="1.0" encoding="UTF-8"?>
<payment_response>
<transaction_type>sale</transaction_type>
<status>approved</status>
<recurring_type>initial</recurring_type>
<unique_id>ccc01a6fb43b45cf38298fee067d1688</unique_id>
<transaction_id>RUN04-merchtranid1</transaction_id>
<mode>test</mode>
<timestamp>2025-02-21T14:45:55Z</timestamp>
<descriptor>UAT Gen Current UK</descriptor>
<amount>0</amount>
<currency>EUR</currency>
<sent_to_acquirer>true</sent_to_acquirer>
<scheme_transaction_identifier>485029514074150</scheme_transaction_identifier>
</payment_response>
2025-02-21 16:45:56,704 - Transaction RUN04-merchtranid2 - Success: <?xml version="1.0" encoding="UTF-8"?>
<payment_response>
<transaction_type>sale</transaction_type>
<status>approved</status>
<recurring_type>initial</recurring_type>
<unique_id>1f293c3166045f645b9ea4aeee755840</unique_id>
<transaction_id>RUN04-merchtranid2</transaction_id>
<mode>test</mode>
<timestamp>2025-02-21T14:45:56Z</timestamp>
<descriptor>UAT Gen Current UK</descriptor>
<amount>0</amount>
<currency>GBP</currency>
<sent_to_acquirer>true</sent_to_acquirer>
<scheme_transaction_identifier>MDHMKJSTW</scheme_transaction_identifier>
<scheme_settlement_date>0129</scheme_settlement_date>
</payment_response>
I need to roll through it with PHP and pick out the <unique_id> and matching <transaction_id> for each transaction. I can do this if it was a properly formatted XML document but this obviously isn't.
Would really appreciate any ideas ?
I have a data file from an external source which contains multiple XML snippets (up to 350). Each one starts with a bit of text and then some XML output like this :
2025-02-21 16:45:55,760 - Transaction RUN04-merchtranid1 - Success: <?xml version="1.0" encoding="UTF-8"?>
<payment_response>
<transaction_type>sale</transaction_type>
<status>approved</status>
<recurring_type>initial</recurring_type>
<unique_id>ccc01a6fb43b45cf38298fee067d1688</unique_id>
<transaction_id>RUN04-merchtranid1</transaction_id>
<mode>test</mode>
<timestamp>2025-02-21T14:45:55Z</timestamp>
<descriptor>UAT Gen Current UK</descriptor>
<amount>0</amount>
<currency>EUR</currency>
<sent_to_acquirer>true</sent_to_acquirer>
<scheme_transaction_identifier>485029514074150</scheme_transaction_identifier>
</payment_response>
2025-02-21 16:45:56,704 - Transaction RUN04-merchtranid2 - Success: <?xml version="1.0" encoding="UTF-8"?>
<payment_response>
<transaction_type>sale</transaction_type>
<status>approved</status>
<recurring_type>initial</recurring_type>
<unique_id>1f293c3166045f645b9ea4aeee755840</unique_id>
<transaction_id>RUN04-merchtranid2</transaction_id>
<mode>test</mode>
<timestamp>2025-02-21T14:45:56Z</timestamp>
<descriptor>UAT Gen Current UK</descriptor>
<amount>0</amount>
<currency>GBP</currency>
<sent_to_acquirer>true</sent_to_acquirer>
<scheme_transaction_identifier>MDHMKJSTW</scheme_transaction_identifier>
<scheme_settlement_date>0129</scheme_settlement_date>
</payment_response>
I need to roll through it with PHP and pick out the <unique_id> and matching <transaction_id> for each transaction. I can do this if it was a properly formatted XML document but this obviously isn't.
Would really appreciate any ideas ?
Share Improve this question edited Feb 24 at 8:34 Olivier 18.3k1 gold badge11 silver badges31 bronze badges asked Feb 24 at 5:43 Adrian SmithAdrian Smith 191 bronze badge 2 |1 Answer
Reset to default 0Given your input is in $input
variable, I made a simple script to parse it line-by-line. If it starts with a timestamp, start collecting a new document. Otherwise append to the current one.
$documents = [];
foreach (explode("\n", $input) as $line) {
preg_match('/(?P<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}) - (?P<message>[^<]+) (?P<xml>.*?)$/s', $line, $m);
if ($m) {
$documents[] = $m['xml'];
} else {
$documents[count($documents)-1] .= "\n".$line;
}
}
$transactions=[];
foreach ($documents as $document) {
$xml = simplexml_load_string($document);
$transactions[(string) $xml->unique_id] = (string) $xml->transaction_id;
}
var_dump($transactions);
This might require some tweaks if your input contains more distinct patterns.
本文标签: phpParsing XML snippets contained in a log fileStack Overflow
版权声明:本文标题:php - Parsing XML snippets contained in a log file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741291856a2370597.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
preg_split
to split on that? 3v4l./nVdGI (Status at the end might not always beSuccess
, I'm guessing? Then either list alternatives, or use something more generic in that part.) – C3roe Commented Feb 24 at 7:18