正则表达式解析 XML 文件并仅返回文件扩展名的 url

本文关键字:文件 返回 扩展名 url XML 正则表达式 | 更新日期: 2023-09-27 18:37:15

我有以下 xml 文件转换为字符串我想解析 xml 文件,只返回带有文件扩展名的网址

<?xml version="1.0" encoding="utf-8"?>
<channel>
  <generator>GOA Celebrations</generator>
  <generator>GOA Celebrations</generator>
  <pubDate>13 Jan 2016</pubDate>
  <title>GOA Celebrations</title>
  <link>http://goframe.com.au/rss/herston-inbound</link>
  <language>en</language>
  <image>
    <url>http://goafame.com.au/site/templates/img/logo.png</url>
  </image>
  <item>
    <title>Herston Inbound - Wednesday, 13 Jan - Alex Peisker - 2015-12-21 09:26am - Live</title>
    <description></description>
    <pubDate>13 Jan 2016 10:32:01 AEST</pubDate>
    <link>http://goafame.com.au/gallery/entry/2991/</link>
    <enclosure url="http://goafame.com.au/site/assets/files/2991/final_cropped_shutterstock_114908098.jpg" length="" type="" />
    <guid isPermaLink="false">c5c1cb0bebd56ae38817b251ad72bedb</guid>
  </item>
  <item>
    <title>Dog 1</title>
    <description></description>
    <pubDate>13 Jan 2016 10:32:01 AEST</pubDate>
    <link>http://goafame.com.au/gallery/entry/2991/</link>
    <enclosure url="http://animaliaz-life.com/data_images/dog/dog4.jpg" length="" type="" />
  </item>
  <item>
    <title>Dog 2</title>
    <description></description>
    <pubDate>13 Jan 2016 10:32:01 AEST</pubDate>
    <link>http://goafame.com.au/gallery/entry/2991/</link>
    <enclosure url="http://cdn1.theodysseyonline.com/files/2015/12/21/6358631429926013411708851658_Dog-Pictures.jpg" length="" type="" />
  </item>
  <item>
    <title>Dog 3</title>
    <description></description>
    <pubDate>13 Jan 2016 10:32:01 AEST</pubDate>
    <link>http://goafame.com.au/gallery/entry/2991/</link>
    <enclosure url="https://i.ytimg.com/vi/AkcfB3z0_-0/maxresdefault.jpg" length="" type="" />
  </item>
</channel>

我的问题是如何使用

定期锻炼

所需的输出将是

http://goafame.com.au/site/templates/img/logo.pnghttp://goafame.com.au/site/assets/files/2991/final_cropped_shutterstock_114908098.jpghttp://animaliaz-life.com/data_images/dog/dog4.jpghttp://cdn1.theodysseyonline.com/files/2015/12/21/6358631429926013411708851658_Dog-Pictures.jpg

https://i.ytimg.com/vi/AkcfB3z0_-0/maxresdefault.jpg

这是我到目前为止尝试过的

Regex linkParser = new Regex(@"'b(?:https?://|www'.)'S+'b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                string rawString = doc.ToString();
                int posCounter = 0;
                foreach (Match m in linkParser.Matches(rawString))
                {
                    posCounter++;
                    links.Add(new LinkModel
                    {
                        IsSelected = false,
                        XmlLink = m.Value,
                        NodePosition = posCounter
                    });
                }

注意:XML 文件可以来自任何来源,并且其他一些 url 不在链接元素中。有些甚至是嵌套的。这就是为什么我想使用正则表达式而不是XDocument。

正则表达式解析 XML 文件并仅返回文件扩展名的 url

我的模式可以匹配您的样本。在这里测试 http://regexstorm.net/tester

https?://[^'s<"]+/[^'s<"]+(?:'.'w{3,4})

这个想法是查找具有启动字符 (/) 后跟文件名模式(以 3,4 个字符扩展名结尾)的所有链接。

var allLinkValues = XDocument.Parse(doc.ToString())
                             .Root
                             .Elements("item")
                             .Select(itemElement => itemElement.Element("link").Value)
                             .ToList();

这里

XDocument.Parse(doc.ToString())加载文档。 Root指向根元素然后我们选择所有"项目"元素并选择"链接"元素的值。

Linq2Xml的力量!

XPath,XmlDocument是您的其他选择。

一般来说,如果字符串方案良好,(XML,JSON,RDF等)不要选择正则表达式作为第一个选项。对于这些类型的文档,有定义良好的解析器。

上面的查询应该可以帮助您开始使用 Xml 导航。