使用Nokogiri插入和删除XML节点和元素

时间:2009-08-13 21:44:31

标签: ruby nokogiri

我想提取XML文件的一部分并记下我在该文件中提取的部分内容,例如“这里提取的内容”。

我正在尝试与Nokogiri这样做,但似乎没有真正记录如何:

  1. 删除<Nokogiri::XML::Element>
  2. 的所有子项
  3. 更改该完整元素的inner_text
  4. 任何线索?

4 个答案:

答案 0 :(得分:16)

Nokogiri让这很容易。以this document为例,以下代码将找到所有vitamins标记,删除其子项(以及子项的子项等),并将其内部文本更改为“删除子项”。:< / p>

require 'nokogiri'

io = File.open('sample.xml', 'r')
doc = Nokogiri::XML(io)
io.close

doc.search('//vitamins').each do |node|
  node.children.remove
  node.content = 'Children removed.'
end

给定的food节点将如下所示:

<food>
    <name>Avocado Dip</name>
    <mfr>Sunnydale</mfr>
    <serving units="g">29</serving>
    <calories total="110" fat="100"/>
    <total-fat>11</total-fat>
    <saturated-fat>3</saturated-fat>
    <cholesterol>5</cholesterol>
    <sodium>210</sodium>
    <carb>2</carb>
    <fiber>0</fiber>
    <protein>1</protein>
    <vitamins>
        <a>0</a>
        <c>0</c>
    </vitamins>
    <minerals>
        <ca>0</ca>
        <fe>0</fe>
    </minerals>
</food>

到此:

<food>
    <name>Avocado Dip</name>
    <mfr>Sunnydale</mfr>
    <serving units="g">29</serving>
    <calories total="110" fat="100"/>
    <total-fat>11</total-fat>
    <saturated-fat>3</saturated-fat>
    <cholesterol>5</cholesterol>
    <sodium>210</sodium>
    <carb>2</carb>
    <fiber>0</fiber>
    <protein>1</protein>
    <vitamins>Children removed.</vitamins>
    <minerals>
        <ca>0</ca>
        <fe>0</fe>
    </minerals>
</food>

答案 1 :(得分:3)

你可以这样做:

doc=Nokogiri::XML(your_document)
note=doc.search("note") # find all tags with the node_name "note"
note.remove

虽然这会删除<note>标记内的所有子项,但我不确定如何“更改所有注释元素的inner_text”。我认为inner_text不适用于Nokogiri :: XML :: Element。

答案 2 :(得分:3)

之前的Nokogiri示例让我朝着正确的方向前进,但使用doc.search留下了格式错误的//vitamins,所以我使用了CSS:

require "rubygems"
require "nokogiri"

f = File.open("food.xml")
doc = Nokogiri::XML(f)

doc.css("food vitamins").each do |node|
  puts "\r\n[debug] Before: vitamins= \r\n#{node}"
  node.children.remove
  node.content = "Children removed"
  puts "\r\n[debug] After: vitamins=\r\n#{node}"
end
f.close

结果是:

debug] Before: vitamins= 
<vitamins>
        <a>0</a>
        <c>0</c>
    </vitamins>

[debug] After: vitamins=
<vitamins>Children removed</vitamins>

答案 3 :(得分:2)

这就是我要做的事情:

首先解析一些XML:

require 'nokogiri'

doc = Nokogiri::XML(<<EOT)
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="nutrition.css"?>
<nutrition>

  <daily-values>
    <total-fat units="g">65</total-fat>
    <saturated-fat units="g">20</saturated-fat>
    <cholesterol units="mg">300</cholesterol>
    <sodium units="mg">2400</sodium>
    <carb units="g">300</carb>
    <fiber units="g">25</fiber>
    <protein units="g">50</protein>
  </daily-values>

  <food>
    <name>Avocado Dip</name>
    <mfr>Sunnydale</mfr>
    <serving units="g">29</serving>
    <calories total="110" fat="100"/>
    <total-fat>11</total-fat>
    <saturated-fat>3</saturated-fat>
    <cholesterol>5</cholesterol>
    <sodium>210</sodium>
    <carb>2</carb>
    <fiber>0</fiber>
    <protein>1</protein>
    <vitamins>
      <a>0</a>
      <c>0</c>
    </vitamins>
    <minerals>
      <ca>0</ca>
      <fe>0</fe>
    </minerals>
  </food>

</nutrition>
EOT

如果我想删除节点的内容,我可以删除其children或为其内容指定nil:

doc.at('total-fat').to_xml # => "<total-fat units=\"g\">65</total-fat>"
doc.at('total-fat').children.remove
doc.at('total-fat').to_xml # => "<total-fat units=\"g\"/>"

或:

doc.at('saturated-fat').to_xml # => "<saturated-fat units=\"g\">20</saturated-fat>"
doc.at('saturated-fat').content = nil
doc.at('saturated-fat').to_xml # => "<saturated-fat units=\"g\"/>"

如果我想从节点中提取文本以便以其他方式使用:

food = doc.at('food').text
# => "\n    Avocado Dip\n    Sunnydale\n    29\n    \n    11\n    3\n    5\n    210\n    2\n    0\n    1\n    \n      0\n      0\n    \n    \n      0\n      0\n    \n  "

或:

food = doc.at('food').children.map(&:text)
# => ["\n    ",
#     "Avocado Dip",
#     "\n    ",
#     "Sunnydale",
#     "\n    ",
#     "29",
#     "\n    ",
#     "",
#     "\n    ",
#     "11",
#     "\n    ",
#     "3",
#     "\n    ",
#     "5",
#     "\n    ",
#     "210",
#     "\n    ",
#     "2",
#     "\n    ",
#     "0",
#     "\n    ",
#     "1",
#     "\n    ",
#     "\n      0\n      0\n    ",
#     "\n    ",
#     "\n      0\n      0\n    ",
#     "\n  "]

或者你想要破坏文本。

而且,如果你想标记你已删除了文字:

doc.at('food').content = 'REMOVED'
doc.at('food').to_xml # => "<food>REMOVED</food>"

您也可以使用XML注释:

doc.at('food').children = '<!-- REMOVED -->'
doc.at('food').to_xml # => "<food>\n  <!-- REMOVED -->\n</food>"