473,738 Members | 4,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5941
On Jun 24, 8:03 am, "jackwoot...@gm ail.com" <jackwoot...@gm ail.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...@gm ail.com" <jackwoot...@gm ail.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*********@gma il.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().par seFromString([
'<html xmlns="http://www.w3.org/1999/xhtml">',
'<head>',
'<title>Example </title>',
'</head>',
'<body>',
'<p id="p1">Kibolog y for all.</p>',
'</body>',
'</html>'
].join('\r\n'), 'application/xml');

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

and p is an HTMLParagraphpE lement 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...@yaho o.dewrote:
jackwoot...@gma il.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().par seFromString([
'<html xmlns="http://www.w3.org/1999/xhtml">',
'<head>',
'<title>Example </title>',
'</head>',
'<body>',
'<p id="p1">Kibolog y for all.</p>',
'</body>',
'</html>'
].join('\r\n'), 'application/xml');

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

and p is an HTMLParagraphpE lement 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*********@gma il.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...@yaho o.dewrote:
For instance
with Mozilla you can do

var xmlDoc = new DOMParser().par seFromString

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
1949
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 W3C guidelines, if I render as 'text/html' then there are some backward compatibility issues I need to worry about. Does this mean that the page must be rendered in one way if I use the "application/xhmlt+xml" content type and another if I use...
1
4266
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"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Some title</title> </head>
4
1248
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 text fields for a username and password. When the user clicks the login button, it uses submit() to send the information to a validation script and then opens a new window with some information that I would like to be able to parse through. I'm...
0
1995
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 = "file:\\C:/work/Eclipse3.1.1/html-file.html"; org.xml.sax.InputSource pSource = new InputSource(aHTMLFile);
2
1476
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 amount of html could be several nodes of different kinds--> </div>
2
2335
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 sit amet, consectetuer <a href="http://www.example.com" title="etc" rel="whatever">Link Here</aadipiscing elit. Vivamus molestie dolor ultrices leo.
7
2979
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 responds with an xml that indicates whether the web page was tagged successfully. Something like this: <?xml version="1.0"?> <result code="success" />
0
1207
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 lightweight library for this purpose. Xport, XHTML Parsing & Objective Reporting Toolkit, is a new open source lightweight library for the purpose of generating and parsing (x)html documents. Although Xport was created for reporting purposes, Xport...
1
4848
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 working in Internet Explorer.(I m using Internet Explorer 6.0). The code is as follows: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.