开发者

Is there a way to select all the contents of a node?

开发者 https://www.devze.com 2022-12-24 01:54 出处:网络
Is there a way to select all the contents of a node in Nokogiri? <开发者_开发技巧root> <element>this is <hi>the content</hi> of my æøå element</element>

Is there a way to select all the contents of a node in Nokogiri?

<开发者_开发技巧root>
    <element>this is <hi>the content</hi> of my æøå element</element>
</root>

The result of getting the content of /root/element should be:

this is <hi>the content</hi> of my æøå element

Edit:

It seems like the solution is simply to use myElement.inner_html(). The problem I had was in fact that I was relying on an old version of libxml2, which escaped all the special characters.


Nokogiri.parse('<root><element>this is <hi>the content</hi> of my element</element></root>').css('element').inner_html

If you want escape that, you can with CGI.unescape method:

require 'cgi'
x = Nokogiri.parse('<root><element>this is <hi>the content</hi> of my element</element></root>').css('element').inner_html
CGI.unescape(x)


I think the previous answer is assuming HTML. I'm not sure that's appropriate, so here's my (similar) answer:

require 'nokogiri'
xml = '<root><element>this is <hi>the content</hi> of my æøå element</element></root>' 
p Nokogiri(xml).at('element').to_xml
0

精彩评论

暂无评论...
验证码 换一张
取 消