PHP If condition with XML - php

I'm trying to build a PHP if expression.
In English, here's the conditions for the IF to be true:
Visbility < 5000m
OR
BKN/OVC < 1500ft
Now, the variables I have to play with are:
for Visibility: $visibility and for the BKN/OVC: it's a bit more complex.
This data is given in
$xml->data->METAR[0]->sky_condition
The data itself is pulled from XML file with the relevant information looking like this:
<sky_condition sky_cover="BKN" cloud_base_ft_agl="600"/>
<sky_condition sky_cover="SCT" cloud_base_ft_agl="1100"/>
Any ideas on how I could sort through the sky_condition->sky_cover for the relevant information and feed it through to the IF?
Thanks.

You could access the attributes of the XML with the array syntax instead of the arrow operator.
if((string)$xml->data->METAR[0]->sky_condition[0]['sky_cover']==='???') {
}
Was this your question?

Related

PHP xpath check for div style and its value

I am trying to parse a html file/strings for two things using php and xpath.
<DIV STYLE="top:110px; left:1280px; width:88px" Class="S0">Aug30</DIV>
I tried to look for an unknown value (here: Aug30) with knowing the style top and left value (here: 110px and 1280px).
And the other way. I know the value Aug30 but want to get its values of top and left.
Perhaps XPATH is not the best way to do this. Any idea on how to solve my problem?
Thanks in advance for your help!
To filter <div> element by style attribute value in XPath you can do something like this :
//div[contains(#style, 'top:110px') and contains(#style, 'left:1280px')]
Above XPath will search for <div> node having style attribute value contains two specific strings.
The other requirement isn't supported in XPath 1.0 as far as I can see. We can get the entire value of style attribute, but getting part of it is a dead end. There are some string functions we can use, even though returning a function's result isn't supported.
You'll need to do that using XPath 2.0 or using the host programming language (PHP in this case).

Accessing XML attributes data

I have two lines of XML data that are attributes but also contain data inside then and they are repeating fields. They are being stored in a SimpleXML variable.
<inputField Type="Name">John Doe</inputField>
<inputField Type="DateOfHire">Tomorrow</inputField>
(Clearly this isnt real data but the syntax is actually in my data and I'm just using string data in them)
Everything that I've seen says to access the data like this, ,which I have tried and it worked perfectly. But my data is dynamic so the data isn't always going to be in the same place, so it doesn't fit my needs.
$xmlFile->inputField[0];
$xmlFile->inputField[1];
This works fine until one of the lines is missing, and I can have anywhere from 0 to 5 lines. So what I was wondering was is there any way that I can access the data by attribute name? So potentially like this.
$xmlFile->inputField['Name'];
or
$xmlFile->inputField->Name;
I use these as examples strictly to illustrate what I'm trying to do, I am aware that neither of the above lines of code are syntactically correct.
Just a note this information is being generated externally so I cannot change the format.
If anyone needs clarification feel free to let me know and would be happy to elaborate.
Maybe like this?
echo $xmlFile->inputField->attributest()->Name;
And what you're using? DOMDocument or simplexml?
You don't say, but I assume you're using SimpleXMLElement?
If you want to access every item, just iterate:
foreach ($xmlFile->inputField as $inputField) { ... }
If you want to access an attribute use array notation:
$inputField['Type']
If you want to access only one specific element, use xpath:
$xmlFile->xpath('inputField[#Type="Name"]');
Perhaps you should read through the basic examples of usage in the SimpleXMLElement documentation?
For example you can a grab a data:
$xmlFile = simplexml_load_file($file);
foreach($xmlFile->inputField as $res) {
echo $res["Name"];
}

What is the fastest way to convert html table to php array?

are there build in functions in latest versions of php specially designed to aid in this task ?
Use a DOM parser like SimpleXML to split the HTML code into nodes, and walk through the nodes to build the array.
For broken/invalid HTML, SimpleHTMLDOM is more lenient (but it's not built in).
String replace and explode would work if the HTML code is clean and always the same, as soon as you have new attributes it will brake.
So only dependable solution would be using regular expressions or XML/HTML parser.
Check http://php.net/manual/en/book.dom.php
An alternative to using a native DOM parser could be using YQL. This way you dont have to do the actual parsing yourself. The YQL Web Service enables applications to query, filter, and combine data from different sources across the Internet.
For instance, to grab the HTML table with the class example given at
http://www.w3schools.com/html/html_tables.asp
you can do
$yql = 'http://tinyurl.com/yql-table-grab';
$yql = json_decode(file_get_contents($yql));
print_r( $yql->query->results );
I've deliberated shortened the URL so it does not mess up the answer. $yql actually links to the YQL API, adds some options and contains the query:
select * from html
where xpath="//table[#class='example']"
and url="http://www.w3schools.com/html/html_tables.asp"
YQL can return JSON and XML. I've made it return JSON and decoded this then, which then results in a nested structure of stdClass objects and Arrays (so it's not all arrays). You have to see if that fits your needs.
You try out the interactive YQL console to see how it works.
i dont know if this is the faster , but you can check this class (using preg_replace)
http://wonshik.com/snippet/Convert-HTML-Table-into-a-PHP-Array
If you want to convert the html-description of a table, here's how I would do it:
remove all closing tags (</...>) ( http://php.net/manual/de/function.str-replace.php)
split string at opening tags (<...>) using a regular expression ( http://php.net/manual/en/function.split.php)
You have to work out the details on your own, since I do not know if you want to handle different lines as subarrays or you want to merge all lines into one big array or something else.
you could use the explode-function to turn the table cols and rows into arrays.
see: php explode

php code to return xml

hi there i have a question and need help on this from you guys .
I have a database created for a game called gamesleaderboard and the fields are id, player_name, score, leveltime. and my task is after getting the score, i have to insert it to a database and sort the dbase accordingly.
after sorting, the code will return an xml in the following structure:
Ahmad100080
Basel95090
Samer920100
Seyd900110
Ahmad100080
Basel95090
Samer920100
Seyd900110
Ahmad100080
Basel95090
plz tell me the necessary details how to do this thankyou.
In most simplistic terms there is nothing really special to do here, you can output XML in exactly the same way you would output HTML in PHP, this is a simple example
You can also use the DOMDocument class (or SimpleXML) to output XML, this is a bit more complex but is better practice. For an example of creating XML with DOMDocument using data from MySQL read more here
Don't forget to send the correct Content-Type header before outputting the XML document tho.
If you are very new to XML I would highly recommend SimpleXML as it will be enough for most of needs. Creating XML using "echo" and strings is not only a dangeous but also a very bad programming technique.
Using SimpleXML you can easily add new nodes, adding child nodes and attributes to them. If you can getting started reading the PHP docs, just search for a SimpleXML tutorial on google. Or ask your questions right here.
$query=mysql_query("Select * from gamesleaderboard ");
$number=mysql_num_rows($query);
if ($query==0)
{
echo "0 rows Affected";
}
$doc= new DOMDocument();
$doc->formatOutput=true;
$root= $doc->createElement("Games");
$doc->appendChild($root);
for ($i=0; $i<$number; $i++){
$row=mysql_fetch_array($qex);
$node=$doc->createElement("user");
$pn=$doc->createElement("player_name");
$pn->appendChild($doc->createTextNode($row["player_name"]));
$node->appendChild($pn);
$sc=$doc->createElement("score");
$sc->appendChild($doc->createTextNode($row["score"]));
$node->appendChild($sc);
$root->appendChild($node);
}
echo $doc->saveXML();
This will display the answer exactly you want. I just tested it. I was probably in highschool when you asked this question here. Anyway it'll help someone else.

Get a single element with PHP and XPath

Lots of tutorials around the net but none of them can explain me this:
How do I select a single element (in a table, for example), having its absolute XPath?
Example:
I have this:
/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr/td/table[3]/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr[4]/td[5]/span
What's that PHP function to get the text of that element?!
Really I could not find an answer. Found lots of guides and hints to get all the elements of the table, all the buttons of a form, etc, but not what I need.
Thank you.
$xml = simplexml_load_string($html_content_string);
$arr = $xml->xpath("//body/table/tbody/tr[2]/td[2]/table/tbody/tr/td/table[3]/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr[4]/td[5]/span");
var_dump($arr);
Load you HTML document into a DOM object then make a DOMXPath object from it and let it evaluate your query string.
It's all described in detail here: http://php.net/manual/en/book.dom.php

Categories