XML데이터의 노드를 찾기 위해서는 Xpath나 서술필터링을 사용하여 노드 데이터로 접근한다.
Xpath는 계층구조의 트리형태로 접근하여 노드나 값을 접근하고 서술 필터링은 조건식을 사용하여 노드나 값에 접근한다.
var data1:XML = <foods>
<fruits>
<fruit color="red">
<name>Apple</name>
</fruit>
<fruit color="orange">
<name>Orange</name>
</fruit>
<fruit color="green">
<name>WaterMelone</name>
</fruit>
<servings>3</servings>
</fruits>
<vegetables>
<vegetable color="red">
<name>토마토</name>
</vegetable>
<vegetable color="brown">
<name>감자</name>
</vegetable>
<vegetable color="green">
<name>양파</name>
</vegetable>
<servings>2</servings>
<vegetable color="green">
<name>멜론</name>
</vegetable>
<servings>2</servings>
</vegetables>
</foods>;
[ Xpath방식 ]
- 계층(트리)구조식로 데이터 접근
var fruitsList:XMLList = data1.fruits.fruit;
trace("******** fruitsList ******** \n " , fruitsList )
<fruit color="red">
<name>Apple</name>
</fruit>
<fruit color="orange">
<name>Orange</name>
</fruit>
<fruit color="green">
<name>WaterMelone</name>
</fruit>
var apple:XML = data1.fruits.fruit[0];
trace("******** apple ******** \n " , apple )
<fruit color="red">
<name>Apple</name>
</fruit>
var vegetableList:XMLList = data1..vegetable;
trace("******** vegetableList ******** \n " , vegetableList )
<vegetable color="red">
<name>토마토</name>
</vegetable>
<vegetable color="brown">
<name>감자</name>
</vegetable>
<vegetable color="green">
<name>양파</name>
</vegetable>
<vegetable color="green">
<name>멜론</name>
</vegetable>
var servings:XMLList = data1.*.servings;
trace("******** servings ******** \n " , servings )
<servings>3</servings>
<servings>2</servings>
<servings>2</servings>
var color:XMLList = data1.fruits.fruit.@color;
trace("******** color ******** \n " , color )
redorangegreen
[ 서술 필터링 ]
- 서술필터링은 조건형식으로 사용, 조건에 맞는 속성/노드를 선택
trace("******** fruit red color ******** \n " , data1..fruit.(@color == "red") );
<fruit color="red">
<name>Apple</name>
</fruit>
trace("******** vegetable green color ******** \n " , data1..vegetable.(@color == "green") );
<vegetable color="green">
<name>양파</name>
</vegetable>
<vegetable color="green">
<name>멜론</name>
</vegetable>
trace("******** 사과의 컬러 속성 ******** \n " , data1..fruit.(name=="Apple").@color)
red
* 첫문자 모음 출력
trace("******** 모음 출력 ******** \n " , data1..fruit.(/^[aeiouAEIOU].*/.test(name) ) )
<fruit color="red">
<name>Apple</name>
</fruit>
<fruit color="orange">
<name>Orange</name>
</fruit>