2016-04-03 8 views
0

Ich habe Probleme beim Lesen einer (etwas) großen XML-Datei mit Nokogiri, kann aber nicht herausfinden, wo die Dinge falsch laufen. Der Dateiinhalt sieht wie folgt aus (nur einen einzelnen Knoten zur besseren Lesbarkeit im Lieferumfang enthalten):Große XML-Datei mit Nokogiri lesen

<?xml version="1.0" encoding="UTF-8"?> 
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0"><title>End | Globally Sourced Menswear</title><link type="self" link="http://www.endclothing.com/eu/"/><updated>2016-04-02T01:25:30+00:00</updated><entry><g:id>391</g:id><g:mpn>WRY3924TY</g:mpn><g:color>N/A</g:color><g:title>Comme des Garcons x Artek Standard Eau De Toilette</g:title><g:link>http://www.endclothing.com/eu/comme-des-garcons-x-artek-standard-eau-de-toilette-wry3924ty.html</g:link><g:price>89.00 EUR</g:price><g:availability>in stock</g:availability><g:brand>CDG Parfum</g:brand><g:custom_label_0>Perfume &amp; Fragrance</g:custom_label_0><g:condition>new</g:condition><g:description><![CDATA[<p>Founded by 4 young idealists in 1935, Finnish design company Artek produce modern furniture to promote the modern culture of habitation. Here they collaborate with <a href="/brands/comme-des-garcons-parfum">Comme des Garçons Parfum</a> to produce a fragrance dubbed 'Standard', ironic for a scent that is anything but.</p> 
<span style="font-style:italic;">Notes Include:</span> 
<ul> 
<li>Thyme</li> 
<li>Black Pepper</li> 
<li>Patchouli</li> 
<li>Cedar Wood</li> 
<li>Citrus</li> 
</ul> 
<p>Due to recent changes in regulations, we are unable to ship aftershaves and perfumes to certain destinations outside of the EU. For full details, please email <a href="mailto:[email protected]?subject=Aftershave and Perfume Shipment">[email protected]</a> or call +44 191 231 3983.</p>]]></g:description><g:image_link>http://media.endclothing.com/media/catalog/product/1/8/18-03-2016_commedesgarcons_xartekstandardeaudetoilette_100ml_sh_1.jpg</g:image_link><g:additional_image_link>http://media.endclothing.com/media/catalog/product/1/8/18-03-2016_commedesgarcons_xartekstandardeaudetoilette_100ml_sh_2.jpg</g:additional_image_link><g:shipping><g:country>FR</g:country><g:service>DPD Priority Service</g:service><g:price>9.00 EUR</g:price></g:shipping><g:shipping><g:country>DE</g:country><g:service>DPD Priority Service</g:service><g:price>9.00 EUR</g:price></g:shipping><g:shipping><g:country>DK</g:country><g:service>DPD Priority Service</g:service><g:price>9.00 EUR</g:price></g:shipping><g:shipping><g:country>NL</g:country><g:service>DPD Priority Service</g:service><g:price>9.00 EUR</g:price></g:shipping><g:shipping><g:country>IT</g:country><g:service>DPD Priority Service</g:service><g:price>9.00 EUR</g:price></g:shipping><g:shipping><g:country>SE</g:country><g:service>DPD Priority Service</g:service><g:price>9.00 EUR</g:price></g:shipping><g:shipping><g:country>BE</g:country><g:service>DPD Priority Service</g:service><g:price>9.00 EUR</g:price></g:shipping><g:shipping><g:country>AT</g:country><g:service>DPD Priority Service</g:service><g:price>15.00 EUR</g:price></g:shipping><g:shipping><g:country>IE</g:country><g:service>Parcel Force Priority Service</g:service><g:price>15.00 EUR</g:price></g:shipping><g:shipping><g:country>ES</g:country><g:service>DPD Priority Service</g:service><g:price>15.00 EUR</g:price></g:shipping><g:shipping><g:country>LV</g:country><g:service>DPD Priority Service</g:service><g:price>19.00 EUR</g:price></g:shipping><g:shipping><g:country>HR</g:country><g:service>DPD Priority Service</g:service><g:price>35.00 EUR</g:price></g:shipping><g:shipping><g:country>CY</g:country><g:service>FEDEX Priority Service</g:service><g:price>45.00 EUR</g:price></g:shipping><g:shipping><g:country>HU</g:country><g:service>DPD Priority Service</g:service><g:price>15.00 EUR</g:price></g:shipping><g:shipping><g:country>PT</g:country><g:service>DPD Priority Service</g:service><g:price>19.00 EUR</g:price></g:shipping><g:shipping><g:country>EE</g:country><g:service>DPD Priority Service</g:service><g:price>25.00 EUR</g:price></g:shipping><g:shipping><g:country>LU</g:country><g:service>DPD Priority Service</g:service><g:price>9.00 EUR</g:price></g:shipping><g:shipping><g:country>SK</g:country><g:service>DPD Priority Service</g:service><g:price>15.00 EUR</g:price></g:shipping><g:shipping><g:country>BG</g:country><g:service>DPD Priority Service</g:service><g:price>25.00 EUR</g:price></g:shipping><g:shipping><g:country>GR</g:country><g:service>FEDEX Priority Service</g:service><g:price>25.00 EUR</g:price></g:shipping><g:shipping><g:country>PL</g:country><g:service>DPD Priority Service</g:service><g:price>15.00 EUR</g:price></g:shipping><g:shipping><g:country>LT</g:country><g:service>DPD Priority Service</g:service><g:price>19.00 EUR</g:price></g:shipping><g:shipping><g:country>SI</g:country><g:service>DPD Priority Service</g:service><g:price>15.00 EUR</g:price></g:shipping><g:shipping><g:country>FI</g:country><g:service>Parcel Force Priority Service</g:service><g:price>19.00 EUR</g:price></g:shipping><g:shipping><g:country>CZ</g:country><g:service>DPD Priority Service</g:service><g:price>15.00 EUR</g:price></g:shipping><g:shipping><g:country>LI</g:country><g:service>FEDEX Priority Service</g:service><g:price>35.00 EUR</g:price></g:shipping><g:shipping><g:country>MC</g:country><g:service>DPD Priority Service</g:service><g:price>15.00 EUR</g:price></g:shipping><g:shipping><g:country>CH</g:country><g:service>Parcel Force Priority Service</g:service><g:price>15.00 EUR</g:price></g:shipping></entry></feed> 

ich den folgenden Code versucht haben, den Stream zu lesen, und während die einzelnen Teile nur scheinen gut zu funktionieren (data gibt ein String, der für mich gültiger XML-Code zu sein scheint), Nokogiri scheint die Zeichenfolge nicht lesen zu können und stürzt einfach ab oder gibt nichts für meine xpath-Abfragen zurück.

url = "http://www.endclothing.com/media/end_feeds/eu.xml.gz" 
stream = open(url, 'Accept-Encoding' => 'gzip') 
data = Zlib::GzipReader.new(stream).read 
page = Nokogiri::XML(data) 

page.xpath("//entry") 
=> [] 
+1

Wie groß? Megabyte, Gigabyte, Terabyte? – Meier

+0

5 MB entpackt, 250 MB entpackt. –

+0

Das Universum erweitert sich so 250MB ist nicht mehr groß. –

Antwort

1

Die XML hat Standard-Namespace auf der Stammelementebene erklärt:

xmlns="http://www.w3.org/2005/Atom" 

In XML, Nachkommen Elemente ohne Präfix erben Standard-Namespace von Vorfahren implizit. Das heißt, entry Element, das Sie versucht haben, zu erhalten, ist im Standard-Namespace des Stammelements.

Auf der anderen Seite, in XPath, Element ohne Präfix immer in leeren Namespace berücksichtigt. Um das Element im XML-Standardnamespace mit XPath zu referenzieren, müssen wir dem Standardnamespace-URI ein Präfix zuordnen und dieses Präfix in unserem XPath verwenden. Beispiel:

page.xpath("//d:entry", 'd' => 'http://www.w3.org/2005/Atom') 
+0

Vielen Dank dafür, ich hatte keine Ahnung von den Namensräumen. Ich habe stattdessen page.remove_namespaces! .xpath ("// entry") verwendet, aber das war alles dank Ihrer Antwort. –