Current location: Hot Scripts Forums » General Web Coding » JavaScript » Advice parsing a html table to xml


Advice parsing a html table to xml

Reply
  #1 (permalink)  
Old 03-15-05, 07:22 AM
RossC0 RossC0 is offline
Newbie Coder
 
Join Date: Jun 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Advice parsing a html table to xml

Hi,

I've got a html table - which I want to read with javascript and create an xml string - with the table values.

My table also has some attributes stored in the row or cell tags with specific information.

i.e.
Code:
<table id="mytable" border="1">
	<tr row="1">
		<td col="1" value="100"><a href="breakdown.php?row=1&col=1">100</a></td>
	</tr>
</table>
What is the best way to get a list of the definied attributes ? (i.e. id and border)

So far I have:
Code:
table = document.getElementById('thetable');

XML = '<table ';
if (table.attributes.length > 0){
	for (x=0; x<table.attributes.length; x++){
		var XML = XML + ' '+table.attributes[x].nodeName+'="'+table.attributes[x].nodeValue+'"';
	}
}
XML = XML + '/>';
alert(XML);
Which works fine in Firefox - However, in IE its picking up all generic tags which arent set i.e. onmousedown, onclick etc..

[EDITED] - Ignore logic flaw caused a nice loop! Gotta love it when you put an alert in it as well!
**Also - when I extract the logic to loop the element and put it in another function I get a endless loop - even though there is only two attributes ??
[/EDITED]


Is there a way just to get the defined attributes?

Last edited by RossC0; 03-15-05 at 07:50 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-15-05, 06:48 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Not sure, I've never had this problem... But why not add a check to the loop.
Something like

Code:
XML = '<table ';
if (table.attributes.length > 0){
	for (x=0; x<table.attributes.length; x++){
		att=table.attributes[x].nodeName
		if(att=="id" || att=="border"){
		XML  += ' '+table.attributes[x].nodeName+'="'+table.attributes[x].nodeValue+'"';
		}
	}
}
XML = XML + '/>';
alert(XML);
That might work. Of course you can change the if-statement so it ignores some attributes and maybe all the events starting with on.... etc.

That's the only way of doing I know of so far... but it just came of the top of my head so maybe there's a better way.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-16-05, 04:19 AM
RossC0 RossC0 is offline
Newbie Coder
 
Join Date: Jun 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

My logic was flawed for separating the logic to a separate function - and now I've fixed the infinate loop.

I've found a fix for IE picking up attributes which aren't set - as shown here as part of my loop syntax - but it still doesnt cover all of them!:

Code:
attribute[x].nodeValue != null && attribute[x].nodeValue != '' && attribute[x].nodeValue != 'inherit'
Basically, IE sets some of an elements nodes ie.
onmouseover=null by default.
class='' by default (so cant catch with a != null statement!)
contentEditable = 'inherit' by default
and on..

So bascially when I look through the DOM Inspector in firefox - it shows me the two element nodeNames that I have set - in this case border and id.

So when I loop I'm only looping two elements - IE tells me there are 95 elements (with various default values).

I dont want to loop all the 95 elements - and as I dynamically have different nodeNames set for different elements.

Does anybody know if there is a way in IE that will just get the set nodeNames and not include the default ones ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-22-05, 03:10 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
If you only want the border and id values, then don't loop at all. Just get those two.
Otherwise, I don't think you can get around it...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
html tutoral thefrtman HTML/XHTML/XML 5 04-27-09 11:25 AM
XL to html Table -- thomasjojo PHP 2 06-11-04 09:07 PM
html page or database table to send by email rpain ASP 5 04-05-04 10:04 AM
Collecting html form data and sending it to a sql table crobinson ASP 3 01-04-04 10:16 PM
extracting price quantity pairs from HTML table dewed PHP 3 12-11-03 03:31 PM


All times are GMT -5. The time now is 09:03 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.