Ruby - 读取和编辑XML文件

Sil*_*ver 5 ruby xml ruby-1.9.3

我正在编写一个Ruby(1.9.3)脚本,它从文件夹中读取XML文件,然后在必要时进行编辑.

我的问题是我得到了由Tidy转换的XML文件,但它的输出有点奇怪,例如:

<?xml version="1.0" encoding="utf-8"?>
<XML>
  <item>
      <ID>000001</ID>
      <YEAR>2013</YEAR>
      <SUPPLIER>Supplier name test,
      Coproration</SUPPLIER>
...
Run Code Online (Sandbox Code Playgroud)

正如你可以看到有和更多的CRLF.我不知道为什么它有这种行为,但我用ruby脚本解决它.但我遇到麻烦,因为我需要看到该行的最后一个字符是" > "还是第一个字符是" < ",以便我可以看到标记是否有问题.

我试过了:

Dir.glob("C:/testing/corrected/*.xml").each do |file|

puts file

  File.open(file, 'r+').each_with_index do |line, index|

    first_char = line[0,1]

    if first_char != "<"
        //copy this line to the previous line and delete this one?
    end

  end

end
Run Code Online (Sandbox Code Playgroud)

当我将其读取到另一个临时文件然后覆盖时,我也觉得我应该复制原始文件内容.这是最好的"方式"吗?欢迎任何提示,因为我在更改文件内容方面没有太多经验.

问候

fbo*_*tti 11

这个额外的东西\n总是出现在<SUPPLIER>节点中吗?正如其他人所建议的那样,Nokogiri是解析XML(或HTML)的绝佳选择.您可以遍历每个<SUPPLIER>节点并删除该\n字符,然后将XML另存为新文件.

require 'nokogiri'

# read and parse the old file
file = File.read("old.xml")
xml = Nokogiri::XML(file)

# replace \n and any additional whitespace with a space
xml.xpath("//SUPPLIER").each do |node|
  node.content = node.content.gsub(/\n\s+/, " ")
end

# save the output into a new file
File.open("new.xml", "w") do |f|
  f.write xml.to_xml
end
Run Code Online (Sandbox Code Playgroud)