使用XMLStarlet在bash中解析xml并以$ {channel}格式保存结果

时间:2013-10-02 03:08:12

标签: xml linux bash rss

所以,我将在bash中使用xmlstarlet作为其xml解析器编写rss剪贴板,并将wget作为其rss下载器(我完成了wget部分,所以没有问题),但我有一个问题解析每个使用它的Feed中的项目,并使用以下格式保存:$ {channel}等(我已经将它存储在echo -e格式中(我的意思是,将其显示为stdout),现在我想解析每个项目使用XMLStarlet的feed并以格式保存它:$ {channel}等等,简而言之,我将在C#中做类似的事情:

//ignore the classes, they're just illustrations    
public class RSSClass
    {
        public string Title { get; set; } //definition
        public string PubDate { get; set; }
    }


    void myRSS_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{

        var rssData = from rss in XElement.Parse(e.Result).Descendants("item")


         select new RSSClass
                    {
                        Title = rss.Element("title").Value,   //this stores the value in <title/> in the respective string named Title
                        PubDate = rss.Element("pubDate").Value  //this is also done the same thing 
                    };

}

我不知道如何在bash中做同样的事情因为不知道什么是bash(我真的是bash中的新人)

更新:我在bash中写了一些sekeleton代码(虽然我仍然没有弄清楚bash xml解析的东西):

#!/bin/bash

RSS_URI="http://rss.detik.com/index.php/hot"
XPATH_MATCH=/rss/channel/item
XPATH_VALUE="concat(pubDate,' | ',title,'\n')"
RSS_DATA="data.xml"
${channel}
${pubdate}
${title}
${link}

DOWNLOAD_AND_PARSE ()   #Downloads and parses RSS [SKELETON]
{
    wget -O RSS_DATA RSS_URI
    echo -e "$(~/bin/xml sel --net -t -m "$XPATH_MATCH" -v "$XPATH_VALUE" "$RSS_DATA")" | awk -F "|" '{print $1 $2}'   
}

我不知道该怎么办:|

提前感谢您的解决方案/答案/等

编辑:我刚刚发现了如何将输出分开,现在关于如何以$ {}格式存储它的问题仍然是一个问题

0 个答案:

没有答案