Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loading xml file online

Status
Not open for further replies.

kenche

Technical User
Jun 6, 2001
3
CA
hi,
I've just completed a htm file that can load a xml file and apply sorts and filters. Everything works fine locally. For some strange reason, the same files stopped working when placed online. I get the error "The data necessary to complete this operation is not yet available." Upon tracing through the script, I find that the problem is in the load command (line 2)
1. xmldoc = new ActiveXObject("MSXML.DOMDocument");
2. xmldoc.load("addressBook.xml");
When I run the script locally, xmldoc.xml would give me the entire xml tree in the debugger. However, the same command online gives me an empty string. I'm sure the addressBook.xml file is uploaded correctly and there are no spelling errors. Please advise.
 
Is this code in ASP or JavaScript?

Either way you may want to try the following definition instead:

xmldoc = new ActiveXObject("microsoft.xmldom");

or

xmldoc = new ActiveXObject("microsoft.XMLDOM");

They seem to vary depending on the type of server you are running, but the first seems to be almost universal.
 
My code is done in JavaScript. I've tried replacing xmldoc = new ActiveXObject("MSXML.DOMDocument"); to either of your definitions and I get the exact same results as before; the script works locally but not on the server. I'll included my source files below. My xml files look like:
<?xml version = &quot;1.0&quot;?>
<contacts>
<contact>
<FirstName>Robert</FirstName>
<LastName>Facca</LastName>
<JobTitle>IT Director</JobTitle>
<Phone>982-5298</Phone>
<Pager>416-753-1418</Pager>
<Email>Robert.Facca@TD.com</Email>
</contact>
</contacts>

My html file is as follows:
<!DOCTYPE HTML PUBLIC &quot;_//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<HTML>
<HEAD><TITLE>NWS Address Book</TITLE>
<link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;TD.css&quot;>
<STYLE>
.head2 {font: bold; cursor: hand}
</STYLE>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
function checkIt(e)
{
var charCode
//saves the charCode to the key pressed
if (navigator.appName.indexOf(&quot;Microsoft Internet Explorer&quot;) >= 0)
{ charCode = e.keyCode
}
else
{
charCode = e.which
}

if (charCode == 13)
{ filter();
}
return true
}

// resets the address book to its original form
function revert()
{
xmldoc = new ActiveXObject(&quot;microsoft.xmldom&quot;);
xmldoc.load(&quot;addressBook.xml&quot;);

xmldoc.documentElement.transformNodeToObject(xmldoc.documentElement,
xmlData.XMLDocument);
}

// filters the addres book according to the search criteria
function filter()
{
var subTree;
var parentTree;
var newTree;
var filterPath;
var searchString;
var field;

// xmldoc = new ActiveXObjec(&quot;MSXML.DOMDocument&quot;);
// xmldoc = new ActiveXObject(&quot;microsoft.xmldom&quot;);
xmldoc = new ActiveXObject(&quot;microsoft.XMLDOM&quot;);
xmldoc.load(&quot;addressBook.xml&quot;);


// gets the field that we wanted to search
field = document.frmAddress.lstFilter.options[document.frmAddress.lstFilter.selectedIndex].value
// gets the value we want to match to
searchString = frmAddress.txtFilter.value;
// filters all contact nodes
filterPath = &quot;/contacts/contact&quot;;
// applies the filter
subTree = xmldoc.selectNodes(filterPath);
// filter it further by the search criteria
newTree = filterNodes(subTree, field, searchString);
// checks that the filter has a result
if (newTree == null)
{ alert(&quot;That value does not exist. Please try another value.&quot;);
frmAddress.txtFilter.focus();
return;
}

newTree.documentElement.transformNodeToObject(newTree.documentElement,
xmlData.XMLDocument);

}

// takes the subtree and returns portions where field = searchString (ie LastName = Cheung)
// partial matches are permitted
function filterNodes(subTree, field, searchString)
{
var objDOM;
var match;

match = false;
objDOM = new ActiveXObject(&quot;microsoft.xmldom&quot;);

// creates the root element
var objNode;
objNode = objDOM.createElement(&quot;contacts&quot;);
objDOM.appendChild(objNode);

// loop for all nodes in the nodeList
for (var i=0; i<subTree.length; i++)
{
// adds the node to the new tree if field = searchString (LastName = Cheung)
if (matchString(subTree.item(i), field, searchString))
{ match = true;
objDOM.documentElement.appendChild(subTree.item(i));
}
}

// returns result tree, or null if tree is empty
if (match)
{ return objDOM; }
else
{ return null; }
}

// returns true if field matches searchString, partial matches are permitted
function matchString(contactTree, field, searchString)
{
var fieldNode;
var objNode;

// gets the first contact node
objNode = contactTree.firstChild;

// loop for all fields in the node
while (objNode.nextSibling != null)
{
// first matches the field in question
if (objNode.nodeName == field)
{
// then matches the searchString
// value starts with searchString is sufficient for a match
// if (objNode.firstChild.nodeValue.toLowerCase() == searchString.toLowerCase())
if (objNode.firstChild.nodeValue.toLowerCase().indexOf(searchString.toLowerCase()) == 0)
{ return true;
}
}
objNode = objNode.nextSibling;
}

return false;
}

-->
</SCRIPT>

</HEAD>
<BODY>

<TABLE BORDER='0' WIDTH='100%'>
<TR>
<TD><A HREF='Nws_main.htm' TARGET='_top'><img src='home.gif' width='57' height='52' ALT='Home' BORDER='0'/></A></TD>
<TD><h1>Network Services Address Book</h1></TD>
<TD WIDTH=&quot;57&quot;></TD>
</TR>
</TABLE>

<XML ID = &quot;xmlData&quot; src = &quot;addressBook.xml&quot;></XML>

<XML ID = &quot;xmlSortLastName&quot;>
<contacts>
<xsl:for-each order-by = &quot;+LastName;+FirstName&quot;
select = &quot;contact&quot;
xmlns:xsl = &quot; <contact>
<FirstName><xsl:value-of select = &quot;FirstName&quot; />
</FirstName>
<LastName><xsl:value-of select = &quot;LastName&quot; />
</LastName>
<JobTitle><xsl:value-of select = &quot;JobTitle&quot; />
</JobTitle>
<Phone><xsl:choose><xsl:when test = &quot;Phone[.!='']&quot;><xsl:value-of select = &quot;Phone&quot;/></xsl:when><xsl:eek:therwise>n/a</xsl:eek:therwise></xsl:choose>
</Phone>
<Pager><xsl:choose><xsl:when test = &quot;Pager[.!='']&quot;><xsl:value-of select = &quot;Pager&quot;/></xsl:when><xsl:eek:therwise>n/a</xsl:eek:therwise></xsl:choose>
</Pager>
<Email><xsl:value-of select = &quot;Email&quot; />
</Email>
</contact>
</xsl:for-each>
</contacts>
</XML>

<XML ID = &quot;xmlSortFirstName&quot;>
<contacts>
<xsl:for-each order-by = &quot;+FirstName;+LastName&quot;
select = &quot;contact&quot;
xmlns:xsl = &quot; <contact>
<FirstName><xsl:value-of select = &quot;FirstName&quot; />
</FirstName>
<LastName><xsl:value-of select = &quot;LastName&quot; />
</LastName>
<JobTitle><xsl:value-of select = &quot;JobTitle&quot; />
</JobTitle>
<Phone><xsl:choose><xsl:when test = &quot;Phone[.!='']&quot;><xsl:value-of select = &quot;Phone&quot;/></xsl:when><xsl:eek:therwise>n/a</xsl:eek:therwise></xsl:choose>
</Phone>
<Pager><xsl:choose><xsl:when test = &quot;Pager[.!='']&quot;><xsl:value-of select = &quot;Pager&quot;/></xsl:when><xsl:eek:therwise>n/a</xsl:eek:therwise></xsl:choose>
</Pager>
<Email><xsl:value-of select = &quot;Email&quot; />
</Email>
</contact>
</xsl:for-each>
</contacts>
</XML>

<XML ID = &quot;xmlSortJobTitle&quot;>
<contacts>
<xsl:for-each order-by = &quot;+JobTitle&quot;
select = &quot;contact&quot;
xmlns:xsl = &quot; <contact>
<FirstName><xsl:value-of select = &quot;FirstName&quot; />
</FirstName>
<LastName><xsl:value-of select = &quot;LastName&quot; />
</LastName>
<JobTitle><xsl:value-of select = &quot;JobTitle&quot; />
</JobTitle>
<Phone><xsl:choose><xsl:when test = &quot;Phone[.!='']&quot;><xsl:value-of select = &quot;Phone&quot;/></xsl:when><xsl:eek:therwise>n/a</xsl:eek:therwise></xsl:choose>
</Phone>
<Pager><xsl:choose><xsl:when test = &quot;Pager[.!='']&quot;><xsl:value-of select = &quot;Pager&quot;/></xsl:when><xsl:eek:therwise>n/a</xsl:eek:therwise></xsl:choose>
</Pager>
<Email><xsl:value-of select = &quot;Email&quot; />
</Email>
</contact>
</xsl:for-each>
</contacts>
</XML>

<XML ID = &quot;xmlSortPhone&quot;>
<contacts>
<xsl:for-each order-by = &quot;+Phone&quot;
select = &quot;contact&quot;
xmlns:xsl = &quot; <contact>
<FirstName><xsl:value-of select = &quot;FirstName&quot; />
</FirstName>
<LastName><xsl:value-of select = &quot;LastName&quot; />
</LastName>
<JobTitle><xsl:value-of select = &quot;JobTitle&quot; />
</JobTitle>
<Phone><xsl:choose><xsl:when test = &quot;Phone[.!='']&quot;><xsl:value-of select = &quot;Phone&quot;/></xsl:when><xsl:eek:therwise>n/a</xsl:eek:therwise></xsl:choose>
</Phone>
<Pager><xsl:choose><xsl:when test = &quot;Pager[.!='']&quot;><xsl:value-of select = &quot;Pager&quot;/></xsl:when><xsl:eek:therwise>n/a</xsl:eek:therwise></xsl:choose>
</Pager>
<Email><xsl:value-of select = &quot;Email&quot; />
</Email>
</contact>
</xsl:for-each>
</contacts>
</XML>

<XML ID = &quot;xmlSortPager&quot;>
<contacts>
<xsl:for-each order-by = &quot;+Pager&quot;
select = &quot;contact&quot;
xmlns:xsl = &quot; <contact>
<FirstName><xsl:value-of select = &quot;FirstName&quot; />
</FirstName>
<LastName><xsl:value-of select = &quot;LastName&quot; />
</LastName>
<JobTitle><xsl:value-of select = &quot;JobTitle&quot; />
</JobTitle>
<Phone><xsl:choose><xsl:when test = &quot;Phone[.!='']&quot;><xsl:value-of select = &quot;Phone&quot;/></xsl:when><xsl:eek:therwise>n/a</xsl:eek:therwise></xsl:choose>
</Phone>
<Pager><xsl:choose><xsl:when test = &quot;Pager[.!='']&quot;><xsl:value-of select = &quot;Pager&quot;/></xsl:when><xsl:eek:therwise>n/a</xsl:eek:therwise></xsl:choose>
</Pager>
<Email><xsl:value-of select = &quot;Email&quot; />
</Email>
</contact>
</xsl:for-each>
</contacts>
</XML>

<XML ID = &quot;xmlSortEmail&quot;>
<contacts>
<xsl:for-each order-by = &quot;+Email&quot;
select = &quot;contact&quot;
xmlns:xsl = &quot; <contact>
<FirstName><xsl:value-of select = &quot;FirstName&quot; />
</FirstName>
<LastName><xsl:value-of select = &quot;LastName&quot; />
</LastName>
<JobTitle><xsl:value-of select = &quot;JobTitle&quot; />
</JobTitle>
<Phone><xsl:choose><xsl:when test = &quot;Phone[.!='']&quot;><xsl:value-of select = &quot;Phone&quot;/></xsl:when><xsl:eek:therwise>n/a</xsl:eek:therwise></xsl:choose>
</Phone>
<Pager><xsl:choose><xsl:when test = &quot;Pager[.!='']&quot;><xsl:value-of select = &quot;Pager&quot;/></xsl:when><xsl:eek:therwise>n/a</xsl:eek:therwise></xsl:choose>
</Pager>
<Email><xsl:value-of select = &quot;Email&quot; />
</Email>
</contact>
</xsl:for-each>
</contacts>
</XML>

<SCRIPT LANGUAGE=&quot;Javascript&quot;>
<!--
var xmldoc;

function sort( xsldoc )
{
xmldoc = xmlData.cloneNode( true );

xmldoc.documentElement.transformNodeToObject(
xsldoc.documentElement, xmlData.XMLDocument );
}
-->
</SCRIPT>
<FORM NAME='frmAddress'>
<SELECT NAME=&quot;lstFilter&quot;>
<OPTION SELECTED=&quot;true&quot; VALUE=&quot;FirstName&quot;>First Name</OPTION>
<OPTION VALUE=&quot;LastName&quot;>Last Name</OPTION>
<OPTION VALUE=&quot;JobTitle&quot;>Job Title</OPTION>
<OPTION VALUE=&quot;Phone&quot;>Phone</OPTION>
<OPTION VALUE=&quot;Pager&quot;>Pager</OPTION>
<OPTION VALUE=&quot;Email&quot;>Email</OPTION>
</SELECT>
<INPUT TYPE=&quot;text&quot; SIZE=&quot;15&quot; NAME=&quot;txtFilter&quot; onKeyPress=&quot;return checkIt(event);&quot;>
<span style='left: -50; top: -50; position: absolute; z-index: 0'><INPUT TYPE=&quot;text&quot; SIZE=&quot;1&quot; TABINDEX=&quot;10&quot;></SPAN>

<INPUT TYPE = &quot;button&quot; ACCESSKEY=&quot;f&quot; VALUE = &quot;Filter&quot; onClick = &quot;filter();&quot;>
<INPUT TYPE = &quot;button&quot; ACCESSKEY=&quot;r&quot; VALUE=&quot;Revert&quot; onClick = &quot;revert()&quot;>
</FORM>
<TABLE BORDER = &quot;1&quot; DATASRC = &quot;#xmlData&quot; DATAPAGESIZE = &quot;50&quot;
ID = &quot;tbl&quot;>
<THEAD>
<TR>
<TH><DIV onClick=&quot;sort(xmlSortFirstName.XMLDocument)&quot; CLASS = &quot;head2&quot;>First Name</DIV></TH>
<TH><DIV onClick=&quot;sort(xmlSortLastName.XMLDocument)&quot; CLASS = &quot;head2&quot;>Last Name</DIV></TH>
<TH><DIV onClick=&quot;sort(xmlSortJobTitle.XMLDocument)&quot; CLASS = &quot;head2&quot;>Job Title</DIV></TH>
<TH><DIV onClick=&quot;sort(xmlSortPhone.XMLDocument)&quot; CLASS = &quot;head2&quot;>Phone</DIV></TH>
<TH><DIV onClick=&quot;sort(xmlSortPager.XMLDocument)&quot; CLASS = &quot;head2&quot;>Pager</DIV></TH>
<TH><DIV onClick=&quot;sort(xmlSortEmail.XMLDocument)&quot; CLASS = &quot;head2&quot;>Email</DIV></TH>
</TR>
</THEAD>

<TR>
<TD><SPAN DATAFLD = &quot;FirstName&quot;></SPAN></TD>
<TD><SPAN DATAFLD = &quot;LastName&quot;></SPAN></TD>
<TD><SPAN DATAFLD = &quot;JobTitle&quot;></SPAN></TD>
<TD WIDTH=&quot;15%&quot;><SPAN DATAFLD = &quot;Phone&quot;></SPAN></TD>
<TD><SPAN DATAFLD = &quot;Pager&quot;></SPAN></TD>
<TD><SPAN DATAFLD = &quot;Email&quot;></SPAN></TD>
</TR>
</TABLE>

</BODY>
</HTML>
 
Hello,
If you were using ASP/VBScript I would recommend using
xmldoc.load (Server.MapPath(&quot;xmldoc.xml&quot;))
MapPath - maps the specified virtual path, either the absolute path on the current server or the path relative to the current page, into a physical path.
See also thread thread426-88368.

But when you are using JavaScript I found only one example in MSDN with hardcoding your server name like this:
xmldoc.load (&quot;
Hope this helps.
D.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top