0

I have the following response from an HTTP get.

<HTML><BODY>Now='11/7/2017 4:08:34 PM' Process='chrome' SessionID=1 User='Local\User' Culture='en-US'<BR></BODY></HTML>

I need to get the data there in the body into a JSON object. So I tried to remove HTML tags. Though it is different, I have tried, as there in this solution. It works for HTML tags but not for <html> itself.

I have tried also as below:

var content = "<HTML><BODY>Now='11/7/2017 4:08:34 PM' Process='chrome' SessionID=1 User='Local\User' Culture='en-US'<BR></BODY></HTML>";

var tag = document.createElement("html");
tag.outerHtml = content;

It gives the following error:

Uncaught DOMException: Failed to set the 'outerHTML' property on 'Element': This element has no parent node.

Though I know that it can be achieved with regex, I want to do it without regex.

Could someone resolve it?

Sivaprasad derangula
  • 1,169
  • 4
  • 12
  • 29

2 Answers2

0

Use DOMParser() to convert the string of HTML into a DOM:

var html = `<HTML><BODY>Now='11/7/2017 4:08:34 PM' Process='chrome' SessionID=1 User='Local\User' Culture='en-US'<BR></BODY></HTML>`;

var parser = new DOMParser();
var html_dom = parser.parseFromString(html, "text/html");
var body = html_dom.querySelector("body");
var content = body.innerHTML;
console.log(content);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-2

Just created a simple codepen. Please try, it shall work:

https://codepen.io/vishalkaului/pen/rYMGoy

+6 and - 16 are to exclude the content before the starting<BODY> tag and after the closing </BODY> tag. It includes both the exclusion for the <BODY></BODY> tags.

(function () {
    let serverResponse = "<HTML><BODY>Now='11/7/2017 4:08:34 PM' 
    Process='chrome' SessionID=1 User='Local\User' Culture='en-
    US'<BR></BODY></HTML>";



 console.log(serverResponse.substr(serverResponse.indexOf('<BODY>')+6,serverResponse.indexOf('</BODY>')-16));
})()

Note: This solution caters only when you know that the structure of the response is same. Though, content can vary but the tags are same before and after the response data.

Vishal Kaul
  • 133
  • 6
  • Please [avoid link only answers](http://meta.stackoverflow.com/tags/link-only-answers/info). Answers that are "barely more than a link to an external site” [may be deleted](http://stackoverflow.com/help/deleted-answers). – Quentin Nov 07 '17 at 11:32
  • Kindly have some patience, I was still in mid of posting the complete solution. – Vishal Kaul Nov 07 '17 at 11:36
  • Patience? Because you were too impatient to finish typing your answer before clicking the "Post Your Answer" button? – Quentin Nov 07 '17 at 11:38
  • "This solution caters only when you know that the structure of the response is same" — Which makes it very fragile and a poor approach to the problem. – Quentin Nov 07 '17 at 11:39
  • Guess, we are here to help each other rather than putting someone else down. – Vishal Kaul Nov 07 '17 at 11:39
  • I agree that's why I mentioned in the `Note` section. It's applicable they know the structure is same. – Vishal Kaul Nov 07 '17 at 11:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158411/discussion-between-vishal-kaul-and-quentin). – Vishal Kaul Nov 07 '17 at 11:45