...

View Full Version : Extracting data using javascript...



doktorn
10-01-2002, 09:15 PM
Hello!

I want to extract data from an xml-sheet using javascript. I have a number of textfields in which I insert some of the data I need. So far so good.

I want to extract data from certain nodes and insert that data into a list. One of the problems is that it has to be done dynamically, it is not to be done a fixed number of times, some times it has to be done one time, other times it has to be done fourteen times.

My xml-sheet looks like this:
<?xml version="1.0" encoding="ISO8859-1" ?>
<personal>
<person>
<anstnr>1</anstnr>
<enamn>Eriksson</enamn>
<fnamn>Rikard</fnamn>
<sign>rier</sign>
<persnr>780101</persnr>
<skiftlag>1</skiftlag>
<disponibel>Ja</disponibel>
<merit>
<meritid>4</meritid>
<kurskod>B1</kurskod>
<exdag>2001-12-01</exdag>
</merit>
</person>
</personal>

I want to extract meritid, kurskod and exdag and place them in an array so that I can extract them by looping through the array. Im planning to use javascript to do the looping part.

I appreciate any suggestions or help :-)

Many thanks in advance

Alex Vincent
10-03-2002, 02:07 AM
OK, to help you answer this we have to explore how to treat XML in IE & Mozilla / Netscape.

In Netscape / Mozilla, you can access the DOM directly from the XML document with a namespaced XHTML script element.

<personal>
<!-- ... -->
<script xmlns="http://www.w3.org/1999/xhtml"><![CDATA[
// JavaScript goes here
]]></script>
</personal>

It's best in this context to enclose all your actions in functions; don't call them before onload if you can help it. (That's why I place the script here at the end of the document -- a practice which is usually avoided like the plague.)

In IE, the best shot you'll have is placing the XML document as an XML island:

<html>
<!-- ... -->
<body>
<!-- ... -->
<xml id="xmlDoc"><!-- xml document fragment goes here --></xml>
</body>
</html>

doktorn
10-03-2002, 09:24 AM
Hello Alex!

I forgot to mention that I will only use IE, sorry.

Ill try to use xml islands.

Thanks for your advice :-)

mpjbrennan
10-03-2002, 01:05 PM
An alternative to xml dataislands is to load the xml document as an activeX object, e.g.

var xml_doc = new ActiveXObject("Microsoft.XMLDOM")
xml_doc.async = false
xml_doc.load("somefile.xml")

patrick

Skyzyx
10-04-2002, 07:09 AM
Here's a post that may help you. Read the part about the XML ticker instead of the Javascript one...

http://www.codingforums.com/showthread.php?s=&threadid=6777



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum