以正确的顺序访问xml的节点

时间:2013-08-12 09:07:57

标签: ruby xml

我在从xml文件中提取数据时遇到问题:

         <diagnosis_list>
            <diagnosis>
              <icd_code V="A00.-"/>
              <description V="Cholera"/>

              <diagnosis_list>

                <diagnosis>
                  <icd_code V="A00.0"/>
                  <description V="Cholera durch Vibrio cholerae O:1, Biovar cholerae"/>
                  <dangerous V="j"/>
                  <subcategories_list>
                    <subcategory V="Klassische Cholera"/>
                  </subcategories_list>
                </diagnosis>

                <diagnosis>
                  <icd_code V="A00.1"/>
                  <description V="Cholera durch Vibrio cholerae O:1, Biovar eltor"/>
                  <dangerous V="j"/>
                  <subcategories_list>
                    <subcategory V="El-Tor-Cholera"/>
                    <subcategory V="Choleraartige Dysenterie"/>
                    <subcategory V="El-Tor-Enteritis"/>
                  </subcategories_list>
                </diagnosis>

              </diagnosis_list>

            </diagnosis>
          </diagnosis_list>

我用nokigiri和Css尝试了几件事,但并没有真正成功。 作为输出我想要像:

 A00.- => Cholera

 A00.0 => Cholera durch Vibrio cholerae O:1, Biovar cholerae
 Subcategories => Klassische Cholera

 A00.1 => Cholera durch Vibrio cholerae O:1, Biovar eltor
 Subcategories => El-Tor-Cholera
                  Choleraartige Dysenterie
                  El-Tor-Enteritis

但是现在的输出并不那么重要,我很高兴每个建议如何以正确的顺序访问xml的节点!谢谢

1 个答案:

答案 0 :(得分:2)

使用xpath:

require 'nokogiri'
doc = Nokogiri::XML(xml)
doc.xpath('//icd_code|//description').each do |node|
  p node['V']
end

打印

"A00.-"
"Cholera"
"A00.0"
"Cholera durch Vibrio cholerae O:1, Biovar cholerae"
"A00.1"
"Cholera durch Vibrio cholerae O:1, Biovar eltor"

<强>更新

require 'nokogiri'
doc = Nokogiri::XML(xml)
doc.css('icd_code').each do |icd_code|
  desc = icd_code.css('~ description')[0]
  puts "#{icd_code['V']} => #{desc['V']}"
  icd_code.css('~ subcategories_list>subcategory').each do |subcategory|
    puts "    #{subcategory['V']}"
  end
end

打印

A00.- => Cholera
A00.0 => Cholera durch Vibrio cholerae O:1, Biovar cholerae
    Klassische Cholera
A00.1 => Cholera durch Vibrio cholerae O:1, Biovar eltor
    El-Tor-Cholera
    Choleraartige Dysenterie
    El-Tor-Enteritis