Reply
Old May 23rd, 2004 Top | #1
 
wessleym's Avatar
OSNN Addict
Joined: March 2003
Posts: 99
Reputation: 0
Power: 113

Default Simple XML Question

From an HTML web page, how would I be able to retrieve data from an XML file? For example say I had a small database of phone numbers, and I wanted to retrieve one and write it to the web page.
-XML File-
<numbers>
<tim>555-1234</tim>
</numbers>
-HTML File-
<html>
<head>
<script>document.write(XML.numbers.tim)</script>
</head>
</html>

I know that's not all quite correct, but is there a way I could do something like that in such an easy way? Thanks.
wessleym is offline   Reply With Quote
Old May 23rd, 2004 Top | #2

OSNN Folding Team  
Geffy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: United Kingdom
Posts: 7,805
Reputation: 1490
Power: 217

Default

first you would need to write an XML Document Type to define all the elements, then you have to write an XML parser


• blog • tumblog • lastfm • flickr • #rubyonrails • @twitter
"I could be replaced with a very small shell script"
Geffy is offline   Reply With Quote
Old May 23rd, 2004 Top | #3
 
wessleym's Avatar
OSNN Addict
Joined: March 2003
Posts: 99
Reputation: 0
Power: 113

Default

Hmm, I have no clue what that means. Could you or someone else explain further?
wessleym is offline   Reply With Quote
Old May 24th, 2004 Top | #4

OSNN Folding Team  
Geffy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: United Kingdom
Posts: 7,805
Reputation: 1490
Power: 217

Default

well basically you have to have a Document Type Definition (DTD) which defines the tags and their heirarchy, then you would have to create the XML document with the link to the DTD in the header
To convert the XML to HTML you either have to use an XML parser which takes the XML document and somehow fits the data into HTML or an XSLT which is a special kind of style sheet for defining how certain elements should be rendered
I'll see if I can knock together a quick XML DTD which you could use


• blog • tumblog • lastfm • flickr • #rubyonrails • @twitter
"I could be replaced with a very small shell script"
Geffy is offline   Reply With Quote
Old May 24th, 2004 Top | #5

OSNN Folding Team  
Geffy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: United Kingdom
Posts: 7,805
Reputation: 1490
Power: 217

Default

The XML Document Type Definition (phonebook.dtd)
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT phonebook (name+)>
<!ELEMENT name (#PCDATA)>
<!ATTLIST name
	number CDATA #REQUIRED
>
The XML document
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE phonebook SYSTEM "phonebook.dtd">
<phonebook>
  <name number="5551234">Tim Jones</name>
  <name number="5556980">Jem Wilde</name>
  <name number="5559730">Judy Wilson</name>
</phonebook>


• blog • tumblog • lastfm • flickr • #rubyonrails • @twitter
"I could be replaced with a very small shell script"
Geffy is offline   Reply With Quote
Old May 24th, 2004 Top | #6
 
wessleym's Avatar
OSNN Addict
Joined: March 2003
Posts: 99
Reputation: 0
Power: 113

Default

An what should the HTML/JavaScript look like? In other words, how to I get this information from the XML and write it to the web page.
wessleym is offline   Reply With Quote
Old May 26th, 2004 Top | #7
 
kid23's Avatar
OSNN Junior Addict
Joined: May 2004
Posts: 8
Reputation: 0
Power: 99

Default

I've had the same issue, and here's how I fixed it. First of all my page isn't in HTML, but in XML, I've rewritten a few templates in XSL that transform my XML into HTML, nothing tricky there.

In my transformation, I wanted to get data from an XML file other than the one being transformed, and there's an easy way to do it, without using javascript at all.

If you use this syntax:

<xsl:value-of select="document('myfile.xml')/tag01/tag02/tag03" />

you'll get the value you're looking for. if you wanna know more about the syntax, refer to XPath tutorials, you'll see what's possible when it comes to fetching some specific tags into an external file.

Let me know if this helps. I know you were looking for HTML rather than XML, but playing with XSLT is not that complicated, and will considerably simplify the development of a page.
kid23 is offline   Reply With Quote

Reply

Bookmarks

Thread Tools

Posting Rules

Similar Threads
Thread Thread Starter Forum Replies Last Post
simple question ProtocolX Web Design & Coding 3 December 16th, 2003 1:05am
Just a simple question... PlagueWielder Windows Desktop Systems 15 October 6th, 2003 8:10pm
Plz help.. simple question HellyHans Windows Desktop Systems 3 June 6th, 2003 7:45pm
Simple question..... robin.munro Windows Desktop Systems 3 July 3rd, 2002 12:24am
simple question jams Windows Desktop Systems 8 April 29th, 2002 3:17pm