PHP에서 중첩된 XML 태그 그룹 처리

이제 내부에 중첩 태그를 포함하는 여러 태그가 있다고 가정해 봅시다:

test.xml<root>
  <tag>
    <elem>text1</elem>
  </tag>
  <tag>
    <elem>text2</elem>
  </tag>
  <tag>
    <elem>text3</elem>
  </tag>
</root>

루프를 사용하여 태그를 순회하고 중첩된 태그의 값을 출력해 봅시다:

index.php<?php
  foreach ($xml->tag as $tag) {
    echo $tag->elem; // 'text1', 'text2', 'text3'
  }
?>
Задание 1

다음은 주어진 XML입니다:

html<root>
  <user>
    <name>john</name>
    <surn>smit</surn>
  </user>
  <user>
    <name>eric</name>
    <surn>wils</surn>
  </user>
  <user>
    <name>kyle</name>
    <surn>tayl</surn>
  </user>
</root>

각 사용자의 이름과 성을 출력하세요.

Задание 2

다음은 주어진 XML입니다:

html<root>
  <product cost="100" amount="3">
    <name>prod1</name>
    <category>cat1</category>
  </product>
  <product cost="200" amount="4">
    <name>prod2</name>
    <category>cat2</category>
  </product>
  <product cost="300" amount="5">
    <name>prod3</name>
    <category>cat3</category>
  </product>
</root>

각 제품의 이름, 카테고리, 가격 및 수량을 출력하세요.