跳至内容

XML

def 函数会自动解析 XML 文件并从中提取文本。

def("DOCS", env.files) // contains some xml files
def("XML", env.files, { endsWith: ".xml" }) // only xml

parse

全局函数 XML.parse 用于读取XML文件并将其转换为JSON对象。

const res = XML.parse('<xml attr="1"><child /></xml>')

属性名称以"@_"作为前缀。

{
"xml": {
"@_attr": "1",
"child": {}
}
}

RSS

你可以使用XML.parse来将RSS订阅解析为一个对象。

const res = await fetch("https://dev.to/feed")
const { rss } = XML.parse(await res.text())
// channel -> item[] -> { title, description, ... }

由于RSS订阅源通常返回渲染后的HTML描述,您可以使用parsers.HTMLToText将其转换回纯文本。

const articles = items.map(({ title, description }) => ({
title,
description: parsers.HTMLToText(description)
}))