T
T
TopClone2016-12-06 19:04:05
ruby
TopClone, 2016-12-06 19:04:05

How to remove tags in xml using Ruby?

I processed the xml file. Now its contents are in the doc variable. I want to create a loop that will edit each line, removing the "" tag from there. Help me please.

if (Gem.win_platform?)
  Encoding.default_external = Encoding.find(Encoding.locale_charmap)
  Encoding.default_internal = __ENCODING__

  [STDIN, STDOUT].each do |io|
    io.set_encoding(Encoding.default_external, Encoding.default_internal)
  end
end
require "rexml/document"
require "date"


file_xml = File.open("file.xml", encoding: "utf-8")
doc =  REXML::Document.new(file_xml)

doc.elements.each("page/item") do |element|
  #
end

Fragment file.xml
<item found="1124120">bmw x5</item>
<item found="967578">audi q7</item>
<item found="127888">lada vesta</item>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
N. Bekseitov, 2016-12-06
@nbekseitov

doc.elements.each("page/item") do |element|
  element.delete_attribute("found") 
  doc.delete_element "/page/item"
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question