5

I'm sending a GET request to a WMS service through angular's HttpClient. The response what I get is a html in string. How can I parse data from this string?

The response looks like this:

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
<body>
<p>Temperature 16.6 °C</p>
<p>Temperature 18.2 °C</p>
</body></html>

I need to get the values inside the paragraph tags. I found solutions only to display data through innerHtml directive, but this is not my case.

EDIT:

I found one possible solution through DOMParser:

const parser = new DOMParser();
const doc = parser.parseFromString(myHtmlStringResponse, "text/html");
const values = [];

const p = doc.getElementsByTagName("p");

for (const item of Array.from(p)) {
     values.push(item.textContent);
}
1
  • DOMParser is the simple and robust answer to this question, whereas stripping HTML is an arbitrary answer which may only work in specific circumstances Commented Dec 25, 2020 at 1:34

2 Answers 2

4

Strip all the html from the response with this regEx:

data.replace(/<(?:.|\n)*?>/gm, '');

where data is your response string

You will get this:

Temperature 16.6 °C
Temperature 18.2 °C
Sign up to request clarification or add additional context in comments.

Comments

0

You can use parse through using tag name.

var pele = document.getElementsByTagName('p');

for(var i=0;i< pele.length;i++) {
  console.log(pele[i].innerHTML)
}

But make sure if you are using jquery then put this code snippet under ready function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.