简体   繁体   English

使用Ox使用cdata生成XML?

[英]Generating XML with cdata using Ox?

I need to generate XML using ox but didn't get much help from the documentation. 我需要使用ox生成XML,但是从文档中获得的帮助并不多。 I need to generate XML like this: 我需要生成这样的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>

I have the data inside the tags as strings in variables like this: 我将变量中的数据作为字符串存储在像这样的变量中:

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

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. 我目前正在使用Nokogiri生成XML,但是我需要处理大数据,出于性能的考虑,我将迁移到Ox。

Any ideas on how to do this? 有关如何执行此操作的任何想法?

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: 不幸的是,尽管在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)

Returns: 返回值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM