1

I have a custom html file, with the below code with custom tag "TBD:comment". I want to get content from this tag.

<HTML>
<BODY>
<h1> This is a heading </h1>
<P id='para1'>First Paragraph with some Random text</P>
<P>Second paragraph with more random text</P>
<A href="http://Geekeefy.wordpress.com">Cool Powershell blog</A>
<TBD:comment name="Title"><h3>Katamma katamma loge kathamma</h3> 
</TBD:comment>
<TBD:comment name="content"><h3>Lorem Ipsum is simply dummy text of the 
printing and typesetting industry. Lorem Ipsum has been the industry's 
standard dummy text ever since the 1500s, when an unk</h3> </TBD:comment>
</BODY>
</HTML>

The below code does not seem to work with the custom tag.

enter code here
$html = Get-Content "C:\Users\sahuBaba\Desktop\ht.html" -Raw
$doc = New-Object -com "HTMLFILE"
$doc.IHTMLDocument2_write($html)

$text = $doc.body.getElementsByTagName("TBD:comment")
"Inner Text: " + $text[1].innerText

no output. Can someone please help? Thanks in advance.

1 Answer 1

1

Try with regex:

$regex = New-Object Text.RegularExpressions.Regex "<TBD:comment.+?(>.+?)<\/TBD:comment>", ('singleline', 'multiline')
$content = "<your html>"
foreach($m in $regex.Matches($content)) {
    # remove leading '<'
    $m.Groups[1].Value.Substring(1)
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks.. it works, one small follow up question if i want to get down for a specific name for the same tag <TBD:comment name="Body[0].BodySection">. if you can help me to get that as well. it will be really very helpful. Thanks in advance.
Try this: $regex = New-Object Text.RegularExpressions.Regex '<TBD:comment.+?(".+?")(>.+?)<\/TBD:comment>', ('singleline', 'multiline') $content = "<your html>" foreach($m in $regex.Matches($content)) { # remove leading '<' $m.Groups[1].Value.Trim('"') $m.Groups[2].Value.Substring(1) }

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.