views:

97

answers:

3

I have a string such as <html><body><div id="message">Hello World!</div></body></html> and I would like to get the content of the #message element without parsing the HTML myself.

I thought maybe I could create a document object from a string in Gecko (this is for a Firefox add on) but I don't see any simple way.

I noticed that there is a createDocument method, but that doesn't take a string. I'd have to strip the <html> portion from the text, and then again I'm starting to assume stuff.

Anyone have any ideas? Thanks.

EDIT: This seems to work for me:

doc = document.implementation.createDocument( "http://www.w3.org/1999/xhtml", "html", null );
doc.firstChild.innerHTML = '<html><body><div id="message">Hello World!</div></body></html>';
node = doc.getElementById( "message" ); 
alert( node.innerHTML );
A: 

Is it always that syntax? If so, why not use a regex?

Adam Bard
my thoughts exactly
pstanton
It may or may not be always that syntax.
apphacker
+2  A: 

Don't like answering my own question, but this seems to have worked for me:

doc = document.implementation.createDocument( "http://www.w3.org/1999/xhtml", "html", null );
doc.firstChild.innerHTML = '<html><body><div id="message">Hello World!</div></body></html>';
node = doc.getElementById( "message" ); 
alert( node.innerHTML );
apphacker
Don't forget to declare variables.
kangax
They are declared in a single, comma separated line at the beginning of the method. We use jslint.
apphacker
+2  A: 

Where do you get the string from? If it's XML, you could get away with using DOMParser.

Otherwise, you have to create an HTML document - https://developer.mozilla.org/en/Parsing_HTML_From_Chrome.

The fact that just using createDocument works seems suspicious, because people used more complicated solutions all this time.

Nickolay
I get it from a remote source via an xhr.
apphacker
I will try the domparser method too, just curious if it works and is simpler. Thank you.
apphacker
Yeah, but who controls what format that remote source returns data in?
Nickolay