Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > parsing XHTML with JavaScript?

Reply
Thread Tools

parsing XHTML with JavaScript?

 
 
jackwootton@gmail.com
Guest
Posts: n/a
 
      06-23-2007
Hello everyone,

I understand that XML can be parsed using JavaScript using the XML
Document object. However, it is possible to parse XHTML using
JavaScript? I currently listen for DOMMutation events, when the
events occur I access the node which was inserted or removed
(event.target). There is only ever about 5 lines of XHTML nested in
the node, however it would be silly for me to parse it manually using
methods like hasChildNodes and parentNode if I there is an object that
could do it for me.

Many thanks,

Jack

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      06-24-2007
On Jun 24, 8:03 am, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> Hello everyone,
>
> I understand that XML can be parsed using JavaScript using the XML
> Document object. However, it is possible to parse XHTML using
> JavaScript? I currently listen for DOMMutation events, when the
> events occur I access the node which was inserted or removed
> (event.target). There is only ever about 5 lines of XHTML nested in
> the node, however it would be silly for me to parse it manually using
> methods like hasChildNodes and parentNode if I there is an object that
> could do it for me.


You haven't said what it is that you are trying to do - that is, why
you are parsing the returned XML. The whole point of XML is to
provide structure, but you want to ignore the structure and parse it
yourself. So I've got to ask, why are you using XML?

Perhaps you should be using JSON: <URL: http://www.json.org/ >

You might find textContent useful, it essentially returns the
innerHTML with all the tags stripped. Or you might want get the
actual node that was modified using the event's relatedNode property.
If you are trying to get attributes of the nodes, you really should
use DOM methods.

--
Rob

 
Reply With Quote
 
 
 
 
jackwootton@gmail.com
Guest
Posts: n/a
 
      06-24-2007
On Jun 24, 3:25 am, RobG <(E-Mail Removed)> wrote:
> On Jun 24, 8:03 am, "(E-Mail Removed)" <(E-Mail Removed)>
> wrote:
>
> > Hello everyone,

>
> > I understand that XML can be parsed using JavaScript using the XML
> > Document object. However, it is possible to parse XHTML using
> > JavaScript? I currently listen for DOMMutation events, when the
> > events occur I access the node which was inserted or removed
> > (event.target). There is only ever about 5 lines of XHTML nested in
> > the node, however it would be silly for me to parse it manually using
> > methods like hasChildNodes and parentNode if I there is an object that
> > could do it for me.

>
> You haven't said what it is that you are trying to do - that is, why
> you are parsing the returned XML. The whole point of XML is to
> provide structure, but you want to ignore the structure and parse it
> yourself. So I've got to ask, why are you using XML?
>
> Perhaps you should be using JSON: <URL:http://www.json.org/>
>
> You might find textContent useful, it essentially returns the
> innerHTML with all the tags stripped. Or you might want get the
> actual node that was modified using the event's relatedNode property.
> If you are trying to get attributes of the nodes, you really should
> use DOM methods.
>
> --
> Rob


I'm not parsing XML. I'm parsing XHTML.

 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      06-24-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I'm not parsing XML. I'm parsing XHTML.


XHTML is XML. So an XML parser can deal with it and build an XML DOM, if
the parser knows XHTML then it can even build an XHTML DOM for the XHTML
elements in the namespace http://www.w3.org/1999/xhtml. For instance
with Mozilla you can do

var xmlDoc = new DOMParser().parseFromString([
'<html xmlns="http://www.w3.org/1999/xhtml">',
'<head>',
'<title>Example</title>',
'</head>',
'<body>',
'<p id="p1">Kibology for all.</p>',
'</body>',
'</html>'
].join('\r\n'), 'application/xml');

var ps = xmlDoc.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'p');
var p = ps[0];
alert(p + ': ' + p.id);

and p is an HTMLParagraphpElement having an id property as the parser
has recognized the namespace of the element and built the XHTML DOM.


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
jackwootton@gmail.com
Guest
Posts: n/a
 
      06-24-2007
On Jun 24, 7:08 am, Martin Honnen <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > I'm not parsing XML. I'm parsing XHTML.

>
> XHTML is XML. So an XML parser can deal with it and build an XML DOM, if
> the parser knows XHTML then it can even build an XHTML DOM for the XHTML
> elements in the namespacehttp://www.w3.org/1999/xhtml. For instance
> with Mozilla you can do
>
> var xmlDoc = new DOMParser().parseFromString([
> '<html xmlns="http://www.w3.org/1999/xhtml">',
> '<head>',
> '<title>Example</title>',
> '</head>',
> '<body>',
> '<p id="p1">Kibology for all.</p>',
> '</body>',
> '</html>'
> ].join('\r\n'), 'application/xml');
>
> var ps = xmlDoc.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'p');
> var p = ps[0];
> alert(p + ': ' + p.id);
>
> and p is an HTMLParagraphpElement having an id property as the parser
> has recognized the namespace of the element and built the XHTML DOM.
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/


Thank you. That was my original question (can the XML parser cope
XHTML). But now, it seems a bit of an obvious question. Thank you
for your help, I will give it a go.

Jack

 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      06-24-2007
(E-Mail Removed) wrote:

> That was my original question (can the XML parser cope
> XHTML). But now, it seems a bit of an obvious question.


It depends on the browser and its XHTML support. IE for instance uses
MSXML as its XML parser which is completely separate from the HTML
parser. Thus if MSXML encounters an element in the XHTML namespace it
does not do anything special to provide an XHTML DOM element, rather it
builds an XML DOM element only. So IE with MSXML can parse XHTML but it
does not build an XHTML DOM. That way you are for instance not able to
import XHTML element nodes from an MSXML XML DOM document into the HTML DOM.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
scripts.contact
Guest
Posts: n/a
 
      06-24-2007
On Jun 24, 5:08 am, Martin Honnen <(E-Mail Removed)> wrote:
> For instance
> with Mozilla you can do
>
> var xmlDoc = new DOMParser().parseFromString



just fyi, opera also supports DOMParser and your example works fine
here.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PSD to XHTML Conversion, PSD to HTML, Joomla, Drupal, WordpressConversion, PSD to XHTML CSS xhtml champs XML 0 08-02-2011 05:40 AM
PSD to XHTML Conversion, PSD to HTML, Joomla, Drupal, WordpressConversion, PSD to XHTML CSS xhtml champs C Programming 0 08-01-2011 06:29 AM
convert xhtml to another xhtml using xslt Usha2009 XML 0 12-20-2009 01:13 PM
Should I Convert Site To XHTML or XHTML mobile? chronos3d HTML 9 12-05-2006 04:46 PM
parse URL (href) from xhtml, xhtml -> text, for data hawat.thufir@gmail.com XML 7 02-08-2006 07:39 PM



Advertisments