Benutzer mit den meisten Antworten
Quellcode entnehmen und damit XML-Datei erzeugen

Frage
-
Hallo,
das Szenario mag etwas komisch klingen, aber ich würde sehr gerne den Quellcode einer Seite, der schon einem XML-File entspricht, entnehmen und damit eine jeweils neue XML-Datei erzeugen.
Es handelt sich hierbei um eine Abfrage bei der YahooWeatherAPI
Beispielausschnitt:
<?xml version="1.0" encoding="UTF-8"?>
-<item> <title>Conditions for Nome, AK, US at 02:00 AM AKDT</title> <geo:lat xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">64.499474</geo:lat> <geo:long xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">-165.405792</geo:long> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2460286/</link> <pubDate>Tue, 19 Jul 2016 02:00 AM AKDT</pubDate> <yweather:condition xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" text="Cloudy" temp="50" date="Tue, 19 Jul 2016 02:00 AM AKDT" code="26"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" text="Rain" date="19 Jul 2016" code="12" low="50" high="51" day="Tue"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" text="Showers" date="20 Jul 2016" code="11" low="49" high="50" day="Wed"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" text="Cloudy" date="21 Jul 2016" code="26" low="50" high="56" day="Thu"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" text="Mostly Cloudy" date="22 Jul 2016" code="28" low="52" high="63" day="Fri"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" text="Cloudy" date="23 Jul 2016" code="26" low="50" high="55" day="Sat"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" text="Showers" date="24 Jul 2016" code="11" low="45" high="53" day="Sun"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" text="Partly Cloudy" date="25 Jul 2016" code="30" low="44" high="56" day="Mon"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" text="Scattered Showers" date="26 Jul 2016" code="39" low="49" high="55" day="Tue"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" text="Rain" date="27 Jul 2016" code="12" low="47" high="55" day="Wed"/> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" text="Rain" date="28 Jul 2016" code="12" low="43" high="55" day="Thu"/> <description><![CDATA[<img src="http://l.yimg.com/a/i/us/we/52/26.gif"/> <BR /> <b>Current Conditions:</b> <BR />Cloudy <BR /> <BR /> <b>Forecast:</b> <BR /> Tue - Rain. High: 51Low: 50 <BR /> Wed - Showers. High: 50Low: 49 <BR /> Thu - Cloudy. High: 56Low: 50 <BR /> Fri - Mostly Cloudy. High: 63Low: 52 <BR /> Sat - Cloudy. High: 55Low: 50 <BR /> <BR /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2460286/">Full Forecast at Yahoo! Weather</a> <BR /> <BR /> (provided by <a href="http://www.weather.com" >The Weather Channel</a>) <BR /> ]]></description> <guid isPermaLink="false"/> </item>
Den kompletten Aufbau, mit allen Knoten und Informationen, würde ich gerne 1 zu 1 entnehmen wollen, um damit weiterarbeiten zu können.
Ich habe damit angefangen, den Quellcode auszulesen. Dafür habe ich die Funktion "Get-Web" von James Brundage verwendet.
https://technet.microsoft.com/en-us/library/ee176824.aspx - [Quelle]
function Get-Web($url, [switch]$self, $credential, $toFile, [switch]$bytes) { $webclient = New-Object Net.Webclient $webclient.Encoding = [System.Text.Encoding]::UTF8 # set UTF8 encoding $webclient.UseDefaultCredentials = $true # Default Credentials $webclient.Proxy.Credentials = $webclient.Credentials # Proxy Credentials if ($credential) { $webClient.Credential = $credential } if ($self) { $webClient.UseDefaultCredentials = $true } if ($toFile) { if (-not "$toFile".Contains(":")) { $toFile = Join-Path $pwd $toFile } $webClient.DownloadFile($url, $toFile) } else { if ($bytes) { $webClient.DownloadData($url) } else { $webClient.DownloadString($url) } } } $url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22)&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" $html = Get-Web $url
Diese speichert mir den Quelltext in die Variable $html.
Doch nun wollte ich die Variable per Export-Clixml Cmdlet wieder ausgeben.
$html | Export-Clixml C:\Users\...\Desktop\Wetter.xml
Dabei bleibt die XML Syntax aber nicht bestehen und die PowerShell verpackt alles in einem neuen Knoten. Was auch verständlich ist, aber dies stellt mein großes Problem dar. Ich würde gerne den kompletten Quellcode so exportieren wollen, dass alle Knoten, bzw. die komplette Syntax erkannt wird bzw. erhalten bleibt.
Ist dies überhaupt mit meinem Ansatz umsetzbar oder müsste ich jedes einzeln XML-Attribut rausfiltern, um durch die Zusammenstellung dieser eine XML Datei zu erstellen?
Ich bin bei der PowerShell-Programmierung noch recht am Anfang, also verzeiht, wenn es sich um kompletten Unsinn handelt :(
Danke schonmal!
Gruß Nrude
Antworten
-
Moin,
wenn Du die Datei 1:1 abspeichern willst, kannst Du doch einfach diesen machen:
$url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22)&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" $response = Invoke-WebRequest $url $response.Content | Out-File c:\temp\wetter.xml -Encoding utf8
Oder habe ich etwas übersehen?Evgenij Smirnov
msg services ag, Berlin -> http://www.msg-services.de
my personal blog (mostly German) -> http://it-pro-berlin.de
Windows Server User Group, Berlin -> http://www.winsvr-berlin.de
Mark Minasi Technical Forum, reloaded -> http://newforum.minasi.comIn theory, there is no difference between theory and practice. In practice, there is.
- Als Antwort vorgeschlagen Denniver ReiningMVP, Moderator Donnerstag, 21. Juli 2016 12:08
- Als Antwort markiert Nrude Freitag, 22. Juli 2016 07:26
Alle Antworten
-
Moin,
wenn Du die Datei 1:1 abspeichern willst, kannst Du doch einfach diesen machen:
$url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22)&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" $response = Invoke-WebRequest $url $response.Content | Out-File c:\temp\wetter.xml -Encoding utf8
Oder habe ich etwas übersehen?Evgenij Smirnov
msg services ag, Berlin -> http://www.msg-services.de
my personal blog (mostly German) -> http://it-pro-berlin.de
Windows Server User Group, Berlin -> http://www.winsvr-berlin.de
Mark Minasi Technical Forum, reloaded -> http://newforum.minasi.comIn theory, there is no difference between theory and practice. In practice, there is.
- Als Antwort vorgeschlagen Denniver ReiningMVP, Moderator Donnerstag, 21. Juli 2016 12:08
- Als Antwort markiert Nrude Freitag, 22. Juli 2016 07:26
-
Hallo,
es hat jetzt etwas länger gedauert, um Ihnen zu antworten.
Das ist mir jetzt peinlich, aber Sie haben vollkommen recht.
Mir war das Invoke-WebRequest nicht bekannt, daher habe ich mich sehr auf das Export-Cmdlet versteift.
Vielen vielen Dank für die Hilfe!
Gruß Nrude