如何用Scala或JavaScript提取XML中的元素名称列表
获取XML中指定子元素的名称(Scala & JavaScript实现)
首先,先明确你提供的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>
你需要提取的是<city>节点下的子元素名称集合:["ID","Name","CountryCode","District","Population"],下面分别给出Scala和JavaScript的实现方案:
Scala实现
Scala对XML有原生的语法支持,处理起来非常方便。我们可以直接解析XML字符串,提取所有<city>节点的子元素标签名,再做去重处理(保证结果的唯一性,即使后续有结构不同的<city>节点也能兼容)。
import scala.xml.XML object XmlElementNameExtractor { def main(args: Array[String]): Unit = { // 你的XML内容字符串 val xmlContent = """<?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>""" // 解析XML字符串 val xmlDoc = XML.loadString(xmlContent) // 提取所有<city>的直接子元素标签名,去重后得到结果 val elementNames = (xmlDoc \\ "city" \ "_").map(_.label).distinct // 打印结果 println("提取的元素名称:" + elementNames.mkString("[", ", ", "]")) // 输出:提取的元素名称:[ID, Name, CountryCode, District, Population] } }
关键代码解释:
XML.loadString(xmlContent):将XML字符串解析为Scala的XML对象xmlDoc \\ "city":递归查找文档中所有<city>节点\ "_":获取每个<city>节点的直接子元素(_匹配任意标签名)map(_.label):提取每个子元素的标签名称distinct:去除重复的标签名,确保结果唯一
JavaScript实现
JavaScript的实现分两种场景:浏览器环境(原生支持XML解析)和Node.js环境(需要第三方包),下面分别给出代码:
浏览器环境(原生DOMParser)
浏览器内置了DOMParser API,可以直接解析XML字符串,无需额外依赖:
// 你的XML内容字符串 const xmlContent = `<?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>`; // 创建XML解析器 const parser = new DOMParser(); const xmlDoc = parser.parseFromString(xmlContent, "text/xml"); // 获取第一个<city>节点的所有子元素(自动排除空白文本节点) const firstCity = xmlDoc.querySelector("city"); const elementNames = Array.from(firstCity.children).map(child => child.tagName); // 打印结果 console.log("提取的元素名称:", elementNames); // 输出:提取的元素名称: ["ID", "Name", "CountryCode", "District", "Population"]
Node.js环境(需要xmldom包)
Node.js原生没有DOMParser,需要安装xmldom包来实现XML解析:
- 先安装依赖:
npm install xmldom
- 实现代码:
const { DOMParser } = require('xmldom'); // 你的XML内容字符串 const xmlContent = `<?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>`; // 创建XML解析器 const parser = new DOMParser(); const xmlDoc = parser.parseFromString(xmlContent, "text/xml"); // 获取第一个<city>节点 const firstCity = xmlDoc.getElementsByTagName("city")[0]; // 过滤掉空白文本节点,提取元素标签名 const elementNames = Array.from(firstCity.childNodes) .filter(node => node.nodeType === Node.ELEMENT_NODE) .map(node => node.tagName); // 打印结果 console.log("提取的元素名称:", elementNames); // 输出:提取的元素名称: ["ID", "Name", "CountryCode", "District", "Population"]
关键代码解释:
- 浏览器环境中
firstCity.children只包含元素节点,无需额外过滤 - Node.js环境中
firstCity.childNodes会包含文本节点(比如换行、空格),所以需要通过nodeType === Node.ELEMENT_NODE过滤出元素节点
内容的提问来源于stack exchange,提问作者stack0114106
相关产品推荐
相关产品推荐

