[Powershell] RSS Feeds ausgeben, Text aus XML TextNodes / Attribute auslesen

Thomas Darimont

Erfahrenes Mitglied
Hallo,

hier mal ein kleines Beispiel wie man mit Powershell die neuesten Artikel RSS Feeds auslesen kann:

Beispiel feed:
XML:
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:re="http://purl.org/atompub/rank/1.0">
    <title type="text">Recent Questions - Stack Overflow</title>
    <link rel="self" href="http://stackoverflow.com/feeds" type="application/atom+xml" />
    <link rel="alternate" href="http://stackoverflow.com/questions" type="text/html" />
    <subtitle>most recent 30 from stackoverflow.com</subtitle>
    <updated>2010-04-23T14:38:00Z</updated>
    <id>http://stackoverflow.com/feeds</id>

    <creativeCommons:license>http://www.creativecommons.org/licenses/by-nc/2.5/rdf</creativeCommons:license> 

    <entry>
        <id>http://stackoverflow.com/questions/2697838/jquery-and-regex-for-adding-icons-to-specific-links</id>
        <re:rank scheme="http://stackoverflow.com">1</re:rank>
        <title type="text">jQuery and regex for adding icons to specific links?!</title>
        <category scheme="http://stackoverflow.com/feeds/tags" term="jquery"/><category scheme="http://stackoverflow.com/feeds/tags" term="regex"/>
        <author>
            <name>rayne</name>
            <uri>http://stackoverflow.com/users/271835</uri>
        </author>
        <link rel="alternate" href="http://stackoverflow.com/questions/2697838/jquery-and-regex-for-adding-icons-to-specific-links" />
        <published>2010-04-23T10:37:50Z</published>
        <updated>2010-04-23T14:37:53Z</updated>
        <summary type="html">
...
        </summary>
    </entry>
	<entry>
...

Code:
$stackoverflow = [ xml ] (new-object net.webclient).DownloadString("http://stackoverflow.com/feeds")
$stackoverflow.feed.entry | %{$_.title.InnerText, $_.link.href}

Dabei stehen die Symbole jeweils für:
[ xml ] => Wandle den String in ein XML Dokument um
| => Pipe (Übergabe von einem Datenstrom an einen "Prozessor"
% => foreach
$_ => aktuelles Element der Iteration

Mit .InnerText greif wir auf den Inhalt des TextNodes (title) zu. Die Link url bekommen wir aus dem href-Attribute des link Elements (link.href)

Beispielausgabe:
Code:
Any ideas why this wont print out
http://stackoverflow.com/questions/2699009/any-ideas-why-this-wont-print-out
How to pass data from a web page to an application?
http://stackoverflow.com/questions/2699527/how-to-pass-data-from-a-web-page-to-an-application
PHP OOP: Avoid Singleton/Static Methods in Domain Model Pattern
http://stackoverflow.com/questions/2697504/php-oop-avoid-singleton-static-methods-in-domain-model-pattern
...

Gruß Tom
 
Zuletzt bearbeitet von einem Moderator:

Neue Beiträge

Zurück