代码之家  ›  专栏  ›  技术社区  ›  user10847

如何使用nokogiri xml生成器

  •  0
  • user10847  · 技术社区  · 11 年前

    所以从一个特定的行开始,它似乎会在下面的行中造成错误,对不起,这个问题似乎很模糊,我无法清楚地说明我在这一行中做得如何。

    builder = Nokogiri::XML::Builder.new do |xml|
      xml.send('document-id' => '{some-fake-id}', 'type' => 'documentType', 'iso-code' => 'BP', 'training' => 'false', 'send-type' => 'ctype', 'exempt' => 'false', 'cout' => 'false', 'void' => 'false', 'void-stored' => 'false', 'auto' => 'false', 'detail' => 'false', 'regular' => 'true', 'close' => 'false', 'vo' => 'false', 'mod' => 'false') 
      {
        xml.send(:"t-id",  $ID)
        xml.send(:"v-id",  "0")
        xml.date  $todayis.to_s
        xml.send(:"time"('dts' => "+05:00", today.year + "-" + today.month + "-2009:37:55.000+01:00"))
        xml.send(:"b-date", $todayis)
        xml.send(:"ticket-open-time", ('dts' => "+01:00")_ today.year + "-" + today.month + "-20T09:37:55.000+01:00")
    

    等

    发生的错误是:

    syntax error, unexpected '\n', expecting =>
    
    1 回复  |  直到 11 年前
        1
  •  0
  •   Eugene Petrov    11 年前

    要获得 <time dst="+05:00">2014-05-2009:38:48.946+01:00</time> 格式化字符串,您应该使用以下nokogiri调用:

    xml.send :"time" , :"dts" => "+05:00" do |xml|
      xml << today.year + "-" + today.month + "-2009:37:55.000+01:00"
    end
    

    在几个单词中,nokogiri使用如下语法:

    xml.send :"tag", :key1=>"value1", :key2=>"value2" do |var| 
      var.nestedtag
      var<<"content" 
    end
    

    其呈现为:

    <tag key1="value1" key2="value2"><nestedtag /> content </tag>
    

    看看 docs 和更多信息的来源( 真的有 ).

    所以你的生成器可能看起来像这样(我引用了变量的可读性):

        builder = Nokogiri::XML::Builder.new do |xml|
          xml.root :"document-id" => '{some-fake-id}',
                   :"type" => 'documentType',
                   :"iso-code" => 'BP',
                   :"training" => 'false',
                   :"send-type" => 'ctype',
                   :"exempt" => 'false',
                   :"cout" => 'false',
                   :"void" => 'false',
                   :"void-stored" => 'false',
                   :"auto" => 'false',
                   :"detail" => 'false',
                   :"regular" => 'true',
                   :"close" => 'false',
                   :"vo" => 'false',
                   :"mod" => 'false' do |xml|
    
    
          xml.send :"t-id", "$ID"
          xml.send :"v-id",  "0"
          xml.date  "$todayis".to_s
          xml.send :"time" , :"dts" => "+05:00" do |xml|
            xml << "today.year" + "-" + "today.month" + "-2009:37:55.000+01:00"
          end
          xml.send :"b-date", "$todayis"
          xml.send :"time" , :"dts" => "+05:00" do |xml|
            xml << "today.year" + "-" + "today.month" + "-2009:37:55.000+01:00"
          end
          xml.send :"ticket-open-time", :'dts' => "+01:00" do |xml|
            xml << "today.year" + "-" + "today.month" + "-20T09:37:55.000+01:00"
          end
        end
    

    这将导致:

    <?xml version="1.0"?>
    <root document-id="{some-fake-id}" type="documentType" iso-code="BP" training="false" send-type="ctype" exempt="false" cout="false" void="false" void-stored="false" auto="false" detail="false" regular="true" close="false" vo="false" mod="false">
    <t-id>$ID</t-id>
    <v-id>0</v-id>
    <date>$todayis</date>
    <time dts="+05:00">today.year-today.month-2009:37:55.000+01:00</time>
    <b-date>$todayis</b-date>
    <time dts="+05:00">today.year-today.month-2009:37:55.000+01:00</time>
    <ticket-open-time dts="+01:00">today.year-today.month-20T09:37:55.000+01:00</ticket-open-time>
    </root>