简体   繁体   English

使用Nokogiri进行快速XML解析

[英]Fast XML parsing with Nokogiri

I'm parsing an XML file with Ruby and Nokogiri, but I have some problems with execution time. 我正在使用Ruby和Nokogiri解析XML文件,但是执行时间有些问题。 It's taking one minute for 4000 lines - it is too much. 4000条线路需要一分钟-太多了。 I'm trying to find some ideas for code optimisation. 我正在尝试寻找一些优化代码的想法。

This is an example of the code: 这是代码示例:

input = Nokogiri::XML(File.open(file_name))
input.xpath('//Transition').each do |transition_record|
  from = transition_record.xpath('@From')
  to = transition_record.xpath('@To')
  name = transition_record.xpath('@Name')
  Transition.create do |transition|
    transition.from_id = from.to_s
    transition.to_id = to.to_s
    transition.name = name.to_s

    if transition_record.at_xpath('Condition').at_xpath('@Type')
      transition.condition = transition_record.at_xpath('Condition').at_xpath('@Type').to_s
    end
  end
end

Transition isn't fixed place in place in the XML and can be at any level. Transition在XML中不是固定位置,可以处于任何级别。 Also, I'm parsing three other tags in the some way. 另外,我正在以某种方式解析其他三个标签。

Does anybody have any ideas how I can improve it? 有人对我可以改善它有任何想法吗?

You can reorganize your code so that you are creating an array of hashes with Nokogiri, then you can use them directly in create . 您可以重新组织代码,以便使用Nokogiri创建哈希数组,然后可以在create直接使用它们。 This will allow you to time each part separately. 这将使您可以分别计时每个部分。 I think you'll find that Nokogiri isn't the problem. 我想您会发现Nokogiri并不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM