Simple XML Question

Discussion in 'Web Design & Coding' started by wessleym, May 23, 2004.

  1. wessleym

    wessleym OSNN Addict

    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.
     
  2. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    first you would need to write an XML Document Type to define all the elements, then you have to write an XML parser
     
  3. wessleym

    wessleym OSNN Addict

    Messages:
    99
    Hmm, I have no clue what that means. Could you or someone else explain further?
     
  4. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    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. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    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>
     
  6. wessleym

    wessleym OSNN Addict

    Messages:
    99
    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.
     
  7. kid23

    kid23 OSNN One Post Wonder

    Messages:
    8
    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.