0

我有代码来检索跺脚消息,这很有效。然后我想从 stomp 消息中获取 xml 来做一些事情,我有代码并且它可以工作。

挑战在于从消息中去除杂乱无章的内容并仅获取 xml。

这是 stomp 消息的示例(是的,数据是相同的,但在这里不相关):

MESSAGE
_HQ_ORIG_ADDRESS:jms.queue.edu
timestamp:1339716293764
redelivered:false
_HQ_ORIG_MESSAGE_ID:xxxxxxxx
expires:0
subscription:subscription/jms.queue.edu
priority:4
message-id:xxxxxxxxxx
destination:jms.queue.edu

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><create><sourceMessageId>4454</sourceMessageId><messageId>3038</messageId><course> <batchUid>ASIA.355.921.2012S1.6733</batchUid><title>ASIA355-921-Chinese Cinema</title><startDate>2012-06-18-07:00</startDate><endDate>2012-09- 21-07:00</endDate><mappedNodeBatchUid>9c0bc373-23a0-4e60-b201- efbbc9bb022e</mappedNodeBatchUid><available>false</available></course></create>
MESSAGE
_HQ_ORIG_ADDRESS:jms.queue.edu
timestamp:1339716293764
redelivered:false
_HQ_ORIG_MESSAGE_ID:xxxxxxxx
expires:0
subscription:subscription/jms.queue.edu
priority:4
message-id:xxxxxxxxxx
destination:jms.queue.edu

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><create><sourceMessageId>4454</sourceMessageId><messageId>3038</messageId><course> <batchUid>ASIA.355.921.2012S1.6733</batchUid><title>ASIA355-921-Chinese Cinema</title><startDate>2012-06-18-07:00</startDate><endDate>2012-09- 21-07:00</endDate><mappedNodeBatchUid>9c0bc373-23a0-4e60-b201- efbbc9bb022e</mappedNodeBatchUid><available>false</available></course></create>

我想要做的是删除消息中从“MESSAGE”开始到并包括以xml开头的每一行之前的换行符的所有行。这将为我提供使用 xml 解析器解析所需的结果:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><create><sourceMessageId>4454</sourceMessageId><messageId>3038</messageId><course> <batchUid>ASIA.355.921.2012S1.6733</batchUid><title>ASIA355-921-Chinese Cinema</title><startDate>2012-06-18-07:00</startDate><endDate>2012-09- 21-07:00</endDate><mappedNodeBatchUid>9c0bc373-23a0-4e60-b201- efbbc9bb022e</mappedNodeBatchUid><available>false</available></course></create>
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><create><sourceMessageId>4454</sourceMessageId><messageId>3038</messageId><course> <batchUid>ASIA.355.921.2012S1.6733</batchUid><title>ASIA355-921-Chinese Cinema</title><startDate>2012-06-18-07:00</startDate><endDate>2012-09- 21-07:00</endDate><mappedNodeBatchUid>9c0bc373-23a0-4e60-b201- efbbc9bb022e</mappedNodeBatchUid><available>false</available></course></create>

我试过了:

$xmlstr = preg_replace("/MESSAGE(.*)jms.queue.edu$/ims",'',$msg);
$xmlstr = trim($xmlstr);

但这会删除第一行第一次出现“MESSAGE”和最后一次出现 xml 之间的所有内容。换句话说,第一个“MESSAGE”和最后一个“xml”之间的所有行都被删除。

有任何想法吗?我尝试过使用各种技巧,包括:正则表达式、内爆/爆炸、写入/读取文件等。但我觉得上面的 preg_replace 代码有效,它只需要能够识别所有事件。我知道这将涉及“while”或“foreach”循环,但我期待一个好的、干净的解决方案。非常感谢任何帮助。

4

1 回答 1

1

?后使用*

或者,试试这个:

list(,$body) = explode("\r\n\r\n",$msg); // adjust line ending as needed
list($xmlstr) = explode("\r\n",$body);

这将获得包含所有 XML 的行。

于 2012-06-21T16:34:32.260 回答