使用scala或javascript从xml获取元素名称列表

使用scala或javascript从xml获取元素名称列表,javascript,scala,Javascript,Scala,我的xml文件如下所示 <?xml version="1.0" encoding="UTF-8"?> <root> <city> <ID>1</ID> <Name>Kabul</Name> <CountryCode>AFG</CountryCode> <District>Kabol</District> <Population>1780000</P

我的xml文件如下所示

<?xml version="1.0" encoding="UTF-8"?>
<root>
<city>
<ID>1</ID>
<Name>Kabul</Name>
<CountryCode>AFG</CountryCode>
<District>Kabol</District>
<Population>1780000</Population>
</city>
<city>
<ID>2</ID>
<Name>Qandahar</Name>
<CountryCode>AFG</CountryCode>
<District>Qandahar</District>
<Population>237500</Population>
</city>
<city>
<ID>3</ID>
<Name>Herat</Name>
<CountryCode>AFG</CountryCode>
<District>Herat</District>
<Population>186800</Population>
</city>
</root>

1.
喀布尔
AFG
喀布尔省
1780000
2.
坎大哈
AFG
坎大哈
237500
3.
赫拉特
AFG
赫拉特
186800

我需要使用scala或javascript获取元素名(“ID”、“Name”、“CountryCode”、“District”、“Population”)。请提供帮助。

对于JavaScript,就像处理DOM中的元素一样。 (但别忘了解析XML!)

var xml='1KabulAFGKabol17800002QandaharAFGQandahar2375003HeratAFGHerat186800';
var parser=新的DOMParser();
//解析xml
var parsedXML=parser.parseFromString(xml,'text/xml');
//现在获取元素
var id=parsedXML.getElementsByTagName('id');
var Name=parsedXML.getElementsByTagName('Name');
var countryCode=parsedXML.getElementsByTagName('countryCode');
var district=parsedXML.getElementsByTagName('district');
var population=parsedXML.getElementsByTagName('population');
//获取标签名;因为getElementsByTagName返回类似HTMLCollection的数组,所以我们需要像使用数组一样循环遍历结果
对于(变量i=0;i}
那么JavaScript与此有什么关系?你搜索过Parse XML Scala吗?嗨,CodeF0x,我只需要元数据详细信息,而不是值。那只是。。(“ID”、“姓名”、“国家代码”、“地区”、“人口”)@stack0114106那么你指的是标签名?@stack0114106已编辑。现在应该符合你的要求了。