【问题标题】:Generating XML with cdata using Ox?使用 Ox 使用 cdata 生成 XML?
【发布时间】:2013-01-22 23:50:08
【问题描述】:

我需要使用ox 生成 XML,但没有从文档中获得太多帮助。我需要像这样生成 XML:

<Jobpostings>
  <Postings>
    <Posting>
      <JobTitle><cdata>Programmer Analyst 3-IT</cdata></JobTitle>
      <Location><cdata>Romania,Bucharest...</cdata></Location>
      <CountryCode><cdata>US</cdata>   </CountryCode>
      <JobDescription><cdata>class technology to develop.</cdata></JobDescription>             
    </Posting>     
  </Postings>
</jobpostings>

我将标签内的数据作为变量中的字符串,如下所示:

jobtitle = "Programmer Analyst 3-IT" and so on...

我目前正在使用 Nokogiri 生成 XML,但我需要处理大数据,并且为了性能,我将迁移到 Ox。

关于如何做到这一点的任何想法?

【问题讨论】:

  • is impressive的表现。
  • Ox 的性能非常好,但我注意到它缺少像 Nokogiri 等重量级库的一些功能。不过,这可能是有意设计的。

标签: ruby xml xml-parsing nokogiri


【解决方案1】:

这很简单,您只需初始化新元素并将它们附加到其他元素。不幸的是,Ox 库中没有 XML 构建器......这是一个例子:

require 'ox'
include Ox

source = Document.new

jobpostings = Element.new('Jobpostings')
source << jobpostings

postings = Element.new('Postings')
jobpostings << postings

posting = Element.new('Posting')
postings << posting

jobtitle = Element.new('JobTitle')
posting << jobtitle
jobtitle << CData.new('Programmer Analyst 3-IT')

location = Element.new('Location')
posting << location
location << CData.new('Romania,Bucharest...')

countrycode = Element.new('CountryCode')
posting << countrycode
countrycode << CData.new('US')
countrycode << '   '

jobdescription = Element.new('JobDescription')
posting << jobdescription
jobdescription << CData.new('class technology to develop.')

puts dump(source)

返回:

<Jobpostings>
  <Postings>
    <Posting>
      <JobTitle>
        <![CDATA[Programmer Analyst 3-IT]]>
      </JobTitle>
      <Location>
        <![CDATA[Romania,Bucharest...]]>
      </Location>
      <CountryCode>
        <![CDATA[US]]>   </CountryCode>
      <JobDescription>
        <![CDATA[class technology to develop.]]>
      </JobDescription>
    </Posting>
  </Postings>
</Jobpostings>

【讨论】:

    猜你喜欢
    • 2020-06-10
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2013-09-07
    • 2019-02-22
    相关资源
    最近更新 更多