【问题标题】:Parsing html data in params for xml response using CDATA使用 CDATA 解析 xml 响应的参数中的 html 数据
【发布时间】:2023-03-13 23:25:01
【问题描述】:

在 api 中,我通过使用参数中的请求数据点击 url 来生成 Xml 响应。它包含一些具有 html 内容和标签的字段。内容已正确保存在 DB 中,但是当生成响应时,标签正在被编码,这将发生,因为我们需要在解析时跳过这些字段。我想知道如何实现 CDATA 以便在解析时跳过特定字段。

def generate_mobile_api_success_response(status_code, format, request_id, content = nil)
  format_type_method, options_hash, content_type = get_format_method(format)

  data = { "request_id" => request_id, "status" => status_code, "message" => status_message(status_code)}
  data["data"] = content unless content.blank?
  data = generate_data_format(format, data)

  resp = [status_code, { "Content-Type" => content_type , "request_id" => request_id}, data.send(format_type_method, options_hash)]
  generate_active_controller_response_format(resp)
  resp
end

传递的内容是params hash,格式是xml。当我尝试打印时,resp 包含以下数据。详细描述标签包含编码数据

 [201, {"request_id"=>"b425bce0-307d-012f-3e68-042b2b8686e6", "Content-Type"=>"application/xml"}, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>\n  <data>\n    <fine_print_line_3>line 3</fine_print_line_3>\n    <available_start_date>2012-02-02T06:00:00+05:30</available_start_date>\n    <status>inactive</status>\n    <highlight_line_2>gfgf</highlight_line_2>\n    <original_price>50.00</original_price>\n    <category_id>bc210bb0-52b7-012e-8896-12313b077c61</category_id>\n    <available_end_date>2012-03-25T00:00:00+05:30</available_end_date>\n    <expiry_date>2012-08-25T00:00:00+05:30</expiry_date>\n    <highlight_line_3></highlight_line_3>\n   <product_service>food</product_service>\n   <created_at>2012-02-03T15:43:56+05:30</created_at>\n   <detailed_description>&lt;b&gt;this is the testing detailed&lt;/b&gt; </detailed_description>...

如果需要,我当然想发布一些额外的代码。

【问题讨论】:

  • 我环顾四周寻找大多数建议的答案,但无法为我工作:(

标签: html ruby-on-rails xml cdata


【解决方案1】:

所以您的数据是 XML 格式的,您想知道这些字段的内容吗?

使用 Nokogiri:

xml = data.send(format_type_method, options_hash)
doc = Nokogiri::XML(xml)
start_date = doc.xpath("//available_start_date").content

p start_date #=> "2012-02-02T06:00:00+05:30"

一旦您将字段放入变量中,您就可以随心所欲。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 2013-03-28
    • 2015-06-27
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多