使用 CDATA 解析 params 中的 html 数据以获取 xml 响应

发布于 2025-01-02 12:15:58 字数 1908 浏览 0 评论 0原文

在 api 中,我通过点击参数中包含请求数据的 url 来生成 Xml 响应。它包含一些具有 html 内容和标签的字段。内容被正确保存在数据库中,但是当生成响应时,标签正在被编码,这会发生,因为我们需要在解析时跳过这些字段。我想知道如何实现 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>...

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

In the api, i'm generating a Xml response by hitting the url with request data in params. It contains some field which have html content and tags.The content is getting saved correctly in DB but when the response is getting generated the tags are being encoded which will happen as we need to skip those fields while parsing. I would like to know that how can i implement CDATA in order to skip the specific fields while parsing.

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

Content passed is a params hash, and format is xml. resp contains the following data when i tried to print it.Detailed description tag contains the encoded data

 [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><b>this is the testing detailed</b> </detailed_description>...

I would surely like to post some extra code if required.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2025-01-09 12:15:58

那么您的数据采用 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"

一旦您将字段放入变量中,您就可以做任何您想要的事情。

So your data is in XML and you would like to know the content of these fields?

Use 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"

Once you have your fields in variables you can whatever you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文