使用 Ox 用 cdata 生成 XML? [英] Generating XML with cdata using Ox?
问题描述
我需要使用 ox 生成 XML,但没有从文档中得到太多帮助.我需要像这样生成 XML:
I need to generate XML using ox but didn't get much help from the documentation. I need to generate XML like this:
<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>
我将标签内的数据作为变量中的字符串,如下所示:
I have the data inside the tags as strings in variables like this:
jobtitle = "Programmer Analyst 3-IT" and so on...
我目前正在使用 Nokogiri 生成 XML,但我需要处理大数据,并且为了性能考虑,我将转向 Ox.
I am currently using Nokogiri to generate XML but I need to work on large data, and, for the performance sake I am moving to Ox.
关于如何做到这一点的任何想法?
Any ideas on how to do this?
推荐答案
这很简单,您只需初始化新元素并将它们附加到其他元素.不幸的是,Ox 库中没有 XML 构建器......这是一个例子:
It's pretty simple, you just initialize new elements and append them to other elements. Unfortunately there isn't an XML builder in the Ox library though... Here's an example:
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>
这篇关于使用 Ox 用 cdata 生成 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!