is there anybody who knows how we can get child values using libxml (Reader class) def xml_import_to_brands require''xml'' reader = XML::Reader.file(''c:/brands.xml'') . . .
i tried this an get an error NoMemoryError failed to allocate memory error line number 30 = stack << reader.name On Jun 2, 6:59 pm, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote:> Quoting lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > is there anybody who knows how we can get child values using libxml > > (Reader class) > > > def xml_import_to_brands > > require''xml'' > > reader = XML::Reader.file(''c:/brands.xml'') > > . > > For docs, go tohttp://libxml.rubyforge.org/rdoc/index.html, scroll down in > middle, left windows to LibXML::XML::Reader. > > Something along the lines of: > > require ''xml'' > > stack = [] > while reader > case reader.node_type > when XML::Reader::TYPE_ELEMENT > # opening tag > stack << reader.name > stack.pop if reader.empty_element? > when XML::Reader::TYPE_TEXT, XML::Reader::TYPE_CDATA > path = stack.join(''/'') > case path > when ''root/parent/child'' > puts reader.value # simple content > when ''root/parent/complex_content'' > puts reader.read_inner_xml # contents including markup > when XML::Reader::TYPE_END_ELEMENT > # closing tag > stack.pop > end > end > > HTH, > Jeffrey
Have not seen this error before. Please show more of your code. Jeffrey Quoting lecielbleu <canali83-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > i tried this an get an error > NoMemoryError > failed to allocate memory > error line number 30 = stack << reader.name
my xml data seems like that : <Brands><Brand><Brand_id>2</ Brand_id><Brand_name>HP</Brand_name></Brand> and here is my codes def xml_testing require''xml'' reader = XML::Reader.file(''c:/brands.xml'') stack = [] while reader case reader.node_type when XML::Reader::TYPE_ELEMENT # opening tag stack << reader.name stack.pop if reader.empty_element? when XML::Reader::TYPE_TEXT, XML::Reader::TYPE_CDATA path = stack.join(''/'') case path when ''Brands/Brand/Brand_name'' puts reader.value # simple content when XML::Reader::TYPE_END_ELEMENT # closing tag stack.pop end end end flash.now[:message]="brands read" end Note : i add one more "end" cause it was saying unexpected end On Jun 3, 3:33 pm, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote:> Have not seen this error before. Please show more of your code. > > Jeffrey > > Quoting lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > i tried this an get an error > > NoMemoryError > > failed to allocate memory > > error line number 30 = stack << reader.name
There are no <xml> header tags. I have no idea how libXML will behave. It isn''t very tolerant of invalid input. Try adding the puts statements I''ve inserted below to find out how far it is getting. Quoting lecielbleu <canali83-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > my xml data seems like that : <Brands><Brand><Brand_id>2</ > Brand_id><Brand_name>HP</Brand_name></Brand> > and here is my codes > > def xml_testing > require''xml'' > reader = XML::Reader.file(''c:/brands.xml'') > > stack = [] > while reader > case reader.node_type > when XML::Reader::TYPE_ELEMENT > # opening tag > stack << reader.nameputs "BEGIN #{stack.join(''/'')}"> stack.pop if reader.empty_element?puts "END #{stack.join(''/'')}" if reader.empty_element?> when XML::Reader::TYPE_TEXT, XML::Reader::TYPE_CDATA > path = stack.join(''/'') > case path > when ''Brands/Brand/Brand_name'' > puts reader.value # simple content > when XML::Reader::TYPE_END_ELEMENT > # closing tagputs "END #{stack.join(''/'')}"> stack.pop > end > end > end > > flash.now[:message]="brands read" > end
i guess problem is while reader line because when i run basic code below : while doc == true puts reader.read_string end strings never stops in the command line , it turns infinite loop. On Jun 4, 12:48 am, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote:> There are no <xml> header tags. I have no idea how libXML will behave. It > isn''t very tolerant of invalid input. Try adding the puts statements I''ve > inserted below to find out how far it is getting. > > Quoting lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > my xml data seems like that : <Brands><Brand><Brand_id>2</ > > Brand_id><Brand_name>HP</Brand_name></Brand> > > and here is my codes > > > def xml_testing > > require''xml'' > > reader = XML::Reader.file(''c:/brands.xml'') > > > stack = [] > > while reader > > case reader.node_type > > when XML::Reader::TYPE_ELEMENT > > # opening tag > > stack << reader.name > > puts "BEGIN #{stack.join(''/'')}"> stack.pop if reader.empty_element? > > puts "END #{stack.join(''/'')}" if reader.empty_element?> when XML::Reader::TYPE_TEXT, XML::Reader::TYPE_CDATA > > path = stack.join(''/'') > > case path > > when ''Brands/Brand/Brand_name'' > > puts reader.value # simple content > > when XML::Reader::TYPE_END_ELEMENT > > # closing tag > > puts "END #{stack.join(''/'')}" > > > stack.pop > > end > > end > > end > > > flash.now[:message]="brands read" > > end
i tried also while doc == true while doc = 1 while doc results dont change.... On Jun 4, 12:51 pm, lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> i guess problem is while reader line because when i run basic code > below : > > while doc == true > puts reader.read_string > end > > strings never stops in the command line , it turns infinite loop. > > On Jun 4, 12:48 am, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote: > > > There are no <xml> header tags. I have no idea how libXML will behave. It > > isn''t very tolerant of invalid input. Try adding the puts statements I''ve > > inserted below to find out how far it is getting. > > > Quoting lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > my xml data seems like that : <Brands><Brand><Brand_id>2</ > > > Brand_id><Brand_name>HP</Brand_name></Brand> > > > and here is my codes > > > > def xml_testing > > > require''xml'' > > > reader = XML::Reader.file(''c:/brands.xml'') > > > > stack = [] > > > while reader > > > case reader.node_type > > > when XML::Reader::TYPE_ELEMENT > > > # opening tag > > > stack << reader.name > > > puts "BEGIN #{stack.join(''/'')}"> stack.pop if reader.empty_element? > > > puts "END #{stack.join(''/'')}" if reader.empty_element?> when XML::Reader::TYPE_TEXT, XML::Reader::TYPE_CDATA > > > path = stack.join(''/'') > > > case path > > > when ''Brands/Brand/Brand_name'' > > > puts reader.value # simple content > > > when XML::Reader::TYPE_END_ELEMENT > > > # closing tag > > > puts "END #{stack.join(''/'')}" > > > > stack.pop > > > end > > > end > > > end > > > > flash.now[:message]="brands read" > > > end
I''m afraid I don''t understand what you are trying to do here. ''doc'' isn''t in the example below. So how the loop would change its value, I am unclear. It seems like you are saying a loop with an unchanging condition runs forever. Yes, it does. Did you intend to do this: while reader.read puts reader.read_string end HTH Jeff Quoting lecielbleu <canali83-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > i tried also > while doc == true > while doc = 1 > while doc > > results dont change.... > > On Jun 4, 12:51 pm, lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > i guess problem is while reader line because when i run basic code > > below : > > > > while doc == true > > puts reader.read_string > > end > > > > strings never stops in the command line , it turns infinite loop. > > > > On Jun 4, 12:48 am, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote: > > > > > There are no <xml> header tags. I have no idea how libXML will behave. It > > > isn''t very tolerant of invalid input. Try adding the puts statements I''ve > > > inserted below to find out how far it is getting. > > > > > Quoting lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > > my xml data seems like that : <Brands><Brand><Brand_id>2</ > > > > Brand_id><Brand_name>HP</Brand_name></Brand> > > > > and here is my codes > > > > > > def xml_testing > > > > require''xml'' > > > > reader = XML::Reader.file(''c:/brands.xml'') > > > > > > stack = [] > > > > while reader > > > > case reader.node_type > > > > when XML::Reader::TYPE_ELEMENT > > > > # opening tag > > > > stack << reader.name > > > > > puts "BEGIN #{stack.join(''/'')}"> stack.pop if reader.empty_element? > > > > > puts "END #{stack.join(''/'')}" if reader.empty_element?> when XML::Reader::TYPE_TEXT, XML::Reader::TYPE_CDATA > > > > path = stack.join(''/'') > > > > case path > > > > when ''Brands/Brand/Brand_name'' > > > > puts reader.value # simple content > > > > when XML::Reader::TYPE_END_ELEMENT > > > > # closing tag > > > > > puts "END #{stack.join(''/'')}" > > > > > > stack.pop > > > > end > > > > end > > > > end > > > > > > flash.now[:message]="brands read" > > > > end > >
ok Jeff i solved problem , using "doc" was a bad mistake :) thanks for your support ,here is the last codes while reader.read case reader.name when "Brand_name" name_from_xml = reader.read_string Brand.create( :name => name_from_xml ) end end ---------------------------- now i have to solve another problem :) do you know how can i get a brand_name using filter brand_id while reader.read case reader.read_string when 55 -------magic line i couldnt find :) ---------- end end magic line must be get brand_name value "Dell" using "55" Sample xml code ===>> <Brand><Brand_id>55</Brand_id><Brand_name>Dell</ Brand_name></Brand> On Jun 4, 9:36 pm, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote:> I''m afraid I don''t understand what you are trying to do here. ''doc'' isn''t in > the example below. So how the loop would change its value, I am unclear. It > seems like you are saying a loop with an unchanging condition runs forever. > Yes, it does. > > Did you intend to do this: > > while reader.read > puts reader.read_string > end > > HTH > Jeff > > Quoting lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > i tried also > > while doc == true > > while doc = 1 > > while doc > > > results dont change.... > > > On Jun 4, 12:51 pm, lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > i guess problem is while reader line because when i run basic code > > > below : > > > > while doc == true > > > puts reader.read_string > > > end > > > > strings never stops in the command line , it turns infinite loop. > > > > On Jun 4, 12:48 am, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote: > > > > > There are no <xml> header tags. I have no idea how libXML will behave. It > > > > isn''t very tolerant of invalid input. Try adding the puts statements I''ve > > > > inserted below to find out how far it is getting. > > > > > Quoting lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > > my xml data seems like that : <Brands><Brand><Brand_id>2</ > > > > > Brand_id><Brand_name>HP</Brand_name></Brand> > > > > > and here is my codes > > > > > > def xml_testing > > > > > require''xml'' > > > > > reader = XML::Reader.file(''c:/brands.xml'') > > > > > > stack = [] > > > > > while reader > > > > > case reader.node_type > > > > > when XML::Reader::TYPE_ELEMENT > > > > > # opening tag > > > > > stack << reader.name > > > > > puts "BEGIN #{stack.join(''/'')}"> stack.pop if reader.empty_element? > > > > > puts "END #{stack.join(''/'')}" if reader.empty_element?> when XML::Reader::TYPE_TEXT, XML::Reader::TYPE_CDATA > > > > > path = stack.join(''/'') > > > > > case path > > > > > when ''Brands/Brand/Brand_name'' > > > > > puts reader.value # simple content > > > > > when XML::Reader::TYPE_END_ELEMENT > > > > > # closing tag > > > > > puts "END #{stack.join(''/'')}" > > > > > > stack.pop > > > > > end > > > > > end > > > > > end > > > > > > flash.now[:message]="brands read" > > > > > end
Glad it is working. Jeff Quoting lecielbleu <canali83-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > ok Jeff i solved problem , using "doc" was a bad mistake :) thanks for > your support ,here is the last codes > > while reader.read > case reader.name > when "Brand_name" > name_from_xml = reader.read_string > Brand.create( :name => name_from_xml ) > end > end > > ---------------------------- > > now i have to solve another problem :) do you know how can i get a > brand_name using filter brand_id > > while reader.read > case reader.read_string > when 55 > -------magic line i couldnt find :) > ---------- > end > end > > magic line must be get brand_name value "Dell" using "55" > > Sample xml code ===>> <Brand><Brand_id>55</Brand_id><Brand_name>Dell</ > Brand_name></Brand> > > > On Jun 4, 9:36 pm, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote: > > I''m afraid I don''t understand what you are trying to do here. ''doc'' isn''t in > > the example below. So how the loop would change its value, I am unclear. It > > seems like you are saying a loop with an unchanging condition runs forever. > > Yes, it does. > > > > Did you intend to do this: > > > > while reader.read > > puts reader.read_string > > end > > > > HTH > > Jeff > > > > Quoting lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > > > > > i tried also > > > while doc == true > > > while doc = 1 > > > while doc > > > > > results dont change.... > > > > > On Jun 4, 12:51 pm, lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > i guess problem is while reader line because when i run basic code > > > > below : > > > > > > while doc == true > > > > puts reader.read_string > > > > end > > > > > > strings never stops in the command line , it turns infinite loop. > > > > > > On Jun 4, 12:48 am, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote: > > > > > > > There are no <xml> header tags. I have no idea how libXML will behave. It > > > > > isn''t very tolerant of invalid input. Try adding the puts statements I''ve > > > > > inserted below to find out how far it is getting. > > > > > > > Quoting lecielbleu <canal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > > > > my xml data seems like that : <Brands><Brand><Brand_id>2</ > > > > > > Brand_id><Brand_name>HP</Brand_name></Brand> > > > > > > and here is my codes > > > > > > > > def xml_testing > > > > > > require''xml'' > > > > > > reader = XML::Reader.file(''c:/brands.xml'') > > > > > > > > stack = [] > > > > > > while reader > > > > > > case reader.node_type > > > > > > when XML::Reader::TYPE_ELEMENT > > > > > > # opening tag > > > > > > stack << reader.name > > > > > > > puts "BEGIN #{stack.join(''/'')}"> stack.pop if reader.empty_element? > > > > > > > puts "END #{stack.join(''/'')}" if reader.empty_element?> when XML::Reader::TYPE_TEXT, XML::Reader::TYPE_CDATA > > > > > > path = stack.join(''/'') > > > > > > case path > > > > > > when ''Brands/Brand/Brand_name'' > > > > > > puts reader.value # simple content > > > > > > when XML::Reader::TYPE_END_ELEMENT > > > > > > # closing tag > > > > > > > puts "END #{stack.join(''/'')}" > > > > > > > > stack.pop > > > > > > end > > > > > > end > > > > > > end > > > > > > > > flash.now[:message]="brands read" > > > > > > end > >