【问题标题】:Parsing response from a string in python从python中的字符串解析响应
【发布时间】:2023-03-27 20:54:01
【问题描述】:

我收到来自 JMS 队列的以下响应,我希望能够解析它以获取 error_code 和 error_description 的值。我打算使用 python 来解析这个字符串。

    TextMessage={ Header={ JMSMessageID={ID:emsfuat-p.111111111111:2} JMSDestination={Queue[abc.def.ghijk.lmno.pqrs.tuv.wxyz]} JMSReplyTo={null} JMSDeliveryMode={PERSISTENT} JMSRedelivered={false} JMSCorrelationID={ABC201113124721} JMSType={null} JMSTimestamp={Sat Nov 01 17:57:15 GST 2014} JMSExpiration={0} JMSPriority={4} } Properties={ } Text={<?xml version="1.0" encoding="UTF-8"?>
<REPLY><header><msg_type>TT</msg_type><msg_function>POSTING</msg_function><src_application>FinBroker</src_application><msg_id>ABC123456789</msg_id><bank_id>EBI</bank_id><timestamp>2014-11-01T17:57:15.19+04:00</timestamp><error_code>ABC-DEF-GHI-000</error_code><error_description>Success</error_description></header></REPLY>} }

我的问题是您能帮我确定发送此响应的格式吗?如何使用 python 解析此格式?

【问题讨论】:

标签: python jms


【解决方案1】:

正如上面在你的标签中指定的,它似乎是一个通用的xml标签。 如果是,那么您可以将 lxml 的 etee 模块用于 xpath。 例如:

def parseXML(path): from lxml import etree tree = etree.tree(path) root = tree.getroot() get_error_code = root.xpath('//')

会给出错误码,如果你没有子节点,那么你可以直接使用'xpath'访问节点

【讨论】:

  • etree 似乎无法处理此问题,因为这不是常规的 xml 格式。关于这个文本是什么格式的任何线索?
猜你喜欢
  • 2014-09-03
  • 1970-01-01
  • 2021-06-03
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 2021-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多