PHP에서 XML 태그 그룹의 속성
이제 속성을 가진 여러 태그가 있다고 가정해 봅시다:
<root>
<tag attr="val1">text1</tag>
<tag attr="val2">text2</tag>
<tag attr="val3">text3</tag>
</root>
이 태그들의 텍스트를 얻어봅시다:
<?php
foreach ($xml->tag as $tag) {
echo $tag; // 'text1', 'text2', 'text3'
}
?>
이제 - 속성 값들을 얻어봅시다:
<?php
foreach ($xml->tag as $tag) {
echo $tag['attr']; // 'val1', 'val2', 'val3'
}
?>
다음 XML이 주어졌습니다:
<root>
<product cost="100" amount="3">
prod1
</product>
<product cost="200" amount="4">
prod2
</product>
<product cost="300" amount="5">
prod3
</product>
</root>
각 제품의 이름, 가격, 수량을 화면에 출력하세요.