Jump to content

Extract Data from an XML File


jd307

Recommended Posts

This is my first time to PHPFreaks so I apologise if I am posting in the wrong place.  I have searched through this site, Google and some other coding websites to find the answer, or at least some kind of idea of how to do this.  Please bear in mind that even though I have been learning PHP for about 6 months, I'm not very good at it as I don't get much time to learn it!  :)

 

I know that similar questions have been asked, but from what I have read (even from php.net) I still do not understand how to do what I need.  Any information on what kind of syntax or even code snippets would be appreciated, though anything that can point me in the right direction would be very appreciated.

 

Here is what I am trying to do:

 

I have an XML file that is hosted on a different webserver to mine.  Basically, the file updates every now and again to give the status of a server as to whether it is up or down.  The file provides information for about 200 servers or so, but I am only interested in one.

 

The content of the XML file looks like this:

  <r id="EU6-REALM07" n="Tyrande" t="PVE" r="EU" l="Low" s="Up" q="No" /> 
  <r id="EU6-REALM09" n="Minahonda" t="RP" r="EU" l="Medium" s="Up" q="No" /> 
  <r id="EU6-REALM10" n="Los Errantes" t="PVE" r="EU" l="Low" s="Up" q="No" /> 
  <r id="EU6-REALM12" n="Darkspear" t="PVE" r="EU" l="High" s="Up" q="No" /> 

 

Now, I am interested in the last line in the code block above (though is not the last line in the XML file) which states 'n="Darkspear"'.  If you also look across this line you can see it also states 's="Up"'.

What I am trying to do is for every page load, the script does a query on the XML file, finds the line with the word 'Darkpear' in it and then check for whether 's="Up"' or 's="Down"' (obviously, if s="Up" the server is up and if s="Down" the server is down).

 

I then want to tell the script to display a different icon depending on whether the server is up or down, but this I can do fairly easily. 

 

Ideally I would like to be able to extract the data into some kind of string or variable so I can use it a bit like:

 

$sstatus = ....;

if ( $sstatus == "Up" ) {
echo "Server is up";
} else {
             echo "Server is down";
}

 

Any help is very appreciated.  If anyone knows of an answer to this already, I'd appreciate if someone could tell me where I can find it.

 

Thank you!

 

Dan

Link to comment
https://forums.phpfreaks.com/topic/64144-extract-data-from-an-xml-file/
Share on other sites

not tested it but something along these lines should work...

 

<?php

$aResult = array();
// define an element handler function - this gets called every time an xml tag is found
function startElement($rParser, $sTagname, $aAttrs) {
global $aResult;
        // check the attributes
if($aAttrs['n'] == "Darkspear") {
                // store it
	$aResult = $aAttrs;
}
}
// only here to satisfy the xml_set_element_handler function
function endElement($rParser, $sTagname) {
  // do nothing
}

$xml = file_get_contents("http://feeds.co.uk/feed.xml");
// check we've got the xml successfully
if(!$xml) die("no xml");
// create the parser object
$parser = xml_parser_create();
// define our callback functions
xml_set_element_handler ($parser, "startElement", "endElement" );
// do the parsing
xml_parse($parser, $xml);
// free up the parser
xml_parser_free($parser);

if ( $aResult['s'] == "Up" ) {
echo "Server is up";
} else {
echo "Server is down";
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.