1

I have a html page as string like below;

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

and I want to get value from that string via xPath. Xpath like below;

/html/body/p

Result has to be ;

This is a paragraph.

1 Answer 1

1

I solved my problem as below. I wrote a method which is convert html to element.

function htmlToElement(html) {
    var doc = new DOMParser().parseFromString(html, "text/html");
    return doc;
}

I got value like below.

var doc = htmlToElement(htmlString);
xPath = "/html/body/p";
var value = doc.evaluate(xPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext().innerHTML;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.