sách gpt4 ăn đã đi

Ruby - 读取和编辑 XML 文件

In lại 作者:数据小太阳 更新时间:2023-10-29 01:53:45 28 4
mua khóa gpt4 giày nike

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

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




000001
2013
Supplier name test,
Coproration

...

如您所见,有和额外的 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?
kết thúc

kết thúc

kết thúc

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

lời chào

câu trả lời hay nhất

那额外的吗\N总是出现在节点?正如其他人所建议的那样,Nokogiri 是解析 XML(或 HTML)的绝佳选择。您可以遍历每个 节点并删除 \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+/, " ")
kết thúc

# save the output into a new file
File.open("new.xml", "w") do |f|
f.write xml.to_xml
kết thúc

关于Ruby - 读取和编辑 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16874830/

28 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress