【问题标题】:How to select specific tags from Mule API html response using JavaScript?如何使用 JavaScript 从 Mule API html 响应中选择特定标签?
【发布时间】:2019-09-08 11:00:05
【问题描述】:

我有一个 Mule 应用程序,它以文本形式返回 html 文档,该文档包含 <tr/><td/> 标记,其中包含我要访问的信息.如何从 Mule API html response 中选择特定标签?

async function getTag(env, val) {
    try {
        let hashes = await fetch(`/admin/api/html/ref/${env}/${val}`)
            .then(res => { return (res.text()) })
            .then(data => {
                let count = 0
                console.log(data)
            })
    } catch (error) {
        console.log(error)
    }

【问题讨论】:

标签: javascript xml mule fetch dom-manipulation


【解决方案1】:

如果我理解正确,您想解析 API 调用返回的 html 文本并从中检索一些信息吗?

如果是,则需要使用 DOMParser 对象来解析 html 响应,然后按照自己喜欢的方式进行处理。

这是一个例子

var parser = new DOMParser();
var htmlDoc = parser.parseFromString(txt, 'text/html');
 // do whatever you want with htmlDoc.getElementsByTagName or using htmlDoc.querySelector

在你的情况下,你想要所有的 <td> 所以你做类似的事情

// use any selector you would like
var info = htmlDoc.querySelectorAll("tr");

参考: DOMParser - Mozilla

【讨论】:

    猜你喜欢
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2021-05-23
    • 1970-01-01
    • 2022-01-08
    • 2021-12-20
    • 2018-07-15
    相关资源
    最近更新 更多