「XMLはタグでデータを囲むって聞いたけど、階層ってどうやって作るの?」
「入れ子ってよく言われるけど何が大事なの?」
XMLを初めて扱うとき、多くの人がこの**階層構造(ネスト)**でつまずきます。
しかし、XMLの階層構造を理解すると、データの関係性や意味を誰が見ても分かる形で表現できるようになります。
これはプログラミングだけでなく、データベース設計やシステム間の連携でも役立つ重要な考え方です。
この記事では、XMLタグの階層構造について、基本の考え方から実際の書き方、よくある間違いまで、初心者にも分かりやすく解説します。
XMLの階層構造(ネスト)とは?

簡単に言うと
XMLの階層構造とは、タグを入れ子にしてデータの親子関係を表現することです。
これにより、「どのデータがどのデータに属しているのか」を一目で理解できるようになります。
日常生活で例えると
階層構造は、日常生活の中にもたくさんあります:
住所の階層
日本
└ 東京都
└ 新宿区
└ 西新宿
└ 1丁目
会社の組織
会社
└ 営業部
└ 第一営業課
└ 田中さん
XMLの階層も、これと同じ考え方です。
XMLでの表現
先ほどの住所をXMLで表現すると:
<住所>
<都道府県>東京都</都道府県>
<市区町村>新宿区</市区町村>
<町名>西新宿</町名>
<番地>1丁目</番地>
</住所>
このように、大きなまとまりから小さなまとまりへと順番に表現できます。
実際のXML階層構造の例

基本的な例:注文データ
注文データをXMLで表現してみましょう:
<order>
<customer>
<name>やまだたろう</name>
<email>yamada@example.com</email>
<address>
<prefecture>東京都</prefecture>
<city>新宿区</city>
<street>西新宿1-1-1</street>
</address>
</customer>
<items>
<item>
<product>りんご</product>
<quantity>3</quantity>
<price>150</price>
</item>
<item>
<product>みかん</product>
<quantity>5</quantity>
<price>100</price>
</item>
</items>
<total>950</total>
</order>
この階層構造のポイント
第1階層:全体のまとまり
<order>
:注文全体を表す
第2階層:大きな分類
<customer>
:顧客情報<items>
:注文商品のリスト<total>
:合計金額
第3階層:詳細情報
<name>
、<email>
:顧客の詳細<address>
:住所情報<item>
:個別の商品
第4階層:さらに細かい情報
<prefecture>
、<city>
:住所の詳細<product>
、<quantity>
:商品の詳細
より複雑な例:社員管理システム
<company>
<departments>
<department id="sales">
<name>営業部</name>
<manager>
<name>さとうけんじ</name>
<employee_id>001</employee_id>
</manager>
<employees>
<employee>
<name>たなかはなこ</name>
<employee_id>002</employee_id>
<position>主任</position>
<projects>
<project>
<name>新商品開発</name>
<status>進行中</status>
</project>
<project>
<name>顧客開拓</name>
<status>完了</status>
</project>
</projects>
</employee>
</employees>
</department>
</departments>
</company>
この例では、6階層の深い構造になっています。
XMLで階層を作るときの基本ルール
開始タグと終了タグは必ずセット
XMLでは、開いたタグは必ず閉じる必要があります。
正しい例
<customer>
<name>やまだたろう</name>
</customer>
間違った例
<customer>
<name>やまだたろう</name>
<!-- </customer> を忘れている! -->
必ずルート要素(親タグ)を1つにする
XMLファイルには、すべてを包む最上位のタグが1つだけ必要です。
正しい例
<root>
<data1>情報1</data1>
<data2>情報2</data2>
</root>
間違った例
<data1>情報1</data1>
<data2>情報2</data2>
<!-- ルート要素がない! -->
タグの入れ子は正しい順番で閉じる
タグを入れ子にするときは、後に開いたタグから先に閉じる必要があります。
正しい例
<order>
<customer>
<name>やまだたろう</name>
</customer>
</order>
間違った例
<order>
<customer>
<name>やまだたろう</name>
</order>
</customer>
<!-- 順番が逆! -->
きれいなインデントで可読性アップ
タグが入れ子になるので、見やすくするためにインデント(字下げ)をつけます。
読みやすい例
<items>
<item>
<product>りんご</product>
<quantity>3</quantity>
</item>
<item>
<product>みかん</product>
<quantity>5</quantity>
</item>
</items>
読みにくい例
<items>
<item>
<product>りんご</product>
<quantity>3</quantity>
</item>
<item>
<product>みかん</product>
<quantity>5</quantity>
</item>
</items>
どちらも正しいXMLですが、インデントがあると階層がわかりやすくなります。
XMLの階層を考えるときのコツ

データの「まとまり」を意識する
階層を設計するときは、データのまとまりを意識することが大切です。
良い設計の例
<library>
<books>
<book>
<title>XMLの基本</title>
<author>やまだたろう</author>
<publication>
<publisher>テック出版</publisher>
<year>2025</year>
</publication>
</book>
</books>
<members>
<member>
<name>さとうはなこ</name>
<contact>
<email>sato@example.com</email>
<phone>090-1234-5678</phone>
</contact>
</member>
</members>
</library>
この例では:
- 本の情報は
<books>
の下にまとめる - 会員の情報は
<members>
の下にまとめる - 連絡先は
<contact>
の下にまとめる
繰り返しデータの扱い方
同じ種類のデータが複数あるときは、複数形の親タグでまとめます。
推奨パターン
<products>
<product>商品1</product>
<product>商品2</product>
<product>商品3</product>
</products>
避けたいパターン
<product1>商品1</product1>
<product2>商品2</product2>
<product3>商品3</product3>
階層の深さを適切に保つ
階層が深すぎると理解しにくくなります。一般的には5〜6階層程度に抑えるのが良いとされています。
深すぎる例(改善が必要)
<root>
<level1>
<level2>
<level3>
<level4>
<level5>
<level6>
<level7>
<data>値</data>
</level7>
</level6>
</level5>
</level4>
</level3>
</level2>
</level1>
</root>
階層設計でよくある間違いと対策

よくある間違い1:平坦すぎる構造
悪い例
<data>
<customer_name>やまだたろう</customer_name>
<customer_email>yamada@example.com</customer_email>
<customer_address>東京都新宿区</customer_address>
<product1_name>りんご</product1_name>
<product1_quantity>3</product1_quantity>
<product2_name>みかん</product2_name>
<product2_quantity>5</product2_quantity>
</data>
良い例
<order>
<customer>
<name>やまだたろう</name>
<email>yamada@example.com</email>
<address>東京都新宿区</address>
</customer>
<products>
<product>
<name>りんご</name>
<quantity>3</quantity>
</product>
<product>
<name>みかん</name>
<quantity>5</quantity>
</product>
</products>
</order>
よくある間違い2:意味のない階層
悪い例
<root>
<data>
<information>
<content>
<value>実際のデータ</value>
</content>
</information>
</data>
</root>
良い例
<order>
<customer_name>やまだたろう</customer_name>
</order>
よくある間違い3:混在する構造
悪い例
<products>
<product>りんご</product>
<item>
<name>みかん</name>
</item>
</products>
良い例
<products>
<product>りんご</product>
<product>みかん</product>
</products>
または
<products>
<product>
<name>りんご</name>
</product>
<product>
<name>みかん</name>
</product>
</products>
XMLスキーマで階層構造を制御する
XMLスキーマとは?
XMLスキーマ(XSD)は、XMLの構造や内容に関するルールを定義するためのものです。
基本的な階層制御の例
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="order">
<xs:complexType>
<xs:sequence>
<xs:element name="customer" type="customerType"/>
<xs:element name="items" type="itemsType"/>
<xs:element name="total" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="customerType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="itemsType">
<xs:sequence>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="product" type="xs:string"/>
<xs:element name="quantity" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
このスキーマにより、XMLの階層構造が正しく守られているかを自動的にチェックできます。
実践的な活用場面

Webサービスでの利用
REST APIのレスポンス
<response>
<status>success</status>
<data>
<users>
<user id="1">
<name>やまだたろう</name>
<profile>
<age>30</age>
<department>営業部</department>
</profile>
</user>
</users>
</data>
</response>
設定ファイルでの利用
アプリケーション設定
<configuration>
<database>
<connection>
<host>localhost</host>
<port>3306</port>
<credentials>
<username>admin</username>
<password>secret</password>
</credentials>
</connection>
</database>
<logging>
<level>INFO</level>
<output>
<file>app.log</file>
<console>true</console>
</output>
</logging>
</configuration>
データ交換での利用
電子商取引
<invoice>
<header>
<invoice_number>INV-2025-001</invoice_number>
<issue_date>2025-07-03</issue_date>
<supplier>
<name>ABC商事</name>
<address>東京都渋谷区</address>
</supplier>
<customer>
<name>XYZ株式会社</name>
<address>大阪府大阪市</address>
</customer>
</header>
<details>
<line_items>
<line_item>
<description>ノートパソコン</description>
<quantity>2</quantity>
<unit_price>80000</unit_price>
<total_price>160000</total_price>
</line_item>
</line_items>
<summary>
<subtotal>160000</subtotal>
<tax>16000</tax>
<total>176000</total>
</summary>
</details>
</invoice>
階層構造を理解するための練習問題
練習問題1:学校のクラス管理
次の情報をXMLの階層構造で表現してみてください:
- 学校名:さくら小学校
- 学年:3年生
- クラス:A組、B組
- A組の担任:やまだせんせい
- A組の生徒:たろうくん、はなこちゃん
- B組の担任:さとうせんせい
- B組の生徒:じろうくん、みちこちゃん
解答例
<school>
<name>さくら小学校</name>
<grades>
<grade level="3">
<classes>
<class name="A">
<teacher>やまだせんせい</teacher>
<students>
<student>たろうくん</student>
<student>はなこちゃん</student>
</students>
</class>
<class name="B">
<teacher>さとうせんせい</teacher>
<students>
<student>じろうくん</student>
<student>みちこちゃん</student>
</students>
</class>
</classes>
</grade>
</grades>
</school>
練習問題2:レシピ管理
料理のレシピをXMLで表現してみてください:
- 料理名:カレーライス
- 調理時間:30分
- 材料:じゃがいも(2個)、にんじん(1本)、たまねぎ(1個)
- 手順:野菜を切る、炒める、煮込む
解答例
<recipe>
<name>カレーライス</name>
<cooking_time>30分</cooking_time>
<ingredients>
<ingredient>
<name>じゃがいも</name>
<quantity>2個</quantity>
</ingredient>
<ingredient>
<name>にんじん</name>
<quantity>1本</quantity>
</ingredient>
<ingredient>
<name>たまねぎ</name>
<quantity>1個</quantity>
</ingredient>
</ingredients>
<instructions>
<step order="1">野菜を切る</step>
<step order="2">炒める</step>
<step order="3">煮込む</step>
</instructions>
</recipe>
まとめ
今回は「XMLのタグ階層(ネスト)」について詳しく解説しました。
重要なポイント
- 階層構造はデータの関係性を表現する手段
- 開始タグと終了タグは必ずセット
- ルート要素は必ず1つ
- インデントで可読性を向上させる
- データのまとまりを意識して設計する
コメント