473,378 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

parsing XHTML with JavaScript?

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

Jun 23 '07 #1
6 5888
On Jun 24, 8:03 am, "jackwoot...@gmail.com" <jackwoot...@gmail.com>
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

Jun 24 '07 #2
On Jun 24, 3:25 am, RobG <r...@iinet.net.auwrote:
On Jun 24, 8:03 am, "jackwoot...@gmail.com" <jackwoot...@gmail.com>
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.

Jun 24 '07 #3
ja*********@gmail.com 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/
Jun 24 '07 #4
On Jun 24, 7:08 am, Martin Honnen <mahotr...@yahoo.dewrote:
jackwoot...@gmail.com 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

Jun 24 '07 #5
ja*********@gmail.com 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/
Jun 24 '07 #6
On Jun 24, 5:08 am, Martin Honnen <mahotr...@yahoo.dewrote:
For instance
with Mozilla you can do

var xmlDoc = new DOMParser().parseFromString

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

Jun 24 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: wardy | last post by:
I'm trying to undestand the impact of using content negotiation when rendering my Web pages to various different browsers as I would like to use the XHTML Strict DOCTYPE declaration. Reading the...
1
by: gizap | last post by:
I'm trying to parse an XHTML document like this: file.html: <?xml version="1.0" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">...
4
by: JustASymbol | last post by:
I'm not sure if this is possible, but if someone has an idea of how to do this or a link that can point me in the right direction, it would be much appreciated. I have a very simple form with...
0
by: june | last post by:
Hi, I have a big problem with parsing HTML into a XHTML using Cberneko to validate the html. First I tried to work with a HTML-File. This solutions works fine: String aHTMLFile =...
2
by: ICPooreMan | last post by:
I have a page which I'm trying to add ajax functionality to however I'm having some difficulties. Basically I have a section of my page that looks like this <div id="pageDiv"> <!--a random...
2
by: Jason | last post by:
I have a web form that will accept XML as input whose contents need to be namespaced, and inserted into an XML document with a different namespace. I need to take this: Lorem ipsum dolor...
7
by: ArdGre | last post by:
HI THERE, I have a strange problem. I am writing a firefox extension to the Onlywire API (http://onlywire.com/index?api). Now the problem is when I tag pages using the onlywire API the service...
0
by: Mitchel Haas | last post by:
I've noticed several inquiries in the past for libraries/toolkits to generate or parse xhtml. Although there are already a few libraries available for this purpose, I'd like to announce a new...
1
by: avpkills2002 | last post by:
I seem to be getting this weird problem in Internet explorer. I have written a code for parsing a XML file and displaying the output. The code works perfectly fine with ffx(Firefox).However is not...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.