Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!talisker.lacave.net!lacave.net!not-for-mail From: "Kyle X." Newsgroups: comp.lang.ruby Subject: Re: Can you search in REXML by attributes? Date: Mon, 11 Apr 2011 13:57:42 -0500 Organization: Service de news de lacave.net Lines: 103 Message-ID: <3eb231103a780e91a17cc5b95fd82ddf@ruby-forum.com> References: <6680e1dd986ba2ce87d806950a81ee57@ruby-forum.com> <07d88b73a8b6b59812b5fed98c782aca@ruby-forum.com> <560f26c9f7549339ef393be5559307fd@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1302548275 98680 65.111.164.187 (11 Apr 2011 18:57:55 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Mon, 11 Apr 2011 18:57:55 +0000 (UTC) In-Reply-To: X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 381304 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <3eb231103a780e91a17cc5b95fd82ddf@ruby-forum.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:2640 > This works for me: > > $ cat noko.rb && ruby -rubygems noko.rb > require 'nokogiri' > > doc = Nokogiri::XML(File.read("one.xml")) > reference = doc.css("IfcCartesianPoint Coordinates > IfcLengthMeasure").first > puts reference.text > > 117.4 > > But if I leave a space before IfcCartesianPoint in the call to the css > method I get a parser error (`on_error': unexpected ' ' after '' > (Nokogiri::CSS::SyntaxError)). This is my file one.xml: > > > > > > > > > > > > > > > > > 117.4 > 119.7 > 0. > > > > > This works too: > > doc.css("IfcCartesianPoint Coordinates IfcLengthMeasure").each {|el| > puts el.text} > > Produces: > > 117.4 > 119.7 > 0. Thank you for your reply. When I continue to try and read the file I have it keeps returning nil values and thus doesn't work. But when I copy and paste the xml you have written over the file I am trying to read then it does work. I understand that the path is slightly different but using the xpath command- doc.xpath("//IfcCartesianPoint/Coordinates/IfcLengthMeasure").each {|el| puts el.text} It should skip ahead to the first appearance of IfcCartesianPoint, much the same as it works for using REXML xpath, no? As this same sting of IfcCartesianPoint/Coordinates/IfcLengthMeasure appears in this file. Based on the documentation here - (http://nokogiri.org/tutorials/searching_a_xml_html_document.html) I think it should be working but it always returns nil. I have attached the xml file I am trying to read and was wondering if you could see where my error is occurring. The first instance of IfcCartesianPoint/Coordinates/IfcLengthMeasure appears on line 225. > > Maybe it's the way you are passing the file to Nokogiri::XML? By the > way, in your way you are not closing the file handler. If you want to > pass Nokogiri the file instead of reading it yourself you can do: > > doc = nil > File.open("one.xml") {|f| doc = Nokogiri::XML(f)} > > or > > doc = File.open("one.xml") {|f| Nokogiri::XML(f)} > > This way, the file is properly closed. > > Jesus. Is there an advantage to using .open vs .read? The program I am writing has to grab lots of information from the xml, maybe 300 items, would it make a difference in speed to use one vs the other? Also for me to read a file at say c:\one.xml for it to read I have to write - doc = File.open("/one.xml") {|f| Nokogiri::XML(f)} Another form will not read including "\one.xml" Thank you again for your time, you have been most helpful and it is greatly appreciated. Attachments: http://www.ruby-forum.com/attachment/6114/one.xml -- Posted via http://www.ruby-forum.com/.