From: James Britt Date: 2006-12-30T02:40:18+09:00 Subject: Re: XML - converting from one feed to another (beginner) rb wrote: > I'm trying to read an XML feed of products and convert them to a > different XML feed to upload to Froogle (Google Base). > > How do I read the lines of XML and then rewrite them in the new XML? > I've started with rexml, but I'm not sure if I'm on the right track. > > > def convert_to_base_xml(xml) > # Takes a product XML feed and converts it into one that > # is formatted for uploading to Google Base > > # doc = REXML::Document.new > feed = REXML::Document.new(xml) > > # Create the export XML document > doc = REXML:Document.new You may be better off building the new XML using either direct string concatenating, or a lib such as Jim Weirich's Builder (which will help ensure the result is proper XML). Or one of the Ruby RSS libraries. (I'd go with populating templates and just make sure the new content is correctly escaped.) The general ides is to loop over the item elements in the source DOM (using either REXML or Hpricot), extract the relevant data, and populate a new item element in the target XML. If you have a template for the target item element you can stuff in the new content on each pass of the loop and append it to the resulting XML. For example, with Hpricot (which you can install as a gem): require 'hpricot' ... src_dom = Hpricot(source_rss_xml). src_dom/'//item'.each do |el| title = (item/'title').text title_url = (item/'title_url').text # ... # Now build the new item element for the target XML # and add it to the accumulating content end (For REXML it's basically the same, but the XPath invocation is different.) Some considerations may be time and memory needs; if you are dealing with large documents, a pull or stream parser would be better, but it can be a bit harder to work with if you are new to it. But see my article in Dr. Dobbs: http://www.ddj.com/184406385 Try the simplest approach first and see if it works, and if works well enough. -- James Britt http://www.rubyaz.org - Hacking in the Desert http://www.jamesbritt.com - Playing with Better Toys