I have found a source of and aledged xml file (although it is named as solarxml.php, when I download it the data that I want is there, i.e. solarflux, aindex, kindex and sunspots, but I have no idea how to determine how to extract the 4 data fields I want, also don't want to download it each time as I want to use those 4 items on my webpage, this when refreshed or reloaded would force a reload of the xml data and thus keeping the information fresh. I have looked at many tutorials and now seem to be chasing myself in circles.

The xml file can be found at: http://www.hamqsl.com/solarxml.php

I will of course acknowledge the source of the info, although it is all in the public domain.

So a snippet of code to get me started would be great, I am sure that this will be like falling off a log to some of you but to me, who has spent 2 days scratching my head seem like a big problem.

Thanks in anticipation

Member Avatar for diafol

You've tgged this with PHP, so you can use a number of functions or libraries. SimpleXML can be used or you could use DOMDocument. You can also use XPath in its various guises. If you show a snippet of the XML file and then the data you wish to extract, that would be useful.

This is an extract of the solarxml.php file which I want to read from a different website and then extract the data for solarflux, aindex, kindex and sunspots, so I can use it on my website

<?xml
version="1.0" encoding="UTF-8"?>
<solar>
<solardata>
    <sourceurl="http://www.hamqsl.com/solar.html">N0NBH</source>
    <updated>07 Mar 2017 1231 GMT</updated>
    <solarflux>72</solarflux>
    <aindex>25</aindex>
    <kindex>3</kindex>
    <kindexnt>No Report</kindexnt>
    <xray>A5.0</xray>
    <sunspots>0</sunspots>
    <heliumline>107.6</heliumline>
. etc
. etc
. etc
. etc
. etc
</solardata>
</solar>
Member Avatar for diafol

Ok looks pretty standard. Have you looked at DomDocument or SimpleXML?

Ok, I now seem to be able to get the file to load, but having tried many different things I am unable to drill down into the nesting withing the simple xml file, I guess it is working because the first item I call returns a nul whjich would be right, but can't find out hopw to drill down into the nodes.

<html>
<body>

<?php
$xml=simplexml_load_file("http://www.hamqsl.com/solarxml.php") or die("Error: Cannot create object");
echo $xml->solar . "<br>";
echo $xml->aindex . "<br>";
echo $xml->kindex . "<br>";
echo $xml->solarflux . "<br>";
echo $xml->sunspots . ",br.":
?> 

</body>
</html>

XPath /solar/solardata/solarwind|/solar/solardata/magneticfield yields:

<solarwind>509.4</solarwind>
<magneticfield>  1.9</magneticfield>

No error messages now, but doesn't return any details from the xml

<!DOCTYPE html>
<html>
<body>

<?php
$xml=simplexml_load_file("http://www.hamqsl.com/solarxml.php") or die("Error: Cannot create object");
echo XPath /solar/solardata/solarwind|/solar/solardata/magneticfield
?> 

</body>
</html>

The XPath works, your PHP looks wrong.

$xmllint --xpath "/solar/solardata/solarwind|/solar/solardata/magneticfield" solarxml.xml

<solarwind>511.8</solarwind><magneticfield> -0.7</magneticfield>

I assume you didn't read the documentation. I don't know PHP, there may be preferred libraries for this type of thing.

Thanks for the help, but I am having a brain drain and just can't seem to get the data out, might have to abort this and try something else, I hate failure but sometimes I have to throw in the towel. Cheers anyway for your help, I know it is only a few lines of code, just can't cracxk it, a bit of little knowledge.

Member Avatar for diafol

Does the following work?

$xml=simplexml_load_file("http://www.hamqsl.com/solarxml.php") or die("Error: Cannot create object");
$aindex = $xml->solardata->aindex;
$kindex = $xml->solardata->kindex;

echo "aindex: $aindex | kindex: $kindex"; 

Hi, all I get back when I run that code is:

solardata->aindex; $kindex = $xml->solardata->kindex; echo "aindex: $aindex | kindex: $kindex"; ?>

So maybe it isn't reading the solarxml.php file for some reason. I can open the page on a browser and it all checks out, even tested the solarxml.php file and it come back as written correctly. I don't know how to do it, but would there be mileage in copying it to my local website and changing the file type to .xml (but that isn't the ideal way of doing it as it increases loadings

Member Avatar for diafol

This sounds like you are not running a webserver like Apache. Turn it on. What.s your setup? XAMPP, WAMP? Something else?

That answers everything, non of the two hosts I use have PHP and scripting enabled. Any idea if you can do the same in html or java ??

Member Avatar for diafol

You cant do the ssame in html. However you can use SSI meaning server side includes. Bit of old tech but may still work. Also you could try cgi scripts
But that is not php.

Do you mean Java or JavaScript? Both is yes by the way.

I'm guessing you mean JavaScript, have a look at the jQuery library and specifically its XML functions. It also supports Ajax calls to get to that text file, though you might want to open up a new question if you hit any issues so that knowledgeable people can help you deal with them. They might not always find it if it's not tagged with JavaScript.

It's easier to get a PHP host than a Java host, but you could have a look at OpenShift. Last time I read they offered a small free option, but I'm not sure if they still do that.

In Java you would basically load the XML into an object. A Vogella tutorial is usually a good place to start when learning the basics.

Member Avatar for diafol

If using js you may hit CORS problems with accessing external files.

Member Avatar for diafol

Bit confused here. Tagged PHP, but you're not running PHP. Do you have any features with these hosted sites of yours? Are they free wix / wordpress - type things? Even the most basic webhosts usually provide PHP/MySQL.

If using js you may hit CORS problems with accessing external files.

You're right. I didn't check if it was his own xml file he wanted read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.