Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Parse XML from Url

Options
  • 28-02-2010 11:44pm
    #1
    Registered Users Posts: 3,184 ✭✭✭


    Hi All,
    I found an XML web page(which updates every few minutes) on the internet and i want to fetch the data on that XML page to my own website(localhost)

    Does any one have any html or php code that will take the xml data from the url and display it on my webpage

    i've tried a few methods but i'm getting nowhere

    Thanks


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Why don't you get the XML and parse it? - How do you want it displayed?


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    The forum charter would be a good place to start, read the section regarding asking questions.


  • Registered Users Posts: 3,184 ✭✭✭Kenno90


    Sorry Guys, will try this again:rolleyes:

    OK the problem is i found an XML page on the internet that updates it's self every few minutes.
    I want to add the whole xml in to my own webpage.

    i've tried the following codes
    (1) 
    <?php 
    $xml = file_get_contents('http://www.[COLOR=Red]Websitehere[/COLOR].xml'); 
    echo"$xml";
    ?>
    
    and
    
    (2)
    <?php 
    $fetch_url = "http://www.[COLOR=Red]Website here[/COLOR].xml"
    $xml = simplexml_load_file($fetch_url) or die("error"); 
    echo"$xml";
    ?>
    
    When i use either of these codes i get no errors just a blank screen that does nothing.

    Have any of you guys experience using xml across domains and help me out,thanks.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Not sure if I know exactly what you want. You just want the XML outputted the same as you have there?

    You may need to set the http headers then to specify that it's XML content

    Try this:

    [php]
    <?php
    header ("Content-Type:text/xml");
    $xml = file_get_contents('http://www.Websitehere.xml');
    echo"$xml";
    <?php>
    [/php]

    (I haven't tested this)


  • Registered Users Posts: 3,184 ✭✭✭Kenno90


    Webmonkey wrote: »
    Not sure if I know exactly what you want. You just want the XML outputted the same as you have there?

    You may need to set the http headers then to specify that it's XML content

    Try this:

    [php]
    <?php
    header ("Content-Type:text/xml");
    $xml = file_get_contents('http://www.Websitehere.xml');
    echo"$xml";
    <?php>
    [/php](I haven't tested this)

    Ye all i want to do is output the xml to my webpage and then once i get that working i will see if i want to display certain parts of the xml.
    Thanks for your help will try your code ;)


  • Advertisement
  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Your original code is probably outputting it alright but browser won't render it. If you go view source, it probably there.


  • Registered Users Posts: 3,184 ✭✭✭Kenno90


    nah not in the source code on the browser, will keep trying though:rolleyes:


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    I know you want to use PHP but this way will work too (some parsing done so please feel free to change as required). Just add a timeout to reference the displayInfo function every interval to keep updating the page

    HTML file
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Book</title>
        <script type="text/javascript" language="javascript" src="book3.js"></script>
        <link rel="stylesheet" type="text/css" href="book.css"/> 
    </head>
    <body onload="javascript:displayInfo('book.xml')">
        <div id='details'></div>
    </body>
    </html>
    

    Javascript file
    var xmlhttp;
    
    function displayInfo(filename) {
    	xmlhttp = GetXmlHttpObject(); // create an object to handle the XML Http Request
    	if(xmlhttp == null) { // If the browser can't handle XML Http requests
    		alert ("Your browser does not support AJAX!");
    		return;
    	}
    
    	xmlhttp.onreadystatechange = processReqChange;	// The command that will handle changes in the state of the XMLHttp request object
    	xmlhttp.open("GET", filename , true);			// The XML file we are going to interrogate
    	xmlhttp.send(null);								// Start the data stream
    }
    
    function processChange() {
    	var xmlDoc = null;								// Create an object to handle the XML object coming back from the file
    	if(xmlhttp.readyState == 4) {					// Only do something if the readystate is 4
    		xmlDoc = xmlhttp.responseXML;				// Store the returned XML object in our local object
    	}
    	if(xmlDoc != null) {							// Make sure something came back
    		var theString;								// To hold the generated HTML
    
    	 	for (i = 0; i < xmlDoc.documentElement.childNodes.length; i++)	// Go through each of the main nodes of the parent node
    		{
    			// If element has no child node, ie. <isbn>
    			if (xmlDoc.documentElement.childNodes[i].childNodes.length == 1) 
    			{
    				var bookXMLDetails = "<span style=\"background-color:green;\">" + xmlDoc.documentElement.childNodes[i].firstChild.nodeValue + "<span>";		  // Retrieve the data of the node and put it in a green span
    				document.getElementById('details').innerHTML += bookXMLDetails + "<br />";	// Display to the screen
    			}
    			// If the element has child nodes
    			if (xmlDoc.documentElement.childNodes[i].childNodes.length > 1) 
    			{
    				currentNode = xmlDoc.documentElement.childNodes[i];	// Store the index of the node
    				var size = currentNode.childNodes.length;			// Find out how many children it has
    				for (j = 0; j < size; j++)							// Loop through each child
    				{
    					if(currentNode.childNodes[j].nodeType == 1) {	// If the node type is non text
    						subNode = currentNode.childNodes[j];		// Then there are subnodes
    						var subSize = subNode.childNodes.length;	// Find out how many
    						for(k=0;k<subSize;k++) {					// Loop through each one
    							// Put it in a red span
    							var bookXMLDetails = "<span style=\"background-color:red;\">" + subNode.childNodes[k].nodeValue + "</span>";
    							// Display to the screen
    							document.getElementById('details').innerHTML += bookXMLDetails + "<br />";
    						}											// End Loop
    					}	// End If
    				}	End Loop
    			}	// End If
    		}	// End Loop
    	} // End If
    } // End Function
    
    function GetXmlHttpObject() {
    	if(window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
    		return new XMLHttpRequest();
    	}
    	if(window.ActiveXObject) {  // code for IE6, IE5
    		return new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	return null;
    }
    

    The XML file (replace reference to this file with the URL for the page you're interested in)
    <?xml version="1.0" encoding="utf-8" ?>
    <book edition="3" year="2004">
      <isbn version ="10">0596007647</isbn>
      <isbn version ="13">0596007648</isbn>
      <title>XML in a Nutshell</title>
      <subtitle>A Desktop Quick Reference</subtitle>
      <tags>
        <tag>reference</tag>
        <tag>xml</tag>
      </tags>
      <author id="eh120">
        <name>Damien Gannon</name>
      </author>
      <author id="eh125">
        <name>Gretta Davern</name>
      </author>
      <publisher>Rielly Media, Inc.</publisher>
    </book>
    

    Hope that helps,
    Regards,
    RD


  • Registered Users Posts: 3,184 ✭✭✭Kenno90


    Thanks Ron,

    Your code helped,:D


Advertisement