0

我在从 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 的节点!谢谢

4

1 回答 1

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
于 2013-08-12T09:15:28.893 回答