Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript_objects thread: using xml with javascript - it works but I get error message


Message #1 by "Ed Young" <abbylee26@h...> on Thu, 13 Mar 2003 18:19:16
>From: "Ed Young" <abbylee26@h...>
>Reply-To: "JavaScript Objects" <javascript_objects@p...>
>To: "JavaScript Objects" <javascript_objects@p...>
>Subject: [javascript_objects] using xml with javascript - it works but I 
>get error message
>Date: Thu, 13 Mar 2003 18:19:16
>
>I want to be able to access a xml file from an html using JavaScript.
>
>The following code works but I keep getting an error message "Line 10
>Object required"
>line 10 = document.write ("<p>" + company_array.item(i).text);
>any help
>
><html>
>   <body>
>      <script language="JavaScript">
>        xml = new ActiveXobject("Microsoft.XMLDOM");
>        xml.load("fein.xml");
>        fein_array = xml.getElementsByTagName("fein");
>        company_array = xml.getElementsByTagName("company");
>        i = 0;
>        while (i < fein_array.length) {
>          document.write ("<p>" + company_array.item(i).text);
>          i++;
>          }
>       </script>
>   </body>
></html>
>
>My xml is a list of vendors and xml numbers...I created this with ASP.NET
>application from an Access database with 3300 records.
>
><?xml version="1.0" standalone="yes">
>  - <NewDataSet>
>     - <fein>
>          <fein>11-1111111</fein>
>          <company>Johnson and Johnson</company>
>          <phone>555-5555</phone>
>       </fein>
>     - <fein>
>          <fein>11-4444444</fein>
>          <company>The Sex Shope</company>
>          <phone>555-Sexy</phone>
>       </fein>
>    </NewDataSet>
>---
You're over-complicating the code. This isn't helped by having nested "fein" 
nodes. So your fein array will have 4 items. Why not just loop through the 
company items?

var company_array = xml.getElementsByTagName("compamy");
for (var i = 0; i < company_array.length; i++)
{
  document.write("<p>" + company_array[i].text + "</p>");
}

I also suggest setting:
xml.async = false;
before loading the xml document as otherwise with a larger document the 
parser will not have finished building the dom tree before you start 
examining it.

Joe

_________________________________________________________________
Surf together with new Shared Browsing 
http://join.msn.com/?page=features/browse&pgmarket=en-gb&XAPID=74&DI=1059


  Return to Index