Merge two XML files in Nokogiri

為{幸葍}努か 提交于 2019-12-10 22:07:12

问题


There are some posts about this topic, but I wasn't able to figure out how to solve my problem.

I have two XML files:

<Products>
  <Product>
    <some>
      <value></value>
    </some>
  </Product>
  <Product>
    <more>
      <data></data>
    </more>
  </Product>
</Products>

And:

<Products>
  <Product>
    <some_other>
      <value></value>
    </some_other>
  </Product>
</Products>

I want to generate an XML document that looks like this:

<Products>
  <Product>
    <some>
      <value></value>
    </some>
  </Product>
  <Product>
    <more>
      <data></data>
    </more>
  </Product>
  <Product>
    <some_other>
      <value></value>
    </some_other>
  </Product>
</Products>

Every node <Product> should be merged into <Products>.

I tried to create a new document using:

doc = Nokogiri::XML("<Products></Products>")
nodes = files.map { |xml| Nokogiri::XML(xml).xpath("//Product") }
set = Nokogiri::XML::NodeSet.new(doc, nodes)

but this throws an error: ArgumentError: node must be a Nokogiri::XML::Node or Nokogiri::XML::Namespace.

I think I don't understand NodeSet, but I can't figure out how to merge these two XML files.


回答1:


Your sample code won't generate what you want because you're throwing away your some and more nodes when you do:

doc = Nokogiri::XML("<Products></Products>")

Instead of creating an empty DOM, you need to piggyback on the original and simply append the new nodes to it:

require 'nokogiri'

xml1 = '<Products>
  <Product>
    <some>
      <value></value>
    </some>
  </Product>
  <Product>
    <more>
      <data></data>
    </more>
  </Product>
</Products>
'

xml2 = '<Products>
  <Product>
    <some_other>
      <value></value>
    </some_other>
  </Product>
</Products>
'

doc = Nokogiri::XML(xml1)

Find the new Product nodes to be added:

new_products = Nokogiri::XML(xml2).search('Product')

Append them to the original document as added children of Products:

doc.at('Products').add_child(new_products)

This results in the DOM in doc looking like:

puts doc.to_xml
# >> <?xml version="1.0"?>
# >> <Products>
# >>   <Product>
# >>     <some>
# >>       <value/>
# >>     </some>
# >>   </Product>
# >>   <Product>
# >>     <more>
# >>       <data/>
# >>     </more>
# >>   </Product>
# >> <Product>
# >>     <some_other>
# >>       <value/>
# >>     </some_other>
# >>   </Product></Products>


来源:https://stackoverflow.com/questions/31008613/merge-two-xml-files-in-nokogiri

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!