• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Simple XML Question

#1
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.
 
#4
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
 
#5
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>
 

kid23

OSNN One Post Wonder
#7
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 posts

Latest profile posts

Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,961
Messages
673,239
Members
89,015
Latest member
evaiwhitis