代码之家  ›  专栏  ›  技术社区  ›  Steinbitglis

有没有办法选择一个节点的所有内容?

  •  1
  • Steinbitglis  · 技术社区  · 16 年前

    有没有办法选择Nokogiri中节点的所有内容?

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

    获取内容的结果 /root/element 应该是:

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

    编辑:

    myElement.inner_html() . 我遇到的问题是,实际上我依赖的是旧版本的libxml2,它逃逸了所有特殊字符。

    2 回复  |  直到 13 年前
        1
  •  0
  •   the Tin Man    13 年前
    Nokogiri.parse('<root><element>this is <hi>the content</hi> of my element</element></root>').css('element').inner_html
    

    如果你想逃避,你可以 CGI.unescape 方法:

    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)
    
        2
  •  0
  •   the Tin Man    13 年前

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