PHP Simplexml + Get Attribute

Accessing @attribute from SimpleXML

You can get the attributes of an XML element by calling the attributes() function on an XML node. You can then var_dump the return value of the function.

More info at php.net
http://php.net/simplexmlelement.attributes

Example code from that page:

$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}

Get an attribute value using SimpleXML for PHP

You can use the attributes() function on the node to get it's attributes:

$xml_str = '<xml>
<node>
<someTag cp="c2">content</someTag>
</node>
</xml>';
$res = simplexml_load_string($xml_str);

$items = $res->xpath("//someTag");
var_dump((string) $items[0]->attributes()->cp);

The returned element is an SimpleXMLElement, so in order to use it I converted it to string (using the (string) cast).

How to get attributes of XML root element using SimpleXMLElement

When loadinG to simplexml, attributes of the root element became attributes of the simpleXml object. So, you can get it just

$str = '<?xml version="1.0" encoding="iso-8859-1"?>
<wwwjob id="32cca11IACH" method="Delete">
</wwwjob>';
$vacancyXML = simplexml_load_string($str);

echo $vacancyXML['method']; // Delete

demo

Get attribute value in SimpleXML object by element and another attribute value

You can use SimpleXML's xpath method to return an attribute of a node based on the value of another attribute:

$sxml = simplexml_load_string($xml);

$rate = (float) $sxml->xpath('./currencies/currency[@id="EUR"]/@rate')[0];

echo $rate;

Note that the method will always return an array, so we need to ask for the first element, and then cast the value to a float.

See https://eval.in/957883 for a full example

Get attributes and values using SimpleXML

This code should work:

$xml = new SimpleXMLElement($xmlString);
$source = $xml->event->attributes()->source;
$timestamp = $xml->event->attributes()->timestamp;
$subject = $xml->event->subject;
$action = $xml->event->action;
$message = $xml->event->message;

... where $xmlString is the string of the xml file.

Read up on how to use simpleXML here.

Hope this helped and good luck!

PHP SimpleXML - Get Attribute Values - Loop

You could try something like this :

$xml=simplexml_load_file("http://2strok.com/gen/maler.xml") or
die("Error: Cannot create object");
foreach($xml->ResultList->XResult as $res) {
if ($res->Contacts->XContact) {
echo $res->Contacts->XContact->Name . "<br>";
echo $res->Contacts->XContact->Value . "<br>";
echo $res->Contacts->XContact->VisitationAddress . "<br>";
}
}

Or this, if you want all contacts :

$xml=simplexml_load_file("http://2strok.com/gen/maler.xml") or
die("Error: Cannot create object");
foreach($xml->ResultList->XResult as $res) {
foreach ($res->Contacts->XContact as $elm) {
echo $elm->Name . "<br>";
echo $elm->Value . "<br>";
echo $elm->VisitationAddress . "<br>";
}
}

simpleXML get node child based on attribute

Simply loop the poster elements and remember to cast the attribute values to strings, since you want to compare them (and probably output them) as strings:

$xml = simplexml_load_file('PosterData.xml');

foreach ($xml->poster as $poster) {
if ((string) $poster['id'] == 'minwage') {
echo (string) $poster->full_image['url'];
}
}

SimpleXml Get Attribute Value

Your main problem is that SimpleXML won't parse media:thumbnail. I wrote a quick solution for you, but I'm pretty sure there is a more efficient way of doing this:

header('Content-Type: text/html');

$rss = simplexml_load_string(
// replace media:thumbnail with mediathumbnail
str_replace(
'media:thumbnail',
'mediathumbnail',
file_get_contents($requestURL)
)
);

echo '<ul>';
foreach($rss->channel->item as $post)
{
echo '<li>';
// getting mediathumbnail attributes
$attributes = $post->mediathumbnail->attributes();
// this is the thumbnail url
$max = $attributes->url;
echo $max;
echo '<a href="'.$post->link.'">'.$post->title.'</a>';
echo '</li>';
}
echo '</ul>';

Hope I helped. :)



Related Topics



Leave a reply



Submit