Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2003
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Accessing the XML file through JavaScript instead of an HTML script

    Hi, I have a script that me and a friend were trying to figure out, to access a .xml file in order to insert information into the HTML file. The following HTML script is what has been being used to access the XML file, however, there are problems when trying to do certain things using this method (In order to save anyone much confusion, I'll only describe exactly what the problems are if it's absolutely necessary, but I think the following information is good enough to describe what we need):

    <span id="showPRICE" DATASRC="#albums" DATAFLD="ARTISTNAME"></span>

    My friend said that it could be done if it could access the XML file through an equivalent JavaScript rather than HTML script.

    There was another post on this forum that I think wants the same thing at:
    XML, Javascript and HTML question

    And on that page, it links to:
    http://www.patrick-brennan.com/javas...ort_table.html

    But the script on that page is very very confusing. How do I access an individual field in the XML file rather than a whole group? Like I said, we want it to be directly equivalent to this HTML script, but using JavaScript instead:

    <span id="showPRICE" DATASRC="#albums" DATAFLD="ARTISTNAME"></span>
    Last edited by Erica_qt; 03-26-2003 at 05:34 AM.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Loading an xml file can be done through several different mechanisms, the one you'll be interested in is likely loading an xml document object into a reference variable.

    Code:
    var
      src=[string XMLFile],
      doc,
      i;
    
    function handleXml(doc){
    /*\
     |  Handle xml document here. 
     |  You can use DOM 1 Core and most of DOM 2 Core.
     |  Note that you will need to check whether you
     |  should use namespaced or non-namespaced functions
     |  if you want to edit the imported xml document.
    \*/
    }
    
    switch((i=(document.implementation&&document.implementation.createDocument)||top.ActiveXObject||null)){
        case ActiveXObject:
            doc=new i('Microsoft.XMLDOM');
            doc.async=false;
            handleXml(doc);
            break;
        case document.implementation.createDocument:
            doc=new i('','',null);
            doc.load(src);
            doc.onload=function(){handleXml(this)};
            break;
        default:
            alert("User agent lacks xml importation mechanism");
    }
    (This code is just written and untested, so it might not work without bugfixing. Especially I wonder whether user agents that doesn't support document.implementation will let it pass or throw an error. Is nn4/ie4/op<7/minor browsers interesting for you?)
    Last edited by liorean; 03-26-2003 at 01:53 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Mar 2003
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I couldn't understand any of that, I'm not too great at programming, so I didn't even know where to start with that...

    However, I was browsing through search engines trying to find an answer, and I found EXACTLY what I needed... But I don't know how to make it work slightly different. They only have one group in their XML file... I need many many groups, and to be able to access them individually... Does anyone know what I mean?

    This information is in "note.xml"
    Code:
    <?xml version="1.0" encoding="ISO8859-1" ?>
    <note>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>Don't forget me this weekend!</body>
    </note>
    and this is in "note.html"
    Code:
    <html>
    <head>
    
    <script language="JavaScript"
    for="window" event="onload">
    
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
    xmlDoc.async="false"
    xmlDoc.load("note.xml")
    nodes = xmlDoc.documentElement.childNodes
    to.innerText = nodes.item(0).text
    from.innerText = nodes.item(1).text
    header.innerText = nodes.item(2).text
    body.innerText = nodes.item(3).text
    
    </script>
    
    <title>HTML using XML data</title>
    </head>
    
    <body bgcolor="yellow">
    <h1>Refsnes Data Internal Note</h1>
    
    <b>To: </b><span id="to"></span>
    
    <br>
    <b>From: </b><span id="from"></span>
    
    <hr>
    <b><span id="header"></span></b>
    
    <hr>
    <span id="body"></span>
    
    </body>
    </html>

  • #4
    New Coder
    Join Date
    Mar 2003
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Does anyone know how to do this? Is it too hard, is that why there is no response yet? Or is it just not possible?

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Oh, there's plenty of people here who know how to do it. The reason there's no response before now is not because it's too hard. It's not 'not possible'.

    It's just that some things take time to code for. You're asking for something specific enough to require a final functionality but generic enough that we can't be sure what you want. And we're lazy. (And this isn't the most frequented forum, as you can see by the number of threads in it.) Give it a shot, explain the structure of the xml you want to dissect and what you want to read out from it. Explain what you want us to do with the information extracted from the file.

    (Oh, just a sidenote, do you know anything about the DOM and how it works? That would make things easier for us.)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    New Coder
    Join Date
    Mar 2003
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, I didn't mean to sound impatient. I would wait up to a week without comenting on the fact that there were no replies. Except that I've posted on this forum before (but lost my account and password, so I made a new one) a while back and usually got VERY quick responses, sometimes within minutes. And so that's why I thought it a little unusual that there were no replies yet.

    Actually, my friend convinced me to describe the specific problem we were having, but since it was significantly different from this (it is related to the script in this post, but this post isn't necessarily related to it) that I just put it in a new post. But it hasn't gotten any replies. The URL for that post is:

    I can't make it correctly insert the information from the XML fields...

    Anyway, the reason we need to be able to access the XML file with javascript is because he figured that he could get the script to work if he made variables equal whatever was in the specific XML fields, THEN insert it into the script.

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Erica if you tell me a few things, I could write a quite simple script doing what you need:

    0. What input do you have for telling the script how to handle the xml (or in this case, determining what entry to handle)? Hard coded, cookies, user agent, prompt, links, forms, serverside determination etc.
    1. What's the structure of each individual entry in the xml file? (I gather you wanted to get a single entry out of many. Am I right?)
    2. When you have read the entry into the javascript, what processing (modification, calculation, etc.) do you want to do?
    3. How do you want to output the result?

    Try to be as concise as possible in explaining.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    New to the CF scene
    Join Date
    Feb 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I understand ..

    I understand what you want because I would like to do the same ..

    Taking an exemple :

    <player>
    <name>toto</name>
    <age>21</age>
    <sex>man</sex>
    ...
    </player>

    We see in the exemple given before a javascript code that access to the i-th child of the <player> node..

    But if you change the XML file your HTML page will be destroyed then!
    What you want is to know how we can directly access the child "name" or "age" .. like that, even if someone add a child to the player node.. your HTML page will still correct!

    .. I try to find some exemples, but for the moment, I have nothing!

    Frederic.

  • #9
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Uh, there are several ways to reference nodes... here are a couple:

    Code:
    xmlDoc.selectSingleNode("//name").text;
    
    xmlDoc.getElementsByTagName("age").item(0).text;
    Just between you & me, the MSXML reference contains many examples.
    *this message will self destruct in n-seconds*

  • #10
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts
    You might find this thread useful as I have just been going through a similar process

    Same question in Javascript forums


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •