Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2640
| From | "Kyle X." <haebooty@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: Can you search in REXML by attributes? |
| Date | 2011-04-11 13:57 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <3eb231103a780e91a17cc5b95fd82ddf@ruby-forum.com> (permalink) |
| References | (2 earlier) <07d88b73a8b6b59812b5fed98c782aca@ruby-forum.com> <AANLkTikDnWpKcMM70U1VjsE5QJv8kpAbkFsKLKnyijtG@mail.gmail.com> <cce24af37a85d53425022f133364bb62@ruby-forum.com> <560f26c9f7549339ef393be5559307fd@ruby-forum.com> <BANLkTikxs8g3Gq6OGNhT7p6oGMUA4RUVNw@mail.gmail.com> |
> 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:
>
> <root xmlns:exp="http://foo" xmlns:xsi="http://bar">
> <IfcWallStandardCase id="i1677">
> <ObjectPlacement>
> <IfcLocalPlacement xsi:nil="true" ref="i1671"/>
> </ObjectPlacement>
> </IfcWallStandardCase>
>
> <IfcAxis2Placement3D id="i1671">
> <Location>
> <IfcCartesianPoint xsi:nil="true" ref="i1667"/>
> </Location>
> </IfcAxis2Placement3D>
>
> <IfcCartesianPoint id="i1667">
> <Coordinates id="i1670" exp:cType="list">
> <IfcLengthMeasure exp:pos="0">117.4</IfcLengthMeasure>
> <IfcLengthMeasure exp:pos="1">119.7</IfcLengthMeasure>
> <IfcLengthMeasure exp:pos="2">0.</IfcLengthMeasure>
> </Coordinates>
> </IfcCartesianPoint>
> </root>
>
> 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/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-03-31 19:53 -0500
Re: Can you search in REXML by attributes? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-31 20:27 -0500
Re: Can you search in REXML by attributes? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-31 20:45 -0500
Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-01 11:27 -0500
Re: Can you search in REXML by attributes? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-01 12:19 -0500
Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-10 18:01 -0500
Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-10 18:27 -0500
Re: Can you search in REXML by attributes? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-11 02:37 -0500
Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-11 13:57 -0500
Re: Can you search in REXML by attributes? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-12 03:17 -0500
Re: Can you search in REXML by attributes? Robert Klemme <shortcutter@googlemail.com> - 2011-04-12 04:46 -0500
Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-12 14:39 -0500
Re: Can you search in REXML by attributes? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-12 15:37 -0500
Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-01 14:22 -0500
Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-04 13:44 -0500
Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-19 15:42 -0500
Re: Can you search in REXML by attributes? "Kyle X." <haebooty@yahoo.com> - 2011-04-20 02:22 -0500
csiph-web