Man*_*ahi 1 ruby xml nokogiri xml-parsing
我需要使用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>
Run Code Online (Sandbox Code Playgroud)
我将标签内的数据作为变量中的字符串,如下所示:
jobtitle = "Programmer Analyst 3-IT" and so on...
Run Code Online (Sandbox Code Playgroud)
我目前正在使用Nokogiri来生成XML,但我需要处理大数据,并且出于性能考虑,我正在转向Ox.
关于如何做到这一点的任何想法?
它非常简单,只需初始化新元素并将它们附加到其他元素即可.不幸的是,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)
Run Code Online (Sandbox Code Playgroud)
返回:
<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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
561 次 |
| 最近记录: |