Reputation: 4636
I have a table called "users", which has three columns:
id, ref, name
I want to parse the following XML document using Nokogiri and list the matched and unmatched records by comparing the values in the "users" table records:
<?xml version="1.0" encoding="UTF-8"?>
<EXPORT>
<DETAIL>
<ID>150</ID>
<REF>188440</REF>
<USER>Bruce</USER>
</DETAIL>
<DETAIL>
<ID>1501003</ID>
<REF>1884402</REF>
<USER>Alice</USER>
</DETAIL>
</EXPORT>
For example:
Users.where(id: 1501003).name
Users.where(ref: 188440).name
Find the name
by comparing the id
and ref
.
Note: if id
does not match, then compare with ref
. If id
matches then ignore ref
.
I tried:
doc = Nokogiri::XML(File.open(self.filename))
exp = "//EXPORT/DETAIL"
NODES = doc.xpath(exp)
nodes.each do |node|
unless node.text.nil?
User.where(id: node.text).first.name
end
Upvotes: 0
Views: 286
Reputation: 160601
I wonder why you want to use Nokogiri to do lookups from what appears to be a database dump in XML. That seems terribly awkward and a very slow and fragile solution.
A database XML dump can easily exceed available RAM making your plan unviable. Using a SAX parser might help but still, there's smell to the solution. See "What is the XY problem?".
Upvotes: 3