views:

189

answers:

2

I have a list of training dates saved into an XML file, and I have a little javascript file that parses all of the training dates and spits them out into a neatly formatted page. This solution was fine until we decided that we wanted another web-page on another sever to access the same XML file.

Since I cannot use JavaScript to parse an XML file that's located on another server, I figured I'd just use an ASP script. However, when I run this following, I get a response that there are 0 nodes matching a value which should have several:

<%
Dim URL, objXML
URL = "http://www.site.com/feed.xml"
Set objXML = Server.CreateObject("MSXML2.DOMDocument.3.0")
objXML.setProperty "ServerHTTPRequest", True
objXML.async =  False
objXML.Load(URL)

If objXML.parseError.errorCode <> 0  Then
    Response.Write(objXML.parseError.reason)
    Response.Write(objXML.parseError.errorCode)
End If

Response.Write(objXML.getElementsByTagName("era").length)
%>

My question is two-fold:

  1. Is there are a way I can use java-script to parse a remote XML file
  2. If not, why doesn't my code give me the proper response?
A: 

I'm not an asp expert but, using plain html and javascript, I would try loading the XML document into an iframe and then use the innerHTML property to access the XML.

document.frames["myXMLframe"].document.innerHTML

Stevko
Browsers don't like the idea of giving access across domain boundaries.
Pointy
I was thinking about that too. Browsers have no problems with cross domain iframes. Locating the script on the same remote server would honor the sandbox restrictions.
Stevko
+1  A: 

Is there are a way I can use java-script to parse a remote XML file

To get around SOP restrictions, you can do it the same way, as it's done with JSONP (just send XML instead)

http://en.wikipedia.org/wiki/JSONP#JSONP

So you would (maybe dynamically) insert a script tag in your page:

<script type="text/javascript" 
        src="http://otherdomain.com/getXml?jsonp=parseXml"&gt;
</script>

And the server would return this content:

parseXml("<?xml version=\"1.0\">...");

Then you only have to define a parseXml(xmlStr) function in your script (but I think you already have one).

Chris Lercher
Nice. I like this approach too. Thanks Chris_I.
Stevko