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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question about changing then passing XML

    I've done some programming, but never in javascript and never for the web; I'm old, I started in Pascal.

    If I have an XML file, let's say a simple one called pets.xml:

    Code:
    <pets>
         <dog breed="beagle">fido</dog>
         <dog breed="terrier">rover</dog>
    </pets>
    And I'm using existing javascript code and custom library that has the following:
    Code:
    var uri = "pets.xml"
    Is there a way to, client side, change the xml so that different xml code gets passed to the variable uri? I do not want to edit the original xml file on the server; I want pets.xml to stay the same.

    But I would like to be able to do search and replace over the entire xml file before the data gets passed to the uri variable.

    For instance, I would like to somehow load the original pets.html file, then call a function to replace "terrier" with "collie" so that the xml now reads:

    Code:
    <pets>
         <dog breed="beagle">fido</dog>
         <dog breed="collie">rover</dog>
    </pets>
    And somehow pass this -- object? -- to the uri var for the existing library to parse and handle.

    Forgive me if this is an unusual way to do what I'm envisioning, but I can see myself writing some javascript functions to do the search and replace (or use existing code if I can find it), but I really am not familiar enough with javascript to feel comfortable digging into the custom library and changing how the xml is parsed.

  • #2
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Where I wrote:

    >For instance, I would like to somehow load the original pets.html file

    I should have written

    >For instance, I would like to somehow load the original pets.xml file

  • #3
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    I'm not quite as old to have worked with Pascal, but at least I did some Delphi many years ago. Anyway, what you are looking for is an Ajax request (Asynchronous Javascript and XML), with which you can load the XML file from the server. Javascript has built-in XML support, but you can also manipulate the string directly (though that sort of defeats the purpose of XML).

    jQuery is a fairly popular library and has nice cross-browser Ajax support and will also do the XML parsing for you, if you want to, but you can also use plain old Vanilla JS to do this. Examples for both methods can be found, for example, here: http://stackoverflow.com/questions/8...without-jquery

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,294
    Thanks
    10
    Thanked 583 Times in 564 Posts
    you would have to change your xml handling routine to accept a string of xml instead of the url of an xml file. tools are around to do both, but urls work a bit better than strings for some cases because relative linking and encoding context is resolved better with urls. this comes into play a lot with XSLT. you might want to look into xsl because it might be simpler than javascript/jquery to make the transforms you want, and xslt can run in lots of places js cannot. it can also run right in the browser, and it's made for doing just what you want, whereas JS needs a lot of help and patience to process xml reliably.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Pascal was my second language; the first was BASIC. With line numbers and everything.

    Okay, I think I'm getting there. If I add this code:

    Code:
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.open("GET","data1.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;
    Then later on in the existing code, there is the statement

    Code:
    var uri = "data2.xml"
    How do I change that last statement to refer to the object loaded from data1.xml (which I will edit in the code) instead of referring directly to data2.xml? I don't know if I'm explaining this clearly...


  •  

    Posting Permissions

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