I have never used XML before and am trying to loop through the XML and display all the display names in the 'A Team'. The code I am using is outputting 10 zeros and not the names.
The code I am using is attached below along with the feed.
Any assistance is much appreciated!
feed: https://apn.apcentiaservices.co.uk/ContactXML/agentfeed?organisation=se724de89ca150f
<?php
$url = 'https://apn.apcentiaservices.co.uk/ContactXML/agentfeed?organisation=se724de89ca150f';
$html = "";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10; $i++){
$title = $xml->organisation['APN']->brand['AllStar Psychics']->pool['A Team']->agent[$i]->display-name;
echo $title;
}
echo $html;
?>
This might getthe basics you asked for. Not sure if it's what you want. I'm not that good at xpath.
$mydata = $xml->xpath('/organisation/brand/pool[#name="A Team"]//display-name');
foreach($mydata as $key=>$value){
echo('Name:' . $value .'<br>');
}
Related
Im trying to create a table of Jobs on my site, pulling info from an xml feed I have access to... I've looked at various examples online and videos but I can't seem to understand how it works. My xml feed returns the following node structure:
<OutputVacancyAsXml>
<Vacancy>
<VacancyID></VacancyID>
<Job></Job>
<ClosingDate></ClosingDate>
</Vacancy>
</OutputVacancyAsXml>
I've had success with pulling through one item with this code:
<?php
$x = simplexml_load_file('https://www.octopus-hr.co.uk/recruit/OutputVacancyAsXml.aspx?CompanyID=400-73A3BCA1-D952-4BA6-AADB-D8BF3B495DF6');
echo $x->Vacancy[5]->Job;
?>
But converting it to foreach seems to be where I'm struggling. Heres the code I have tried so far with no luck;
<?php
$html = "";
$url = "https://www.octopus-hr.co.uk/recruit/OutputVacancyAsXml.aspx?CompanyID=400-73A3BCA1-D952-4BA6-AADB-D8BF3B495DF6";
$xml = simplexml_load_file($url);
for ($i = 0; $i < 10; $i++) {
$title = $xml->OutputVacancyAsXml->Vacancy[$i]->job;
$html .= "<p>$title</p>";
}
echo $html;
?>
Thanks all :)
Taken from the documentation
Note:
Properties ($movies->movie in previous example) are not arrays. They
are iterable and accessible objects.
With that kept in mind you can simple run over the nodes with foreach
$xml = simplexml_load_file($url);
foreach ($xml->OutputVacancyAsXml->Vacancy as $vacanacy)
{
echo (string)$vacanacy->Job; // Echo out the Job Title
}
Ok looks like I found a solution. Heres the code that worked for me plus it contains a little bit of code that pulls out duplicated (it was displaying each item 4 times!)...
<?php
$x = simplexml_load_file('https://www.octopus-hr.co.uk/recruit/OutputVacancyAsXml.aspx?CompanyID=400-73A3BCA1-D952-4BA6-AADB-D8BF3B495DF6');
$num = count($x->Vacancy);
//echo "num is $num";
$stopduplicates = array();
for ($i = 0; $i < $num; $i++) {
$job = $x->Vacancy[$i]->Job;
$closingdate = $x->Vacancy[$i]->ClosingDate;
// http://stackoverflow.com/questions/416548/forcing-a-simplexml-object-to-a-string-regardless-of-context
$vacancyid = (string) $x->Vacancy[$i]->VacancyID;
if (!in_array($vacancyid, $stopduplicates)) {
echo '
<tr class="job-row">
<td class="job-cell">'.$job.'</td>
<td class="date-cell">'.$closingdate.'</td>
<td class="apply-cell">
Apply Here
</td>
</tr>';
}
$stopduplicates[] = $vacancyid;
} //print_r($stopduplicates);
?>
So far what i have been trying to do is output a video response i know it wont work if i dont have the $id after the tag but before the tag it wont even work if the str_replace doesn't work.
<?php
$html = "";
$url = "https://www.youtube.com/feeds/videos.xml?user=Fliberjig1";
$xml = simplexml_load_file($url);
for($i = 0; $i < 6; $i++) {
$id = $xml->entry[$i]->id;
$id = str_replace("YT:VIDEO:","", $id);
$title = $xml->entry[$i]->title;
$html .= "<li><h3>$title</h3>
<p>$id</p>
<iframe width='854' height='480' src='https://www.youtube.com/embed/$id' frameborder='0' allowfullscreen></iframe></li>";
}
echo $html;
?>
the result will be like this
YT:VIDEO:WOCIEMNSI4C
but i want it to be like this
WOCIEMNSI4C
Can you please help in any way to help me with my problem
If I understand you correctly, you just want to parse the string. This can be done using:
$pieces = explode(":", $id);
$clean_id = $pieces[2];
It seems that the feed contains yt:video:WOCIemNSI4c and not YT:VIDEO:WOCIEMNSI4C so what you actually need is the str_ireplace
function instead of str_replace you're currently using.
So if you change your 7th line of code to this:
$id = str_ireplace("YT:VIDEO:","", $id);
you should be ok.
$id = "YT:VIDEO:WOCIEMNSI4C";
$rest = substr("$id", -11); //result WOCIEMNSI4C
I need to read an XML file, i watched some tutorials and tried different sollutions, but for some reason I can't figure out why it doenst work.
The XML file that I want to read: http://www.voetbalzone.nl/rss/rss.xml
This is the code that im using:
$xml= "http://www.voetbalzone.nl/rss/rss.xml"
for ($i = 0; $i < 10; $i++)
{
$title = $xml->rss->channel->item[$i]->title;
}
The error I get: Premature end of data in tag
It works for me like this:
<?php
$xml = simplexml_load_file("http://www.voetbalzone.nl/rss/rss.xml");
for ($i = 0; $i < 10; $i++)
{
$title = $xml->channel->item[$i]->title;
}
?>
Note that you are overwriting the variable $title each time, so that you will have the title of the 10. element in it after the loop finished [I assume that is not what you want?]
To get all 'item'-Elements inside 'channel' as an Array to iterate through you can use xpath like this:
<?php
$xml = simplexml_load_file("http://www.voetbalzone.nl/rss/rss.xml");
$item_array = $xml->xpath("//rss/channel/item");
foreach($item_array as $item) {
echo $item->title . "\n";
}
?>
I would suggest to read about php's SimpleXML here: http://php.net/manual/en/book.simplexml.php
Hello I want to parse a HTML table and assign those values to php variables so that i can insert them in to mysql database
I have tried some html parsing methods like dom_html_parsing but as a beginer i am getting much confused, i would be gald if some provides me some hint on this so that i can code
Parsing code i used is
include('simple_html_dom.php');
$dom = str_get_html($result);
$table = array();
$html = str_get_html($result);
foreach($html->find('tr') as $row) {
$time = $row->find('td',-1)->plaintext;
$title = $row->find('td',0)->plaintext;
$title0 = $row->find('td',1)->plaintext;
$title1 = $row->find('td',2)->plaintext;
$title2 = $row->find('td',3)->plaintext;
$title3 = $row->find('td',4)->plaintext;
$title4 = $row->find('td',5)->plaintext;
$title5 = $row->find('td',6)->plaintext;
$table[$title][$title0][$title1][$title2][$title3][$title4][$title3] = true;
}
echo '<pre>';
print_r($table);
echo '</pre>';
the arrays are printing but i do not know how to insert those particular values in to mysql database, i want to assign those values to variables first so that i can insert in to the database and the format i need is shown above, name and fathername & htno are printed only once in html table but i need it to be repeated with each row of table
Please help me
$table_data = array();
$dom = new DOMDocument();
$dom->loadHTML($html_string);
$rows = $dom->getElementsByTagName('tr');
for ($i = 0; $i < $rows->length; $i++) {
$cells = $rows->item($i)->getElementsByTagName('td');
for ($j = 0; $j < $cells->length; $j++) {
$table_data[$i][$j] = $cells->item($j)->textContent;
}
}
//print_r($table_data);
You can use phpquery. It's similar to jQuery, but for PHP.
I am trying to fetch the name,address and location from crawling of a website . Its a single page and dont want any other thing other than this. I am using the below code.
<?php
include 'simple_html_dom.php';
$html = "http://www.phunwa.com/phone/0191/2604233";
$dom = new DomDocument();
$dom->loadHtml($html);
$xpath = new DomXpath($dom);
$div = $xpath->query('//*[#class="address-tags"]')->item(0);
for($i=0; $i < $div->length; $i++ )
{
print "nodename=".$div->item( $i )->nodeName;
print "\t";
print "nodevalue : ".$div->item( $i )->nodeValue;
print "\r\n";
echo $link->getElementsByTagName("<p>");
}
?>
The website html source code is
<div class="address-tags">
<p><strong>Name:</strong> RAJ GOPAL SINGH</p>
<p><strong>Address:</strong> R/O BARNAI NETARKOTHIAN, P.O.MUTHI TEH.& DISTT.JAMMU,X, 181206</p>
<p><strong>Location:</strong> JAMMU, Jammu & Kashmir, India</p>
<p><strong>Other Numbers:</strong> 01912604233 | +911912604233 | +91-191-2604233</p>
Can somone please help me get the three attributes as output. Nothing is echop on the page as of now.
Thanks alot .
you need $dom->load($html); instead of $dom->loadHtml($html);. After doing this you wil; find your html is not well formed, so $xpath stay empty.
Maybe try something like:
$html = file_get_contents('http://www.phunwa.com/phone/0191/2604233');
$name = preg_replace('/(.*)(<p><strong>Name:<\/strong> )([^<]+)(<\/p>)(.*)/mis','$3',$html);
$address = preg_replace('/(.*)(<p><strong>Address:<\/strong> )([^<]+)(<\/p>)(.*)/mis','$3',$html);
$location = preg_replace('/(.*)(<p><strong>Location:<\/strong> )([^<]+)(<\/p>)(.*)/mis','$3',$html);
$othernumbers = preg_replace('/(.*)(<p><strong>Other Numbers:<\/strong> )(.*)/mis','$3',$html);
list($othernumbers,$trash)= preg_split('/<\/p>/mis',$othernumbers,0);
echo 'name: '.$name.'<br>address: '.$address.'<br>location: '.$location.'<br>other numbers: '.$othernumbers;
exit;
You should use the following for your XPath query:
//*[#class='address-tags']/p
so you're retrieving the actual paragraph nodes that are children of the 'address-tags' parent. Then you can use a loop on them:
$nodes = $xpath->query('//*[#class="address-tags"]/p');
for ($i = 0; $i < $nodes->length; $i++) {
echo $nodes->item($i)->nodeValue;
}
// or just
foreach($nodes as $node) {
echo $node->nodeValue;
}
Right now your code is properly fetching the first div that's found, but then you continue treating that div as if it was a DOMNodeList returned from an xpath query, which is incorrect. ->item() returns a DOMNode object, which does NOT have an ->item() method.