2010-12-01 6 views
1

J'ai un xml qui ressemble à ceci:obtenir la valeur à partir d'une liste

<?xml version="1.0" encoding="UTF-8"?> 

<feed xmlns="http://www.w3.org/2005/Atom" version="1.0" xml:lang="en"> 
<title type="text">Google Hot Trends</title> 
<subtitle type="text">What are people searching for on Google today?</subtitle> 
<id>http://www.google.com/trends/hottrends/atom/hourly,2007-08</id> 
<link href="http://www.google.com/trends/hottrends/atom/hourly" rel="self"/> 
<updated>2010-12-01T14:03:30Z</updated> 
<entry xmlns="http://www.w3.org/2005/Atom"> 
<id>2010-12-01T14:03:30Z</id> 
<title type="text"><![CDATA[rosa parks, adam levine girlfriend, meet the famous, ...]]></title> 

<content type="html"><![CDATA[<ol> 
<li><span class="Mild up2"><a href="http://www.google.com/trends/hottrends?q=rosa+parks&date=2010-12-1&sa=X">rosa parks</a></span></li> 
<li><span class="Mild down1"><a href="http://www.google.com/trends/hottrends?q=adam+levine+girlfriend&date=2010-12-1&sa=X">adam levine girlfriend</a></span></li> 
<li><span class="Mild down1"><a href="http://www.google.com/trends/hottrends?q=meet+the+famous&date=2010-12-1&sa=X">meet the famous</a></span></li> 
<li><span class="Mild equal"><a href="http://www.google.com/trends/hottrends?q=sons+of+anarchy+season+4&date=2010-12-1&sa=X">sons of anarchy season 4</a></span></li> 
<li><span class="Mild equal"><a href="http://www.google.com/trends/hottrends?q=victoria+secret+angels&date=2010-12-1&sa=X">victoria secret angels</a></span></li> 
<li><span class="Mild up5"><a href="http://www.google.com/trends/hottrends?q=happy+hanukkah&date=2010-12-1&sa=X">happy hanukkah</a></span></li> 
<li><span class="Mild down1"><a href="http://www.google.com/trends/hottrends?q=katy+perry+victoria+secret+fashion+show&date=2010-12-1&sa=X">katy perry victoria secret fashion show</a></span></li> 
<li><span class="Mild down1"><a href="http://www.google.com/trends/hottrends?q=married+to+the+mob+clothing&date=2010-12-1&sa=X">married to the mob clothing</a></span></li> 
<li><span class="Mild down1"><a href="http://www.google.com/trends/hottrends?q=victoria+secret+models+names+and+pictures&date=2010-12-1&sa=X">victoria secret models names and pictures</a></span></li> 
<li><span class="Mild equal"><a href="http://www.google.com/trends/hottrends?q=world+aids+day+2010&date=2010-12-1&sa=X">world aids day 2010</a></span></li> 
<li><span class="Mild up6"><a href="http://www.google.com/trends/hottrends?q=shahla+jahed&date=2010-12-1&sa=X">shahla jahed</a></span></li> 
<li><span class="Mild equal"><a href="http://www.google.com/trends/hottrends?q=interpol&date=2010-12-1&sa=X">interpol</a></span></li> 
<li><span class="Mild up18"><a href="http://www.google.com/trends/hottrends?q=wallis+simpson&date=2010-12-1&sa=X">wallis simpson</a></span></li> 
<li><span class="Mild up46"><a href="http://www.google.com/trends/hottrends?q=chanukah&date=2010-12-1&sa=X">chanukah</a></span></li> 
<li><span class="Mild new"><a href="http://www.google.com/trends/hottrends?q=hiv+symptoms&date=2010-12-1&sa=X">hiv symptoms</a></span></li> 
<li><span class="Mild up6"><a href="http://www.google.com/trends/hottrends?q=santa+tracker&date=2010-12-1&sa=X">santa tracker</a></span></li> 
<li><span class="Mild down8"><a href="http://www.google.com/trends/hottrends?q=vs+fashion+show+2010&date=2010-12-1&sa=X">vs fashion show 2010</a></span></li> 
<li><span class="Mild down3"><a href="http://www.google.com/trends/hottrends?q=advent+calendar&date=2010-12-1&sa=X">advent calendar</a></span></li> 
<li><span class="Mild up8"><a href="http://www.google.com/trends/hottrends?q=december+1&date=2010-12-1&sa=X">december 1</a></span></li> 
<li><span class="Mild up19"><a href="http://www.google.com/trends/hottrends?q=aids+ribbon&date=2010-12-1&sa=X">aids ribbon</a></span></li> 
</ol> 
]]></content></entry></feed> 

Comment pourrais-je être en mesure d'obtenir les valeurs des <a> balises qui sont à l'intérieur des balises <li>?

Répondre

2

Ce que vous pouvez faire si votre <content> est bien formé, est de le transformer en XML puis d'utiliser e4x pour demander les informations que vous vouliez.

Vous avez encore une chose à faire, c'est que votre XML contient namespace donc pour obtenir le noeud de contenu, vous devez en prendre soin. Je vous suggère de lire le bon post sur XML here par exemple.

Ainsi, le nœud content est à l'intérieur du nœud entry qui ont aussi un namespace, mais comme il est le même que le nœud racine, nous pouvons réutiliser la même namespace:

var xml:XML=<feed xmlns="http://www.w3.org/2005/Atom" version="1.0" xml:lang="en"> 
<title type="text">Google Hot Trends</title> 
<subtitle type="text">What are people searching for on Google today?</subtitle> 
<id>http://www.google.com/trends/hottrends/atom/hourly,2007-08</id> 
<link href="http://www.google.com/trends/hottrends/atom/hourly" rel="self"/> 
<updated>2010-12-01T14:03:30Z</updated> 
<entry xmlns="http://www.w3.org/2005/Atom"> 
<id>2010-12-01T14:03:30Z</id> 
<title type="text"><![CDATA[rosa parks, adam levine girlfriend, meet the famous, ...]]></title> 

<content type="html"><![CDATA[<ol> 
<li><span class="Mild up2"><a href="http://www.google.com/trends/hottrends?q=rosa+parks&date=2010-12-1&sa=X">rosa parks</a></span></li> 
<li><span class="Mild down1"><a href="http://www.google.com/trends/hottrends?q=adam+levine+girlfriend&date=2010-12-1&sa=X">adam levine girlfriend</a></span></li> 
<li><span class="Mild down1"><a href="http://www.google.com/trends/hottrends?q=meet+the+famous&date=2010-12-1&sa=X">meet the famous</a></span></li> 
<li><span class="Mild equal"><a href="http://www.google.com/trends/hottrends?q=sons+of+anarchy+season+4&date=2010-12-1&sa=X">sons of anarchy season 4</a></span></li> 
<li><span class="Mild equal"><a href="http://www.google.com/trends/hottrends?q=victoria+secret+angels&date=2010-12-1&sa=X">victoria secret angels</a></span></li> 
<li><span class="Mild up5"><a href="http://www.google.com/trends/hottrends?q=happy+hanukkah&date=2010-12-1&sa=X">happy hanukkah</a></span></li> 
<li><span class="Mild down1"><a href="http://www.google.com/trends/hottrends?q=katy+perry+victoria+secret+fashion+show&date=2010-12-1&sa=X">katy perry victoria secret fashion show</a></span></li> 
<li><span class="Mild down1"><a href="http://www.google.com/trends/hottrends?q=married+to+the+mob+clothing&date=2010-12-1&sa=X">married to the mob clothing</a></span></li> 
<li><span class="Mild down1"><a href="http://www.google.com/trends/hottrends?q=victoria+secret+models+names+and+pictures&date=2010-12-1&sa=X">victoria secret models names and pictures</a></span></li> 
<li><span class="Mild equal"><a href="http://www.google.com/trends/hottrends?q=world+aids+day+2010&date=2010-12-1&sa=X">world aids day 2010</a></span></li> 
<li><span class="Mild up6"><a href="http://www.google.com/trends/hottrends?q=shahla+jahed&date=2010-12-1&sa=X">shahla jahed</a></span></li> 
<li><span class="Mild equal"><a href="http://www.google.com/trends/hottrends?q=interpol&date=2010-12-1&sa=X">interpol</a></span></li> 
<li><span class="Mild up18"><a href="http://www.google.com/trends/hottrends?q=wallis+simpson&date=2010-12-1&sa=X">wallis simpson</a></span></li> 
<li><span class="Mild up46"><a href="http://www.google.com/trends/hottrends?q=chanukah&date=2010-12-1&sa=X">chanukah</a></span></li> 
<li><span class="Mild new"><a href="http://www.google.com/trends/hottrends?q=hiv+symptoms&date=2010-12-1&sa=X">hiv symptoms</a></span></li> 
<li><span class="Mild up6"><a href="http://www.google.com/trends/hottrends?q=santa+tracker&date=2010-12-1&sa=X">santa tracker</a></span></li> 
<li><span class="Mild down8"><a href="http://www.google.com/trends/hottrends?q=vs+fashion+show+2010&date=2010-12-1&sa=X">vs fashion show 2010</a></span></li> 
<li><span class="Mild down3"><a href="http://www.google.com/trends/hottrends?q=advent+calendar&date=2010-12-1&sa=X">advent calendar</a></span></li> 
<li><span class="Mild up8"><a href="http://www.google.com/trends/hottrends?q=december+1&date=2010-12-1&sa=X">december 1</a></span></li> 
<li><span class="Mild up19"><a href="http://www.google.com/trends/hottrends?q=aids+ribbon&date=2010-12-1&sa=X">aids ribbon</a></span></li> 
</ol> 
]]></content></entry></feed>; 

// get the current namespace from the XML 
var ns:Namespace = xml.namespace(); 

// get and create a new XML from the content node using the previous namespace 
var contentXML:XML = new XML(xml.ns::entry.ns::content.toString()); 

// get all the a node 
var anchors:XMLList = contentXML.li.span.a; 

// now do what you want with values 
for each (var anchor:XML in anchors) 
trace([email protected]); 
+0

+1: J'allais répondez-y de la même manière mais continue à être interrompu. Travail stupide! :) – Robusto

+0

@Robusto, oui nous devons tous payer la facture;) – Patrick

Questions connexes