This question already has answers here:
SimpleXML: Selecting Elements Which Have A Certain Attribute Value
(2 answers)
Closed 8 years ago.
I'm getting a xml using the file_get_contents function and then creating a SimpleXMLElement with it.
The xml file can be seen here: http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Nirvana&api_key=0ca5b0824b7973303c361510e7dbfced
The problem is that I need to get the value of lfm->artist->image[#size='small'] and I can't find how to do it.
You should use DOMXPath for this: http://php.net/manual/en/class.domxpath.php
This XPath query would work for your XML:
\\lfm\artist\image[#size='small']
As follows:
$doc = new DOMDocument();
$doc->loadXML($url);
$xpath = new DOMXpath($doc);
$elements = $xpath->query("\\lfm\artist\image[#size='small']");
if (!is_null($elements)) {
foreach ($elements as $element) {
echo "<br/>[". $element->nodeName. "]";
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
Related
I want to be able to extract information from specific nodes from an external XML file. I currently have been trying
$contents = file_get_contents('https://experiencehermann.com/post-sitemap.xml');
$dom = new DOMDocument;
$dom -> loadXML($contents);
$finder = new DOMXPath($dom);
$nodes = $finder->query('//loc');
foreach ($nodes as $node) {
echo $node->nodeValue ."</br />";
}
I'm able to use this same technique when I have the XML in the PHP directly but not when pulling from an external source.
Thanks in advance!
As your query is quite simple, you don't even need XPath, you can simply use the getElementsByTagName method on the DOMDocument object:
$dom = new DOMDocument;
$dom->loadXML($contents);
$nodes = $dom->getElementsByTagName('loc');
foreach ($nodes as $node) {
if ($node->nodeName === 'image:loc')
continue;
echo $node->nodeValue ."<br />\n";
}
This question already has answers here:
Loop over DOMDocument
(4 answers)
Closed 1 year ago.
Is it possible to get all HTML elements (children) with content using PHP (DOMDocument class)? I just can't get the results. Let say I only know that I will have <td> tag but don't know what tags would be inside <td>
Example:
$doc = new DOMDocument();
$el = "<td><a href='http://google.hr'>test1</a><div>Test2</div></td>";
$doc->loadHTML($el);
$doc->getElementsByTagName("td")->item(0)->nodeValue /* I only get plain text */
EDIT: No JavaScript like solution
This will give you all element information:
$html = "<td><a href='http://google.hr'>test1</a><div>Test2</div></td>";
$dom = new DOMDocument();
$dom->loadHTML($html);
foreach($dom->getElementsByTagName('*') as $element ){
echo "<pre>";
print_R($element);
echo "</pre>";
}
To get Attribute information use like:
$p = $dom->getElementsByTagName('a')->item(0);
if ($p->hasAttributes()) {
foreach ($p->attributes as $attr) {
$name = $attr->nodeName;
$value = $attr->nodeValue;
echo "Attribute '$name' :: '$value'<br />";
}
}
This question already has answers here:
How to get Open Graph Protocol of a webpage by php?
(8 answers)
Closed 8 years ago.
I am trying to retrieve some meta data included into a SimpleXMLElement. I am using XPATH and I struggle to get the value that interests me.
Here is an extract of the webpage header (from : http://www.wayfair.de/CleverFurn-Couchtisch-Abby-69318X2-MFE2223.html)
Do you know how I could retrieve all xmlns data in an array containing :
1) og:type
2) og:url
3) og:image
....
x) og:upc
<meta xmlns:og="http://opengraphprotocol.org/schema/" property="og:title" content="CleverFurn Couchtisch "Abby"" />
And here's my php code
<?php
$html = file_get_contents("http://www.wayfair.de/CleverFurn-Couchtisch-Abby-69318X2-MFE2223.html");
$doc = new DOMDocument();
$doc->strictErrorChecking = false;
$doc->recover=true;
#$doc->loadHTML("<html><body>".$html."</body></html>");
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//*/meta[#property='og:url']");
if (!is_null($elements)) {
foreach ($elements as $element) {
echo "<br/>[". $element->nodeName. "]";
var_dump($element);
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
?>
Just found the answer :
How to get Open Graph Protocol of a webpage by php?
<?php
$html = file_get_contents("http://www.wayfair.de/CleverFurn-Couchtisch-Abby-69318X2-MFE2223.html");
libxml_use_internal_errors(true); // Yeah if you are so worried about using # with warnings
$doc = new DomDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$query = '//*/meta[starts-with(#property, \'og:\')]';
$metas = $xpath->query($query);
foreach ($metas as $meta) {
$property = $meta->getAttribute('property');
$content = $meta->getAttribute('content');
$rmetas[$property] = $content;
}
var_dump($rmetas);
?>
This question already has answers here:
Getting DOM elements by classname
(7 answers)
Closed 9 years ago.
I know how to get the value based on id and tags while parsing HTML. But I don't know how to get the value based on classname. This is what I have tried:
$dom = new DOMDocument();
$dom->loadHTML($html);
$data = $dom->getElementsByTagName($identifier);
foreach ($data as $v)
{
$value[] = $v->nodeValue;
}
You have just tried to get elements by tagname. What you need to do is:-
1.Get all the elements by TagName.
2. Now take the classname from the tagname if exists.
3.Compare the classname wit ur input classname, if exists print that data.
Try out this , worked for me:-
$dom = new DOMDocument();
$dom->loadHTML($html);
$new_data = $dom->getElementsByTagName('*');
$matched = array();
//echo $data->nodeValue;
for ($i = 0; $i < $new_data->length; $i++) {
if (isset($new_data->item($i)->attributes->getNamedItem('class')->nodeValue)) {
$classname = $new_data->item($i)->attributes->getNamedItem('class')->nodeValue;
if ($classname == $identifier) {
$matched[] = $new_data->item($i)->nodeValue;
}
}
}
print_r($matched);
I'm looking for filtering this xml enter link description here
The best way to filter an xml it is :
-to run all the xml and to affect value in variable
-after that we rewrite this xml with this variable
is there any other method?
For this method i've used the dom like this:
$flux= new DOMDocument();
if ($flux->load('http://xml.weather.com/weather/local/FRXX0076?unit=m&hbhf=6&ut=C'))
{$loc=$flux->getElementsByTagName('t');
foreach($loc as $lo)
echo $lo->firstChild->nodeValue . "<br />";
}
in this code i've tried to display <t> but there are 2 balise <t> in <hour> , therefore i've two value of <t> instead the first child of <hour>
The brief answer:
$flux= new DOMDocument();
if ($flux->load('http://xml.weather.com/weather/local/FRXX0076?unit=m&hbhf=6&ut=C'))
{
$xpath = new DomXPath($flux);
$elements = $xpath->query("*/hour/t[1]");
if (!is_null($elements)) {
foreach ($elements as $element)
{
echo "<br/>[". $element->nodeName. "]";
$nodes = $element->childNodes;
foreach ($nodes as $node)
{
echo $node->nodeValue. "\n";
}
}
}
}
I think you should know where to go from this point :)
More advanced version will be to issue XPath query on all hour elements ("*/hour") and then in foreach for each hour element issue another xpath query in this element context ($xpath->query("*/t[1]", $hourElement);). This way you'll also have access to hour object and can for example display this hour.
UPDATE
Simpler version of foreach:
if (!is_null($elements)) {
foreach ($elements as $element)
{
echo "<br/>".$element->nodeValue;
}
}