Can not get CDATA out of XML node using Nokogiri

Input appreciated: Trying to get data from a CDATA field out of an xml
doc
using Nokogiri.

This is the node:
<ATTACHED_DOCUMENT>

</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!

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

On Fri, Aug 13, 2010 at 2:15 PM, Gary W. [email protected]
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> </ATTACHED_DOCUMENT>
</TRADE_SUPPLEMENT>
</SUPPLEMENT_ORDER_DETAIL>
"

maybe try .cdatas on the parent node rather than children. Not sure
about Nokogiri but such a method exists for rexml/document.

On Fri, Aug 13, 2010 at 3:54 PM, Gary W. [email protected]
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.

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