faqts : Computers1 : Programming : Languages : JavaScript : XML

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 3 people (67%) answered Yes
Recently 2 of 3 people (67%) answered Yes

Entry

JavaScript: File: XML: Table: How convert database table to XML, and load it in array? 1D

Apr 15th, 2006 16:21
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 06 April 2021 - 07:19 pm ----------------------
JavaScript: File: XML: Table: How convert database table to XML, and 
load it in array? 1D
---
Steps: Overview:
 1. -Given your list (or thus 1 dimensional (=1D) table)
--- cut here: begin --------------------------------------------------
    --------------
    columnheader1
    --------------
    test11
    test21
    test31
    --------------
--- cut here: end ----------------------------------------------------
 2. -You can convert this table to XML, by
     wrapping each row between the tags <ROW> and </ROW>.
     And inside this row, you wrap each column
     between the tags <COLUMN> and </COLUMN>
--- cut here: begin --------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<TABLE>
 <ROWHEADER>
   columnheader1
 </ROWHEADER>
 <ROW>
   test11
 </ROW>
 <ROW>
   test12
 </ROW>
 <ROW>
   test13
 </ROW>
</TABLE>
--- cut here: end ----------------------------------------------------
 3. -Save this e.g. as the file
      table.xml
 4. -To read this XML file data,
     create e.g. the following file:
     1. -It contains 1 for next loop
         which goes through the rows.
     2. -Using the rowI values you can then e.g. store the
         table values in an array, for further handling with
         e.g. JavaScript.
         1. -E.g.
              myArray[ rowI ] = columnP.item( columnI ).text
--- cut here: begin --------------------------------------------------
<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
 //
 // create a 1 dimensional array (=list) in JavaScript
 var myArray = new Array( 4 );
 //
 var fileP = new ActiveXObject( "Microsoft.XMLDOM" );
 var rootP = null;
 var rowP = null;
 var rowI = 0;
 fileP.load( "table.xml" );
 rootP = fileP.documentElement;
 rowP = rootP.childNodes;
 for ( rowI = 0; rowI <= rowP.length - 1; rowI++ ) {
  alert( "row = " + rowI );
  alert( rowP.item( rowI ).text );
 // store that element in that position in the array
  myArray[ rowI ] = rowP.item( rowI ).text;
 }
 //
 // print the array again, for checking purposes
 //
 for ( rowI = 0; rowI <= rowP.length - 1; rowI++ ) {
  alert( myArray[ rowI ] );
 }
</SCRIPT>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->
--- cut here: end ----------------------------------------------------
 5. -Save it e.g. as
      table.htm
 6. If you load this file
     table.htm
    in your browser, you will see
    the row numbers, followed by
    all the columns of that row, one after the other.
--- cut here: begin --------------------------------------------------
row = 0
columnheader1
---
row = 1
test11
---
row = 2
test12
---
row = 3
test13
---
followed by again a printing out by reading the
array values again
===
Tested successfully on
Microsoft Windows XP Professional (service pack 2),
running
Microsoft Internet Explorer v6.x
===
Internet: see also:
---
JavaScript: XML: Link: Overview: Can you give an overview of links?
http://www.faqts.com/knowledge_base/view.phtml/aid/40517/fid/616
----------------------------------------------------------------------