使用 Nokogiri 插入和删除 XML 节点和元素 [英] Inserting and deleting XML nodes and elements using Nokogiri
问题描述
我想提取 XML 文件的一部分并记下我提取了该文件中的某些部分,例如此处提取了某些内容".
I want to extract parts of an XML file and make a note that I extracted some part in that file, like "here something was extracted".
我正在尝试用 Nokogiri 来做这件事,但似乎没有真正记录如何:
I'm trying to do this with Nokogiri, but it seems to not really be documented on how to:
- 删除
- 更改该完整元素的
inner_text
有什么线索吗?
推荐答案
Nokogiri 使这变得非常容易.以本文档为例,以下代码将查找所有维生素
标签,移除他们的孩子(以及孩子的孩子等),并将他们的内部文字更改为已移除孩子":
Nokogiri makes this pretty easy. Using this document as an example, the following code will find all vitamins
tags, remove their children (and the children's children, etc.), and change their inner text to say "Children removed.":
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
节点看起来像这样:
A given food
node will go from looking like this:
<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>
这篇关于使用 Nokogiri 插入和删除 XML 节点和元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!