With regex how do i match between an XML tag multiple times?

后端 未结 5 1590
再見小時候
再見小時候 2020-12-21 11:41

First, before you say anything, i HAVE to do this because the RSS is malformed, but i can\'t correct it on my end. So, while I tried using an RSS and a XML parser, they fail

5条回答
  •  鱼传尺愫
    2020-12-21 12:13

    Many parsers can handle slight deviations from the specs. Any binding to the excellent libxml2 library would be able to handle poorly formed XML. There are bindings in many languages. For example, the following Ruby snippet parses it just fine:

    require 'nokogiri'
    
    xml = open('rss.txt').read
    doc = Nokogiri::XML.parse(xml)
    doc.xpath('//title').each do |title|
      puts title.inner_text
    end
    

    Result:

    "joint terrorism task force" location:oregon - Google News
    "joint terrorism task force" location:oregon - Google News
    Federal and FBI Joint Terrorism Task Force are still flawed - OregonLive.com
    Striking a fair balance - OregonLive.com
    Blame the terrorists, not the FBI - Portland Tribune
    Why Oregon? Why not?: Terrorism can strike anywhere - The Register-Guard
    INDIVIDUAL TRAVEL UNDER ATTACK - NewsWithViews.com
    The other terrorism-and pondering Portland - BlueOregon
    Fla. dance troupe causes scare at Lincoln Tunnel - Northwest Cable News
    

    Edit: based on your comments I see you're using jQuery. You should be able to use a jQuery XML parser to extract the titles (and other parts, as needed).

提交回复
热议问题