Input appreciated: Trying to get data from a CDATA field out of an xml doc
using Nokogiri.
This is the node:
<ATTACHED_DOCUMENT>
<![CDATA[insert PDF code here]]>
</ATTACHED_DOCUMENT>
When I query I am getting at best empty string, where I should expect the
text "insert PDF code here". This is what I have tried, without
success:
xml_request_doc.xpath("//attached_document")
returns: [#<Nokogiri::XML::Element:0x1252bfe
name="attached_document">]
xml_request_doc.xpath("//attached_document/text()")
returns: []
xml_request_doc.xpath("//attached_document").value
returns: NoMethodError Exception: undefined method `value'' for
[#<Nokogiri::XML::Element:
xml_request_doc.xpath("//attached_document").inner_html
returns: ""
Any thoughts where going wrong (I am successfully using Nokogiri for every
other element in my doc.
Thx!
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
One way would be to check the children until you get the CData node:
parent_node = xml_request_doc.xpath("//attached_document")
# this assumes parent_node is not nil
cdata_node = parent_node.children.detect {|n| n.cdata?}
# i think you can then just do:
cdata_node.value
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Fri, Aug 13, 2010 at 2:15 PM, Gary Winklosky <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > One way would be to check the children until you get the CData node: > > > parent_node = xml_request_doc.xpath("//attached_document") > > # this assumes parent_node is not nil > cdata_node = parent_node.children.detect {|n| n.cdata?}Thanks Gary, I had been trying the .children, and tried what you suggested to be sure, and I get nil as the return value for cdata_node = parent_node.children.detect {|n| n.cdata?} It is very strange - the following is the xml in text before creating the Nokogiri doc, and it says <ATTACHED_DOCUMENT> has no children, and does not seem to be any other objects inside the attached_document node in the Nokogiri doc: <USER_INFO login=\"aaa\" password=\"bbb\" /> <REQUEST request_type=\"NEW\"> <SUPPLEMENT_ORDER_DETAIL report_id=\"ccc\" ordered_by=\"ddd\" phone=\"eee\" email=\"fff\" faxed_docs=\"ggg\" rush=\"T\" reason=\"hhh\"> <TRADE_SUPPLEMENT account_number=\"iii\" creditor=\"jjj\" included_bankruptcy=\"T\"> <ATTACHED_DOCUMENT> <![CDATA[zzz]]> </ATTACHED_DOCUMENT> </TRADE_SUPPLEMENT> </SUPPLEMENT_ORDER_DETAIL> </REQUEST>" -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Gary Winklosky
2010-Aug-13 19:54 UTC
Re: Re: Can not get CDATA out of XML node using Nokogiri
maybe try .cdatas on the parent node rather than children. Not sure about Nokogiri but such a method exists for rexml/document. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Kahn
2010-Aug-13 22:24 UTC
Re: Re: Re: Can not get CDATA out of XML node using Nokogiri
On Fri, Aug 13, 2010 at 3:54 PM, Gary Winklosky <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> maybe try .cdatas on the parent node rather than children. Not sure > about Nokogiri but such a method exists for rexml/document.Thanks. It does work with REXML, out of the box, no fiddling. I like Nokogiri''s speed but am finding it harder to work with - maybe it is just getting used to and I don''t understand things, but aside from this what else I find a little strange is when Nokogiri matches on a node/element it wants everything in lowercase, even if my xml is uppercase. Anyhow +1 for old REXML.> -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Kahn
2010-Aug-14 17:35 UTC
Re: Re: Re: Can not get CDATA out of XML node using Nokogiri
> maybe try .cdatas on the parent node rather than children. Not sure > about Nokogiri but such a method exists for rexml/document.This is beautiful in REXML, just give me what I want, no negotiation -- back to the joy of Rails: REXML::XPath.first(xml_request_doc, "//ATTACHED_DOCUMENT").cdatas On Fri, Aug 13, 2010 at 3:54 PM, Gary Winklosky <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> maybe try .cdatas on the parent node rather than children. Not sure > about Nokogiri but such a method exists for rexml/document. > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.