我如何使用JavaScript(nodejs)在此HTML文档中获得特定字段

发布于 2025-02-08 10:49:44 字数 956 浏览 1 评论 0原文

<Response>
<SMSMessageData>
    <Message>Sent to 1/1 Total Cost: NGN 2.2000</Message>
    <Recipients>
        <Recipient>
            <number>+9109929199111</number>
            <cost>NGN 2.2000</cost>
            <status>Success</status>
            <statusCode>101</statusCode>
            <messageId>ATXid_f615eb5c6e901459e52d67d045a55355</messageId>
            <messageParts>1</messageParts>
        </Recipient>
    </Recipients>
</SMSMessageData>

我只想使用JavaScript(nodejs)代码在数字标签中提取元素.................................................................................................................................................................................................................. ................................................................................. ..............

<Response>
<SMSMessageData>
    <Message>Sent to 1/1 Total Cost: NGN 2.2000</Message>
    <Recipients>
        <Recipient>
            <number>+9109929199111</number>
            <cost>NGN 2.2000</cost>
            <status>Success</status>
            <statusCode>101</statusCode>
            <messageId>ATXid_f615eb5c6e901459e52d67d045a55355</messageId>
            <messageParts>1</messageParts>
        </Recipient>
    </Recipients>
</SMSMessageData>

I want to just extract the element in the number tag using javascript (nodejs) code...............................................................................................

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅忆流年 2025-02-15 10:49:45

您需要使用一个解析器,例如 Cheerio

然后您需要阅读/提供数据( HTML),然后您可以使用其API选择所需的数据:

const cheerio = require('cheerio');

const data = `
<Response>
<SMSMessageData>
    <Message>Sent to 1/1 Total Cost: NGN 2.2000</Message>
    <Recipients>
        <Recipient>
            <number>+9109929199111</number>
            <cost>NGN 2.2000</cost>
            <status>Success</status>
            <statusCode>101</statusCode>
            <messageId>ATXid_f615eb5c6e901459e52d67d045a55355</messageId>
            <messageParts>1</messageParts>
        </Recipient>
    </Recipients>
</SMSMessageData>
</Response>
`;


const $ = cheerio.load(data);

const num = $('number').text();

console.log(num);

you need to use a parser such as cheerio

then you need to read/provide data to it (the HTML), and then you can select the data you need by using its API:

const cheerio = require('cheerio');

const data = `
<Response>
<SMSMessageData>
    <Message>Sent to 1/1 Total Cost: NGN 2.2000</Message>
    <Recipients>
        <Recipient>
            <number>+9109929199111</number>
            <cost>NGN 2.2000</cost>
            <status>Success</status>
            <statusCode>101</statusCode>
            <messageId>ATXid_f615eb5c6e901459e52d67d045a55355</messageId>
            <messageParts>1</messageParts>
        </Recipient>
    </Recipients>
</SMSMessageData>
</Response>
`;


const $ = cheerio.load(data);

const num = $('number').text();

console.log(num);
甩你一脸翔 2025-02-15 10:49:45

使用getElementsbytagname将使用指定标签返回元素(在您的情况下numbern tag)。然后,您可以选择第一个元素使用innertext属性来提取元素的文本值。

const el = document.getElementsByTagName('number')[0];
console.log(el.innerText);
<Response>
<SMSMessageData>
    <Message>Sent to 1/1 Total Cost: NGN 2.2000</Message>
    <Recipients>
        <Recipient>
            <number>+9109929199111</number>
            <cost>NGN 2.2000</cost>
            <status>Success</status>
            <statusCode>101</statusCode>
            <messageId>ATXid_f615eb5c6e901459e52d67d045a55355</messageId>
            <messageParts>1</messageParts>
        </Recipient>
    </Recipients>
</SMSMessageData>

Using the getElementsByTagName which will return the element with the specified tag (in your case the number tag). then you can select the first element use the innerText property to extract the text value of the element.

const el = document.getElementsByTagName('number')[0];
console.log(el.innerText);
<Response>
<SMSMessageData>
    <Message>Sent to 1/1 Total Cost: NGN 2.2000</Message>
    <Recipients>
        <Recipient>
            <number>+9109929199111</number>
            <cost>NGN 2.2000</cost>
            <status>Success</status>
            <statusCode>101</statusCode>
            <messageId>ATXid_f615eb5c6e901459e52d67d045a55355</messageId>
            <messageParts>1</messageParts>
        </Recipient>
    </Recipients>
</SMSMessageData>

转瞬即逝 2025-02-15 10:49:45

为此,只需告诉系统通过调用document.queryselector(“ number”) ,然后使用innertext字符串。使用下面的代码将变量Content设置为数字标签内部的文本。

var numberElement=document.querySelector("number"); //Fetch the element
var content=numberElement.innerText; //Retrieve its contents

To do this, simply tell the system to fetch the HTML element by calling document.querySelector("number"), and then use the innerText property to get the text content of the string. Use the code below to set a variable content to the text inside of the number tag.

var numberElement=document.querySelector("number"); //Fetch the element
var content=numberElement.innerText; //Retrieve its contents
清晨说晚安 2025-02-15 10:49:45

您可以通过其标签名选择一个字段,但是它不是一个好的解决方案(因为将来您可能会有其他数字字段)。更好的解决方案是使用ID。
喜欢:

const number=document.getElementById('ID').innerText;

或:

const number=document.querySelector('#ID').innerText;

You can select a field by it's TagName, however its not a good solution (because you might have some other number fields in future). the better solution is using ID's.
like:

const number=document.getElementById('ID').innerText;

or:

const number=document.querySelector('#ID').innerText;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文