2011-02-22 2 views
0

Я пытался выяснить, как отображать потомки (в данном случае exchangeRate и PlacesOfInterest) родительского узла с определенным атрибутом.Как отображать потомки XML узла с определенным атрибутом в AS3?

Чтобы установить сцену - пользователь нажимает кнопку, которая устанавливает строчную переменную в пункт назначения, например. Японии или Австралии.

Код затем проходит через множество узлов в XML и любые, которые имеют атрибут соответствия прослеживается - достаточно просто

То, что я не могу понять, как тогда отображать только дочерние узлы узел с этим атрибутом.

Я уверен, что должен быть способ сделать это, и я, вероятно, буду стучать головой о стол, когда найду его, но любая помощь будет принята с благодарностью!

public function ParseDestinations(destinationInput:XML):void 
    { 
     var destAttributes:XMLList = destinationInput.adventure.destination.attributes(); 

     for each (var destLocation:XML in destAttributes) 
     {    
      if (destLocation == destName){ 
       trace(destLocation); 
       trace(destinationInput.adventure.destination.exchangeRate.text()); 
      } 
     } 
    } 



<destinations> 
    <adventure> 
     <destination location="japan"> 
      <exchangeRate>400</exchangeRate> 
      <placesOfInterest>Samurai History</placesOfInterest> 
     </destination> 
     <destination location="australia"> 
      <exchangeRate>140</exchangeRate> 
      <placesOfInterest>Surf and BBQ</placesOfInterest> 
     </destination> 
    </adventure> 
</destinations> 

ответ

0

Вы должны иметь возможность легко фильтровать узлы с E4X в AS3:

var destinations:XML = <destinations> 
    <adventure> 
     <destination location="japan"> 
      <exchangeRate>400</exchangeRate> 
      <placesOfInterest>Samurai History</placesOfInterest> 
     </destination> 
     <destination location="australia"> 
      <exchangeRate>140</exchangeRate> 
      <placesOfInterest>Surf and BBQ</placesOfInterest> 
     </destination> 
    </adventure> 
</destinations>; 
//filter by attribute name 
var filteredByLocation:XMLList = destinations.adventure.destination.(@location == "japan"); 
trace(filteredByLocation); 
//filter by node value 
var filteredByExchangeRate:XMLList = destinations.adventure.destination.(exchangeRate < 200); 
trace(filteredByExchangeRate); 

Посмотрите на Yahoo! devnet article или Roger's E4X article для получения более подробной информации.

, связанные с StackOverflow вопросы:

НТН

+0

Thankyou Джордж! Это очень помогло мне! Я знал, что должен быть простой способ сделать это - я обязательно прочитаю эти сообщения сейчас –

0

Если вы не знаете имя потомка или вы хотите, чтобы выбрать различные потомки с таким же атрибутом ценность вы можете использовать:

destination.descendants ("*"). Elements(). (Attribute ("location") == "japan");

Например:

var xmlData:XML = 
<xml> 
    <firstTag> 
     <firstSubTag> 
      <firstSubSubTag significance="important">data_1</firstSubSubTag> 
      <secondSubSubTag>data_2</secondSubSubTag> 
     </firstSubTag> 
     <secondSubTag> 
      <thirdSubSubTag>data_3</thirdSubSubTag> 
      <fourthSubSubTag significance="important">data_4</fourthSubSubTag> 
     </secondSubTag> 
    </firstTag> 
</xml> 


trace(xmlData.descendants("*").elements().(attribute("significance") == "important")); 

Результат:

//<firstSubSubTag significance="important">data_1</firstSubSubTag> 
//<fourthSubSubTag significance="important">data_4</fourthSubSubTag> 
Смежные вопросы