2009-11-10 6 views
1

내 AS3 프로그램에서 하위 XML 태그 이름을 가져 오려고합니다. 예를 들어,이 같은 정보를 가진 파일이 : 내가있는 ArrayCollection로 파일을로드하고 내가 이름으로해야하는 각 항목에 액세스 할 수 있습니다AS3에서 XML 태그 이름을 가져 오는 방법

<LocationInfo> 
    <City>New York City</City> 
    <State>NY</State> 
    <Zip>10098</Zip> 
</LocationInfo> 

을 같은 뉴욕

을 반환 // [ "도시"]로

값을 얻는 대신 NY 또는 10098 대신 State 또는 Zip과 같은 태그 이름을 얻으려고합니다.

답변

3

, 당신은 거의 항상 XMLListCollection에를 사용하는 것이 더 낫다. 대부분의 경우 ArrayCollection의 추가 기능이 실제로 필요합니다. XMLListCollection을 사용하면 훨씬 쉽게 작업 할 수 있습니다. 또한 ArrayCollection은 항상 일을 올바르게 직렬화하지 않습니다. 특정 상황을 제공 할 수는 없지만 XML이 제대로 저장되지 않아서 리팩터링을해야한다는 것을 알고 있습니다. 마지막으로 XMLListCollection.toXMLString()ArrayCollection.toString보다 훨씬 더 나은 데이터 상태를 제공합니다. XMLListCollection에와

, 당신이 다음 중 하나에서 수행 될 것이다 찾고있는 당신이 정말로있는 ArrayCollection을 사용해야하는 경우

var coll:XMLListCollection = <xml-here/> 

// Iterate through all of the children nodes 
for(var i:int = 0; i < coll.children().length(); i++) 
{ 
    var currentNode:XML = coll.children()[ i ]; 
    // Notice the QName class? It is used with XML. 
    // It IS NOT a String, and if you forget to cast them, then 
    // currentNode.localName() will NEVER be "city", even though it may 
    // trace that way. 
    var name:QName  = currentNode.name(); // url.to.namespace::city 
    var locName:QName = currentNode.localName() // City 
} 

// get all city nodes in the current parent element 
var currCities:XMLList = currentParentNode.cities; // currentParentNode can be 
// an XMLList, an XML object, an XMLListAdapter, or an XMLListCollection. 
// This means that you can do something like coll.cities.cities, if there are 
// children nodes like that. 
for(i = 0; i < cities.length(); i++) 
{ 
    var currentCity:XML = cities[ i ]; 
} 

// get all city nodes in all descendant nodes. 
var cities:XMLList = currentParentNode.descendants( 
         new QName("url.to.namespace", "City") 
        ); 
for(i = 0; i < cities.length(); i++) 
{ 
    var currentCity:XML = cities[ i ]; 
} 

는, 당신은 사용할 수있는 중 ... 구문 것은 달성하기 원하는 항목 :

// This is one of the few times I recommend anonymous typing. When dealing with 
// XML (which likely has already been turned into a String anyway, but be safe) 
// anonymous syntax can give you better results. 

// it = iterant, I use this to contrast with i for my sanity. 
for(var it:* in coll) 
{ 
    trace(it,"=",coll[ it ]); 
} 
0

각 LocationInfo 태그는 ArrayCollection의 항목이됩니다. 각 엔트리는 동적 객체이므로 실제로 동적 객체에서 속성 이름을 가져 오는 방법을 묻는 것입니다. 그들이 다른 방식으로 요청했지만 이것은 이미 답변되었습니다.

https://stackoverflow.com/questions/372317/how-can-i-get-list-of-properties-in-an-object-in-actionscript

을 그리고 여기에 가장 간단한을 듯 대답 : 다음은 원래의 페이지로 연결되는 링크입니다

var obj:Object; // I'm assuming this is your object 

for(var id:String in obj) { 
    var value:Object = obj[id]; 

    trace(id + " = " + value); 
} 
2

당신이하는 XMLNode의 요소 이름을 얻기 위해 localName 속성을 사용할 수 있습니다. 같은

뭔가 : XML 처리 할 때

var xml : XML = <LocationInfo>...</LocationInfo>; 

foreach(var child : XMLNode in xml.LocationInfo.childNodes) 
{ 
    trace(child.localName + " = " + child.nodeValue); 
}