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.
Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Help parsing an XML file from the web

    Hello,

    I am trying to get the values from the xml file seen at: http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

    I am just trying to get one value before I write code for all of them using the script below. The script in the head is from w3schools.com and is trying to parse the xml file but I am getting no luck with anything. I have been all over the web for help with this xml source but have found nothing to work from.

    Im pretty sure I could work with this xml if I could just get it working and returning one value first.

    Many thanks


    Code:
    <html>
    
    <head>
    <script language="javascript" type="text/javascript">
    
    
        function loadXMLDoc(dname) {
            var xmlDoc;
            if (window.XMLHttpRequest) {
                xmlDoc = new window.XMLHttpRequest();
                xmlDoc.open("GET", dname, false);
                xmlDoc.send("");
                return xmlDoc.responseXML;
            }
            // IE 5 and IE 6
            else if (ActiveXObject("Microsoft.XMLDOM")) {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = false;
                xmlDoc.load(dname);
                return xmlDoc;
            }
            alert("Error loading document");
            return null;
        }
    
    </script>
    
    </head>
    
    <body>
    
    
    <script type="text/javascript">
    
    xmlDoc = loadXMLDoc("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
    
    
        var a = xmlDoc.getElementsByTagName("Cube")[0];
    
        var currency = a.getElementsByTagName("Cube")[1].getAttribute("currency");
        
    alert(currency);
        
    
        
    </script>
    
    </body>
    </html>

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    I do not think that you can call an external file using ajax it is prevented by scripting.

  • #3
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    are there any other ways I can parse and use this file then please?

  • #4
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    I think that the best way is that your server sided code opens a url connection to the server where the xml file is stored and retrieves it from there.


    I do not know what server sided code you are using but I can help you if you use java.

  • #5
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    well i'm trying to use the xml values to make a currency type calculator on the client side using javascript and the DOM. I dont think server side would be suitable for this. I am using PHP for any server side scripting that I do.

  • #6
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    In fact if you open a URL connection using PHP you can retrieve the string value of the xml and then you can print it out using the echo command in php .
    There is an example here on how to open a url connection to a certain url and read its content in php:
    http://www.koders.com/php/fidBBC4BAA...1.aspx?s=login

    Name the file for example (xmlretrieve.php) and make your ajax call to the file xmlretrieve.php instead of
    http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

  • #7
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hello,

    I see how that could work but im not sure how it will work in practice with the site im creating.

    When I get home from work I will upload what I have to a webserver and give you the link, maybe you can see better what I am trying to achieve.

    Thanks!

  • #8
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hello,

    This is my site cut down to the basics. http://www.rtwpricetag.com/tester.php

    It is a calculator using javascript. The totaling is not actually working on this live version but I hope this shows the problem a bit better.

    From the XML file located at the link I posted yesterday I need to extract the currency rates and store them in javascript variables so they can be applied when the convert drop down box is changed.

    Maybe the scripts you suggest below are capable of achieving this but I have very little knowledge of actually writing AJAX.

  • #9
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    First of all you have to write this class and put it somewhere in your code(I do not know php I just got it from koders):
    PHP Code:
    <?php
    // CURL or fopen of URLs
    class URLConnection {

        
    /**
         * The url
         */
        
    protected $url;
        
        
    /**
         * The cache contents of the url
         */        
        
    protected $contents;
        
        
    /**
         * Default constructor
         */
        
    public function __construct($url) {
            
    $this->url $url;
        }

        
    /**
        * 'Getters' to return contents and url
        */
        
    public function getContents(){
            
    $this->readAndCacheContents();
            return 
    $this->contents;
        }
        public function 
    getUrl(){
            return 
    $this->url;
        }
        
        
    /**
        * Reads and cache the contents of the url
        */
        
    public function readAndCacheContents() {
            if ( !empty(
    $this->contents) ) {
                return;
            }
            
            if ( 
    function_exists('curl_init') ) {
                
    $ch curl_init();        
                
    curl_setopt($chCURLOPT_URL$this->url);
                
    curl_setopt($chCURLOPT_HTTPHEADER, array('Accept-Charset: UTF-8;'));        // lets hope for a UTF-8 version
                
    curl_setopt($chCURLOPT_CONNECTTIMEOUT3);
                
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
                
    curl_setopt($chCURLOPT_HEADER0);
                
    curl_setopt($chCURLOPT_USERAGENT'Cinnamon Statistics Engine/'Cinnamon::getVersion());
                
    curl_setopt($chCURLOPT_FOLLOWLOCATION1);
                
    curl_setopt($chCURLOPT_MAXREDIRS2);
                
    $this->contents curl_exec($ch);
                
    curl_close($ch);
            } else {
                
    $fh fopen($this->url'rb');
                if ( 
    $fh ) {
                    
    $this->contents stream_get_contents($fh);
                    
    fclose($fh);        
                }
            }
        
        }
    }
    ?>
    This class should be placed somewhere and give it a name URLConnection.php
    This will be a common class that you will use whenever you need to get data from an external system.

    After that you should create another php file named Currencies.php that uses the URLConnection.php and it should contain something like that:

    PHP Code:
    <?php
    header
    ("Content-Type: text/xml");
    include 
    'URLConnection.php';

    $obj = new URLConnection('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml');

    echo 
    $obj.getContents();
    ?>
    Once done you use your method to call the ajax but for local file not external file as follows:
    Code:
    <html>
    
    <head>
    <script language="javascript" type="text/javascript">
    
    
        function loadXMLDoc(dname) {
            var xmlDoc;
            if (window.XMLHttpRequest) {
                xmlDoc = new window.XMLHttpRequest();
                xmlDoc.open("GET", dname, false);
                xmlDoc.send("");
                return xmlDoc.responseXML;
            }
            // IE 5 and IE 6
            else if (ActiveXObject("Microsoft.XMLDOM")) {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = false;
                xmlDoc.load(dname);
                return xmlDoc;
            }
            alert("Error loading document");
            return null;
        }
    
    </script>
    
    </head>
    
    <body>
    
    
    <script type="text/javascript">
    
    xmlDoc = loadXMLDoc("make sure to put here the right path to the file...Currencies.php");
    
    
        var a = xmlDoc.getElementsByTagName("Cube")[0];
    
        var currency = a.getElementsByTagName("Cube")[1].getAttribute("currency");
        
    alert(currency);
        
    
        
    </script>
    
    </body>
    </html>
    As I told you I do not know PHP but this what I have done in Java a similar example and I tried to translate it to PHP.

    Give it a try

  • #10
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    hello,

    I now have 3 files:

    1) URLConnection.php - containing the class URLConnection

    2) Currencies,php - contains the following piece of code. Note the inclusion of the class in URLConnection.php

    PHP Code:
    <?php
    header
    ("Content-Type: text/xml");
    include 
    'URLConnection.php';

    $obj = new URLConnection('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml');

    echo 
    $obj.getContents();
    ?>
    3) Test.php - the AJAX call. The code I have is the same but note the xmldoc I am loading, I have inserted Currencies.php which I can only assume is correct. Anyway, I get no popup as the code invites and the screen is blank. I don't no if the error is with the AJAX call or with the DOM coding? What do you think?

    Code:
    <html>
    
    <head>
    <script language="javascript" type="text/javascript">
    
    
        function loadXMLDoc(dname) {
            var xmlDoc;
            if (window.XMLHttpRequest) {
                xmlDoc = new window.XMLHttpRequest();
                xmlDoc.open("GET", dname, false);
                xmlDoc.send("");
                return xmlDoc.responseXML;
            }
            // IE 5 and IE 6
            else if (ActiveXObject("Microsoft.XMLDOM")) {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = false;
                xmlDoc.load(dname);
                return xmlDoc;
            }
            alert("Error loading document");
            return null;
        }
    
    </script>
    
    </head>
    
    <body>
    
    
    <script type="text/javascript">
    
    xmlDoc = loadXMLDoc("Currencies.php");
    
    
        var a = xmlDoc.getElementsByTagName("Cube")[0];
    
        var currency = a.getElementsByTagName("Cube")[1].getAttribute("currency");
        
    alert(currency);
        
    
        
    </script>
    
    </body>
    </html>

  • #11
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    As I told you I do not know PHP and I do not know if the syntax in the file currencies.php is correct.

    Second you need to make sure that the include command in the file currencies.php points to a correct path.

    once you verified both points above I want you to put these debugging messages in the test file:
    after this:
    Code:
    xmlDoc = loadXMLDoc("Currencies.php");
    put this:
    Code:
    alert(xmlDoc .xml);
    alert(xmlDoc .parserError.reason);
    alert(xmlDoc .parserError.srcText);

  • Users who have thanked ckeyrouz for this post:

    alex57 (08-13-2009)

  • #12
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    hello,

    Ok. I think the syntax in Currencies.php is correct:

    PHP Code:

    <?php
    //header("Content-Type: text/xml");
    include 'URLConnection.php';

    $obj = new URLConnection('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml');
    echo 
    $obj;
    echo 
    "<br>";
    echo 
    $obj->getContents();
    echo 
    "<br>";
    echo 
    $obj->getURL();
    echo 
    "<br>";
    echo 
    $obj->readAndCacheContents();

    ?>
    if I load this page I get the following printed to the browser:

    Object id #1
    Reference rates European Central Bank
    http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

    this is a good sign because the second two lines can be seen in the file at http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml. However the readAndCacheContents() doesnt seem to return anything.

    The include command path in Currencies.php is pointing to the correct place; all files are in the same folder.

    I put the alerts in the test file but no alert messages appeared. Im not sure the call to Currencies.php is correct with:

    Code:
    xmlDoc = loadXMLDoc("Currencies.php");
    what do you think? Do you know if that is a valid call.

    thanks

  • #13
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    try changing this:
    Code:
    xmlDoc.open("GET", dname, false);
    to this:
    Code:
    xmlDoc.open("POST", dname, false);

  • #14
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I made the change but I still get no alert boxes to give info on any errors.

    Thanks for all your help. Much appreciated. Any other methods of using this xml source or any further ideas on this method?

  • #15
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts

    Netscape

    can you check if the xml is well formed or not, so I need you to remove all the alert message and replace your js function with this one:
    Code:
    function loadXMLDoc(dname) {
            var xmlDoc;
            if (window.XMLHttpRequest) {
                xmlDoc = new window.XMLHttpRequest();
                xmlDoc.open("GET", dname, false);
                xmlDoc.send("");
               alert(xmlDoc.responseText);
                return xmlDoc.responseXML;
            }
            // IE 5 and IE 6
            else if (ActiveXObject("Microsoft.XMLDOM")) {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = false;
                xmlDoc.load(dname);
               alert(xmlDoc.responseText);
                return xmlDoc;
            }
            alert("Error loading document");
            return null;
        }
    By the way what browser you are using?


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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