Jump to content

Javascript accessing xml.


swan52

Recommended Posts

What I am looking to do is access a set of xml files and put them in tables. There are 2 things I wish to do but seem unable to achieve, first I wish to list the titles of the xml files consecutively in a table. This works with 1 xml loaded, however I do not know how to list two or more xml's in the one table.Javascript to get title(s) -

var xmlDoc=null; if (window.ActiveXObject){ xmlDoc=new ActiveXObject("Microsoft.XMLDOM");} else if (document.implementation.createDocument){ xmlDoc=document.implementation.createDocument("","",null);} else{ alert('Your browser cannot handle this script'); }if (xmlDoc!=null) {xmlDoc.async=false;// --------------------------------------------xmlDoc.load("folder/1.xml");// xmlDoc.load("folder/2.xml"); ?????// --------------------------------------------var x=xmlDoc.getElementsByTagName("Testing123");var htmlText="";htmlText+="<table cellspacing='0' cellpadding='1' border='1' style='background-color: white;'>"+"<thead><tr><th>Title1</th><th>Title2</th><th>Title3</th></tr></thead>";//for (var m=0;m<x.length;m++){htmlText+="<td WIDTH=210><center>"+(x[0].attributes.getNamedItem("test1")).value;+"</center></td>";htmlText+="<td WIDTH=150><center>"+(x[0].attributes.getNamedItem("test2")).value;+"</center></td>";htmlText+="<td WIDTH=150><center>"+(x[0].attributes.getNamedItem("test3")).value;+"</center></td>";}htmlText+="<tfoot><tr><th colspan='5'>-</th></tr></tfoot>";htmlText+="</table>";document.write(htmlText)}

The second thing I am trying to do is to list the attributes inside of the xml files, however my problem is that try as I might the list seems to want to go hozontally instead of vertically due I think to each of the elements using the same name.Here is the javascript I was using to attempt to list the attributes -

var xmlDoc=null; if (window.ActiveXObject){ xmlDoc=new ActiveXObject("Microsoft.XMLDOM");} else if (document.implementation.createDocument){ xmlDoc=document.implementation.createDocument("","",null);} else{ alert('Your browser cannot handle this script'); }if (xmlDoc!=null) {xmlDoc.async=false;xmlDoc.load("folder/1.xml");var x=xmlDoc.getElementsByTagName("Testing123");var y=xmlDoc.getElementsByTagName("Test");var htmlText="";htmlText+="<table cellspacing='0' cellpadding='1' border='1' style='background-color: white;'>"+"<thead><tr><th>test1</th><th>test2</th><th>test3</th></tr></thead>";for (var m=0;m<x.length;m++)for (var n=0;n<y.length;n++){htmlText+="<td WIDTH=100><center>"+(x[m].getElementsByTagName("Test")[n].getAttribute("test1"))+"</center></td>";}htmlText+="<tfoot><tr><th colspan='3'>-</th></tr></tfoot>";htmlText+="</table>";document.write(htmlText)}

1.xml for example would go like this -

<Testing123 test1="one" test2="two" test3="three"><Test test1="1" test2="6" test3="11"></Test><Test test1="2" test2="7" test3="12"></Test><Test test1="3" test2="8" test3="13"></Test><Test test1="4" test2="9" test3="14"></Test><Test test1="5" test2="10" test3="15"></Test></Testing123>

2.xml... and so on

<Testing123 test1="four" test2="five" test3="six"></Testing123>

What am I doing wrong in both of these examples?

Link to comment
Share on other sites

1. stop thinking of this as an XML problem. XML is just string data. It sounds like you're accessing it just fine.2. That means this is a table building problem. Look at this code:

htmlText+="<table cellspacing='0' cellpadding='1' border='1' style='background-color: white;'>"+"<thead><tr><th>test1</th><th>test2</th><th>test3</th></tr></thead>";for (var m=0;m<x.length;m++)for (var n=0;n<y.length;n++){htmlText+="<td WIDTH=100><center>"+(x[m].getElementsByTagName("Test")[n].getAttribute("test1"))+"</center></td>";

After you build the <thead> element, your code builds lots of <td> elements, but not one <tr> element till we get to the tfoot.So problem 1, you're asking the browser to understand <td> elements that don't come inside <tr> elements. That's bad news. You're asking the browser to make a judgment call. They don't like that. The result is not predictable. You're lucky if the browser even sticks that content in the table at all.Problem 2, without table rows, of course the result is horizontal. Only the end and beginning of a <tr> will create any sense of verticality.So try adding some <tr></tr> elements to your loop structures.

Link to comment
Share on other sites

With regards to the second part, thanks for the very informative reply pointing this out, I knew I'd missed something silly! However on the first point, I am still not entirely sure as to how I would display 'titles' from multiple xml's, so that they can be viewed inside of a single table. Can I list the filenames, or will I need to create an array? What would be the standard practice to access several files and display their results?

Link to comment
Share on other sites

Oh, I see. Let's keep it simple.For starters, you need a separate function that generates a unique HttpRequest object every time it is called. Something like this:

function getHttpRequest() {   var xmlhttp = false;   if (window.XMLHttpRequest) {	  // IE7+, Firefox, Chrome, Opera, Safari	  xmlhttp = new XMLHttpRequest();   } else {	  // IE6, IE5	  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   }   return xmlhttp;}

. . . and you need unique request objects to store them in. That way you can have multiple xml documents. Something like:

var xmlReq1 = getHttpRequest();var xmlReq2 = getHttpRequest();

or maybe:

var i;var xmlReq = [];var filenames = ["file0.xml","file1.xml","file2.xml","file3.xml"];var len = filenames.length;for (i = 0; i < len; ++i) {   xmlReq[i] = getHttpRequest()   // set properties here}

I hope that's what you're asking. It certainly is something your script should be doing, if you're to have multiple xml documents hanging around at the same time. The code you were playing with in your commented section would simply destroy one request object and replace it with another before you could use it.

Link to comment
Share on other sites

That's great, thanks ever so much.I have been trying to access a large set of records for a while now and somebody I know suggested I learn javascript and HTML due to the information being stored in hundreds upon hundreds of xml files. I feel I have really been dropped in the deep end so far, as I have little to no experience of either javascript or HTML and I really wanted to understand how the information is laid out and how it accessed, so it has been a very steep learning curve so far. Your replies have already answered a lot of questions that have been puzzling me for some time now, so again. Thanks. :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...