我如何使用JavaScript(nodejs)在此HTML文档中获得特定字段
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要使用一个解析器,例如 Cheerio
然后您需要阅读/提供数据( HTML),然后您可以使用其API选择所需的数据:
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:
使用
getElementsbytagname
将使用指定标签返回元素(在您的情况下numbern
tag)。然后,您可以选择第一个元素使用innertext
属性来提取元素的文本值。Using the
getElementsByTagName
which will return the element with the specified tag (in your case thenumber
tag). then you can select the first element use theinnerText
property to extract the text value of the element.为此,只需告诉系统通过调用
document.queryselector(“ number”)
,然后使用innertext
字符串。使用下面的代码将变量Content
设置为数字标签内部的文本。To do this, simply tell the system to fetch the HTML element by calling
document.querySelector("number")
, and then use theinnerText
property to get the text content of the string. Use the code below to set a variablecontent
to the text inside of the number tag.您可以通过其标签名选择一个字段,但是它不是一个好的解决方案(因为将来您可能会有其他数字字段)。更好的解决方案是使用ID。
喜欢:
或:
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:
or: