0

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
1
  • 1
    Please explain 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. If the data is in a database search against it. If it's not and you want to find records to load then load the data into a separate table and select the desired records from it and insert into the master. A DB 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. meta.stackexchange.com/questions/66377/what-is-the-xy-problem Commented Sep 10, 2015 at 15:53

1 Answer 1

3

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?".

  • If the data is already in a database, search against it and select the records you need.
  • If it's not and you want to find records to load, then load the data into a separate table and select the desired records from it and insert them into the master.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Tin Man

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.