Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2674

Re: Can you search in REXML by attributes?

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.ruby
Subject Re: Can you search in REXML by attributes?
Date 2011-04-12 04:46 -0500
Organization Service de news de lacave.net
Message-ID <BANLkTim+s5g_prBMmzOJs2f7_+3gjfERnw@mail.gmail.com> (permalink)
References (4 earlier) <cce24af37a85d53425022f133364bb62@ruby-forum.com> <560f26c9f7549339ef393be5559307fd@ruby-forum.com> <BANLkTikxs8g3Gq6OGNhT7p6oGMUA4RUVNw@mail.gmail.com> <3eb231103a780e91a17cc5b95fd82ddf@ruby-forum.com> <BANLkTim9mUWkWgJDH4Obkx6gHjnUtdHLSQ@mail.gmail.com>

Show all headers | View raw


2011/4/12 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:
> On Mon, Apr 11, 2011 at 8:57 PM, Kyle X. <haebooty@yahoo.com> wrote:
>> 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.
>
> The difference is that you have namespaces in your file. Check this URL:
>
> http://tenderlovemaking.com/2009/04/23/namespaces-in-xml/
>
> In order to make this work, you can do something like this:
>
> require 'nokogiri'
>
> doc = Nokogiri::XML(File.read("one.xml"))

The alternative is

doc = File.open("one.xml") {|io| Nokogiri::XML(io)}

> doc.collect_namespaces.each {|key,value| puts "#{key} => #{value}"}
> doc.css("uosNS|IfcCartesianPoint uosNS|Coordinates
> uosNS|IfcLengthMeasure", {"uosNS" =>
> "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL"}).each {|el| puts
> el.text}
>
> (I added a line that shows all namespaces in the document). All nodes
> under the uos node inherit the namespace referenced by the url you see
> in the code, so in order to search for nodes within the uos node, you
> need to specify the namespace.
>
>> Is there an advantage to using .open vs .read?
>
> read reads the whole file in memory. Passing a file handler to
> nokogiri will probably make no difference, because most likely it's
> reading the full file to memory too.

I will have to read the whole file but it may make a crucial
difference whether it does so in one go or in chunks.  Large files
might not even be readable with the File.read approach.  If you pass
the file as a single string there is no choice but if you pass the
File instance nokogiri can decide what to do.  This is more efficient.
 Note also that because of buffering small files will have just one
(or a few) IO operations anyway.

>> 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?
>
> The only answer to this question is to benchmark.

I don't think the file loading influences access speed.  Once the file
is loaded into a object structure IO is over and all operations are in
memory plus the model of the file will be the same regardless whether
you read in one big chunk or in smaller ones.

The two approaches to loading the file do most likely have different
performance characteristics though.

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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