1

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に移行しています。

これを行う方法についてのアイデアはありますか?

4

1 に答える 1

5

これは非常に簡単です。新しい要素を初期化して、他の要素に追加するだけです。残念ながら、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>
于 2013-02-07T14:27:40.013 に答える