编写Java程序,创建一个 XML 文档,文档名为“hero.xml”,用于保存“王者荣耀”的英雄信息。

查看本章节

查看作业目录


需求说明:

创建一个 XML 文档,文档名为“hero.xml”,用于保存“王者荣耀”的英雄信息。英雄信息包括编号(id)、姓名(name)、性别(gender)、职业(profession)、国籍(nationality)和价格(price)

实现思路:

  1. 创建 Java 项目,在项目中创建 XML 文档 hero.xml
  2. 在 hero.xml 文档中,创建根节点 heroList
  3. 在根节点内,创建属性节点 hero,包含 id 和 name 两个属性
  4. 在属性节点 hero 内,再创建元素节点 gender、profession、nationality 和 price
  5. 每一个 hero 节点就是一条完整的英雄信息。保存多条英雄信息,只需要重复完整的 hero 节点即可

实现代码:

<?xml version="1.0" encoding="utf-8"?>
<heroList>
<hero name="亚瑟" id="1">
	<gender>男</gender>
	<profession>坦克</profession>
	<nationality>英国</nationality>
	<price>2000</price>
</hero>
<hero name="孙尚香" id="2">
	<gender>女</gender>
	<profession>射手</profession>
	<nationality>中国</nationality>
	<price>18888</price>
</hero>
<hero name="马可波罗" id="3">
	<gender>男</gender>
	<profession>射手</profession>
	<nationality>威尼斯</nationality>
	<price>13000</price>
</hero>
</heroList>

猜你喜欢

转载自blog.csdn.net/weixin_44893902/article/details/108959889