0

I have out put like below and i want to search for a particular string and it's value how to do this in rails.

ex : i want search for Nokogiri::XML::Text pattern and get all pattern matching Nokogiri::XML::Text

#<LinkedIn::Location:0x4a339e8 @doc=#<Nokogiri::XML::Document:0x2519cb8 name="document" children=[#<Nokogiri::XML::Element:0x2519a0c name="per
son" children=[#<Nokogiri::XML::Text:0x25197f0 "\n  ">, #<Nokogiri::XML::Element:0x251970c name="`" children=[#<Nokogiri::XML::Text:0x2
519124 "\n    ">, #<Nokogiri::XML::Element:0x25190f4 name="name" children=[#<Nokogiri::XML::Text:0x2518bd8 "Bengaluru Area, India">]>, #<Nokog
iri::XML::Text:0x2518b00 "\n    ">, #<Nokogiri::XML::Element:0x2518ad0 name="country" children=[#<Nokogiri::XML::Text:0x2518680 "\n      ">, #
<Nokogiri::XML::Element:0x2518650 name="code" children=[#<Nokogiri::XML::Text:0x2517fcc "in">]>, #<Nokogiri::XML::Text:0x2517eb8 "\n    ">]>,
#<Nokogiri::XML::Text:0x2517cd8 "\n  ">]>, #<Nokogiri::XML::Text:0x2517bdc "\n">]>]>>
Joe
  • 4,460
  • 19
  • 60
  • 106
  • I think you can parse nokogiri response further either and at the end u can use to_s or api methods that can help u to find out value. – Amar Apr 12 '12 at 12:47
  • Amar thanks for the response i have been trying to what you have commented for two days but ended up getting nothing. Please help me. – Joe Apr 12 '12 at 12:49
  • http://stackoverflow.com/questions/10122342/how-to-convert-nokogiri-object-to-xml-file-in-rails please see the above question's discussion for further information – Joe Apr 12 '12 at 12:50
  • In what way you are parsing can u give me the idea what u are doing might be i checked locally – Amar Apr 12 '12 at 12:52
  • i don't even know how to parse this. when i put client.profile.to_s than i get the above output but when i try to print client.profile than the out put i get is so don't know how to proceed further. please help. – Joe Apr 12 '12 at 12:54
  • don't use to_s doc=client.profile and then traverse children recursive way on each node check node.class == Nokogiri::XML::Text then do to_s and save result into some variable or in this case node also has children so traverse those children.Which gem u are using for fetching linked in profile – Amar Apr 12 '12 at 13:26
  • thanks i will check it , i am using linkedin gem to get user profile. i am sorry to ask you could you provide some sample if possible? – Joe Apr 12 '12 at 13:33

1 Answers1

1

You can get the value like :

Way-1 :

reader = Nokogiri::XML::Reader(xml)
reader.read #Moves to next node in document
reader.attribute("cdn") # To get the value of attributes

Way-2 :

doc = Nokogiri::XML(xml)
elems = doc.xpath("//*[@messageId]") #get all elements with an attribute of 'messageId'
elems[0].attr('messageId') #gets value of attribute of first elem
Vik
  • 5,931
  • 3
  • 31
  • 38
  • undefined method `empty?' for # this is the error i get – Joe Apr 12 '12 at 13:54
  • I am sorry Vik i was on leave – Joe Apr 16 '12 at 08:32
  • I am not seeing any code of Nokogiri , you are using Xpath to parse xml . Wanting to suggest you that get the xml and try to parse it on console with Nokogiri . – Vik Apr 16 '12 at 09:25