Simple XML Question

wessleym

OSNN Addict
Joined
5 Mar 2003
Messages
99
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.
 
first you would need to write an XML Document Type to define all the elements, then you have to write an XML parser
 
Hmm, I have no clue what that means. Could you or someone else explain further?
 
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
 
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>
 
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.
 
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.
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back