JavaScript the script processing the XML-document

Example of the simple script which can process the XML-document



<HTML>

<head>

<title> </title>

<script language = "javascript">

<!

var xmldoc = new ActiveXObject ("msxml");

var xmlsrc = " http: // localhost/xml/journal.xml ";

function viewTitle (elem) {// Display of heading of the document determined by an element <title>

this.document.writeln (' <center> <table width = " 100 % " border=0> <tr> <td width = " 100 % "

align = "center" bgcolor = "silver"> <b> <font color = "black"> ' +elem.text + ' </font> </b> </td> </tr> </table> </center> <br> ');

}

function viewContactsList (elem) {// Display of contents of affiliated elements <author-list>

this.document.writeln (' <tr> <td align = "right" colspan = "2" bgcolor = "gray"> <b> <font color = "white"> our properties </font> </b> </td> </tr> ');

this.document.writeln (' <tr> <td bgcolor = "silver" colspan = "2"> <center> <table width = " 80 % " border=0> ');

if (elem.type == 0) {

if (elem.children! =null) {

this.document.writeln (' <tr> <td colspan=2 width = " 100 % "> </td> </tr> ');

var cur_item=elem.children.item ("address");

if (cur_item! =null) {

this.document.writeln (' <tr> <td> <font color = "blue"> </font </td Address <td align = "right">

         <b> <font color = "red"> ' +cur_item.text + ' </font> </b> </td> </tr> ');

}

var cur_item=elem.children.item ("tel", 0);

if (cur_item! =null) {

this.document.writeln (' <tr> <td> <font color = "blue"> Phone </font> </td> <td align = "right">

         <b> <font color = "red"> ' +cur_item.text + ' </font> </b> </td> </tr> ');

} var cur_item=elem.children.item ("email");

if (cur_item! =null) {

this.document.writeln (' <tr> <td> <font color = "blue"> E-Mail </font> </td> <td align = "right">

         <b> <font color = "red"> ' +cur_item.text + ' </font> </b> </td> </tr> ');

}

var cur_item=elem.children.item ("url");

if (cur_item! =null) {

this.document.writeln (' <tr> <td> <font color = "blue"> URL </font> </td> <td align = "right">

         <b> <font color = "red"> ' +cur_item.text + ' </font> </b> </td> </tr> ');

}

}

}

this.document.writeln (' <tr> <td colspan=2 width = " 100 % "> </td> </tr> ');

this.document.writeln (' </table> </center> </td> </tr> ');

}

function viewAuthorsList (elem) {// Display of contents of affiliated elements <author-list>

this.document.writeln (' <tr> <td align = "right" colspan = "2" bgcolor = "gray"> <b> <font color = "white"> our authors </font> </b> </td> </tr> ');

this.document.writeln (' <tr> <td bgcolor = "silver" colspan = "2"> <center> <table width = " 80 % " border=0> ');

if (elem.type == 0) {

if (elem.children! =null) {

for (i=0; i <elem.children.length; i ++) {

var cur_author = elem.children.item ("author", i);

this.document.writeln (' <tr> <td colspan=2 width = " 100 % "> </td> </tr> ');

if (cur_author.children! =null) {

var cur_item=cur_author.children.item ("firstname");

if (cur_item! =null) {

this.document.writeln (' <tr> <td> <font color = "blue"> the Name </font> </td> <td align = "right">

         <b> <font color = "red"> ' +cur_item.text + ' </font> </b> </td> </tr> ');

}

var cur_item=cur_author.children.item ("lastname");

if (cur_item! =null) {

this.document.writeln (' <tr> <td> <font color = "blue"> the Surname </font> </td> <td align = "right">

         <b> <font color = "red"> ' +cur_item.text + ' </font> </b> </td> </tr> ');

}

var cur_item=cur_author.children.item ("email");

if (cur_item! =null) {

this.document.writeln (' <tr> <td> <font color = "blue"> E-Mail </font> </td> <td align = "right">

         <b> <font color = "red"> ' +cur_item.text + ' </font> </b> </td> </tr> ');

}

}

}

}

}

this.document.writeln (' </table> </center> </td> </tr> ');

}

function viewError () {

this.document.writeln (' <center> <hr> Error was detected ');

}

function parse (root) {

if (root == null) return;

var i=0;

var elem;

if (root.children! =null) {// If the enclosed elements have not been determined, property children will be is established in null

this.document.writeln (' <center> <table width = " 80 % " border=0> <tr> <td> ');

// Perebor affiliated elements

for (i=0; i <root.children.length; i ++) {

elem=root.children.item (I);

if (root.children.item (i) .tagName == "TITLE") {

viewTitle (elem); // Analysis of subelements <title>

}

if (elem.tagName == "CONTACTS") {

viewContactsList (elem); // Analysis of subelements <contacts>

}

if (elem.tagName == "AUTHORS-LIST") {

viewAuthorsList (elem); // Analysis of subelements <authors-list>

}

}

this.document.writeln (' </td> </tr> </table> ');

}

}

function viewDocument () {

xmldoc. URL = xmlsrc; // Loading XML of the document

this.document.writeln (' <body bgcolor = "white"> ');

parse (xmldoc.root); // the Beginning of analysis of the document

this.document.writeln (' </body> ');

}

// Generating page

viewDocument ();

//>

</script>

</head>