Getting XML attributes in PHP - php

Looked at a few other SO posts on this but no joy.
I've got this code:
$url = "http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml";
$string = file_get_contents($url);
$string = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $string);
$xml = simplexml_load_string($string);
foreach ($xml->entry as $val) {
echo "RESULTS: " . $val->attributes() . "\n";
but I can't get any results.
I'm specifically interested in getting the ID value which would be 549592189 in this fragment:
<id im:id="549592189" im:bundleId="com.activision.wipeout">http://itunes.apple.com/us/app/wipeout/id549592189?mt=8&uo=2</id>
Any suggestions?

SimpleXML gives you can easy way to drill down in the XML structure and get the element(s) you want. No need for the regex, whatever it does.
<?php
// Load XML
$url = "http://itunes.apple.com/us/rss/toppaidapplications/limit=10/genre=6014/xml";
$string = file_get_contents($url);
$xml = new SimpleXMLElement($string);
// Get the entries
$entries = $xml->entry;
foreach($entries as $e){
// Get each entriy's id
$id = $e->id;
// Get the attributes
// ID is in the "im" namespace
$attr = $id->attributes('im', TRUE);
// echo id
echo $attr['id'].'<br/>';
}
DEMO: http://codepad.viper-7.com/qNo7gs

Try with xpath:
$doc = new DOMDocument;
#$doc->loadHTML($string);
$xpath = new DOMXpath($doc);
$r = $xpath->query("//id/#im:id");
$id = $r->item(0)->value;

Try:
$sxml = new SimpleXMLElement($url);
for($i = 0;$i <=10;$i++){
$appid= $sxml->entry[$i]->id->attributes("im",TRUE);
echo $appid;
}

Related

PHP: XPath query returns nothing from large XML

$newstring = substr_replace("http://ws.spotify.com/search/1/track?q=", $_COOKIE["word"], 39, 0);
/*$curl = curl_init($newstring);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);*/
//echo $result;
$xml = simplexml_load_file($newstring);
//print_r($xml);
$xpath = new DOMXPath($xml);
$value = $xpath->query("//track/#href");
foreach ($value as $e) {
echo $e->nodevalue;
}
This is my code. I am using spotify to supply me with an xml document. I am then trying to get the href link from all of the track tags so I can use it. Right now the print_r($xml) I have commented out works, but if I try to query and print that out it returns nothing. The exact link I am trying to get my xml from is: http://ws.spotify.com/search/1/track?q=incredible
This maybe is not the answer you need, because I dropped the DOMXPath, I'm using getElementsByTagName() instead.
$url = "http://ws.spotify.com/search/1/track?q=incredible";
$xml = file_get_contents( $url );
$domDocument = new DOMDocument();
$domDocument->loadXML( $xml );
$value = $domDocument->getElementsByTagName( "track" );
foreach ( $value as $e ) {
echo $e->getAttribute( "href" )."<br>";
}

Why array result is duplicated?

Can anyone help test this code and tell me what the error is?
The expected result is 01223658060102111111. but it is duplicated like this
01223658060102111111012236580601021111110122365806010211111101223658060102111111
here is my code
<?php
ini_set('user_agent', 'My-Application/2.5'); //without this file_get_content would not work
$saveURL = fopen("url.txt", "w");
$html = file_get_contents("http://www.carlist.my/used-cars/2592832/2004-toyota-camry-2-0.html");
$dom = new DOMDocument();
#$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$cont = $xpath->evaluate("//ul[contains(#class, 'list-contact')]/li");
foreach($cont as $con){
echo $con->nodeValue;
}
Replace this line to
$cont = $xpath->evaluate("//ul[contains(#class, 'list-contact')]/li");
to
$cont = $xpath->evaluate("//ul[contains(#class, 'list-contact')][1]/li");
#july77: How can I split them to get result 0122365806 and 060102111111
$arr = array();
foreach($cont as $con){
$arr[] = $con->nodeValue;
}
$first = $arr[0];
$second = $arr[1];

how to get xml tag by name

I have this XML:
<InternalData>
<DataSet>
<Table>
<Expire_x0>2050-12-12T00:00:00+02:00</Expire_x0>
</Table>
</DataSet>
</InternalData>
How do I get the value of the <Expire_x0> tag?
I tried this:
$result = $s->__call("XmlString",array($params));
$obj_pros = get_object_vars($result);
$xml = $obj_pros['XmlStringResult'];
$xml = simplexml_load_string($xml);
$x = $xml->getElementsByTagName("Expire_x0");
echo $x;
without success. What is wrong?
If I understand correctly, you're looking to get the value of the <Expire_x0> tag, for which you can use this code.
$xml = simple_xml_load_string($xml); //Load XML String
$xml = json_decode(json_encode($xml), true); //Convert to Standard PHP Array
$expire = $xml['InternalData']['DataSet']['Table']['Expire_x0']; //Find Tag Value
Either:
$dom = new DOMDocument;
$dom->loadXML($xml);
$expiresx0 = $dom->getElementsByTagName('Expire_x0');
foreach ($expiresx0 as $expirex0) {
echo $expirex0->nodeValue, PHP_EOL;
}
http://php.net/manual/de/domdocument.getelementsbytagname.php
Or:
$xml = simplexml_load_string(xml);
echo $xml->DataSet->Table->Expire_x0;
http://php.net/manual/de/function.simplexml-load-file.php
Looks like you mixed both.
try this
<?php
$k = '<InternalData>
<DataSet>
<Table>
<Expire_x0>2050-12-12T00:00:00+02:00</Expire_x0>
</Table>
</DataSet>
</InternalData>';
$xml = simplexml_load_string($k);
echo $xml->DataSet->Table->Expire_x0;
}
Output
2050-12-12T00:00:00+02:00
So many ways...
Which one is preferred?
is it better to use :
$dom = new DOMDocument;
$dom->loadXML($xml);
$expiresx0 = $dom->getElementsByTagName('Expire_x0');
or :
$xml2 = simplexml_load_string($xml);
$xml2 = json_decode(json_encode($xml2), true);
$expire = $xml2['DataSet']['Table']['Expire_x0'];
or maybe:
$xml1 = simplexml_load_string($xml);
$x = $xml1->DataSet[0]->Table[0]->Expire_x0;
echo $x;
?
You should use like:
$xml = simplexml_load_string($xml);
echo $xml->DataSet->Table->Expire_x0;
Do not use root tag InternalData

Xpath for extracting links

I create an scraper for an automoto site and first I want to get all manufactures and after that all links of models for each manufactures but with the code below I get only the first model on the list. Why?
<?php
$dom = new DOMDocument();
#$dom->loadHTMLFile('http://www.auto-types.com');
$xpath = new DOMXPath($dom);
$entries = $xpath->query("//li[#class='clearfix_center']/a/#href");
$output = array();
foreach($entries as $e) {
$dom2 = new DOMDocument();
#$dom2->loadHTMLFile('http://www.auto-types.com' . $e->textContent);
$xpath2 = new DOMXPath($dom2);
$data = array();
$data['newLinks'] = trim($xpath2->query("//div[#class='modelImage']/a/#href")->item(0)->textContent);
$output[] = $data;
}
echo '<pre>' . print_r($output, true) . '</pre>';
?>
SO I need to get: mercedes/100, mercedes/200, mercedes/300 but now with my script i get only the first link so mercedes/100...
please help
You need to iterate through the results instead of just taking the first item:
$items = $xpath2->query("//div[#class='modelImage']/a/#href");
$links = array();
foreach($items as $item) {
$links[] = $item->textContent;
}
$data['newLinks'] = implode(', ', $links);

xpath won't retrieve elements

Here is the URL of the xml source:
I'm tryng to grab all the RichText elements using xpath relative location and then print the elementID attribute. It is outputting nothing though. Any ideas?
<?php
$url = "FXG";
$xml = simplexml_load_file($url);
//print_r($xml);
$textNode = $xml->xpath("//RichText");
$count = count($textNode);
$i = 0;
while($i < $count)
{
echo '<h1>'.$textNode[$i]['s7:elementID'].'</h1>';
$i++;
}
?>
You need to register the namespaces that are set in the xml
$url = "http://testvipd7.scene7.com/is/agm/papermusepress/HOL_12_F_green?&fmt=fxgraw";
$xml = simplexml_load_file($url);
$xml->registerXPathNamespace('default', 'http://ns.adobe.com/fxg/2008');
$xml->registerXPathNamespace('s7', 'http://ns.adobe.com/S7FXG/2008');
$textNode = $xml->xpath("//default:RichText/#s7:elementID");
foreach($textNode as $node) {
echo '<h1>'.$node[elementID].'</h1>';
}
I hope this helps.
Strange. This, however, works.
$textNode = $xml->xpath("//*[name() = 'RichText']");

Categories