Display data within XML tags in PHP

The XML that's received is below. How can I get the values ('AI', '3', '20.78'...) and display them in PHP? The values are always returned in that order but the length can vary.

<?xml version="1.0" encoding="UTF-8"?>
<Data>
    <Item Type="AI" Chan="3" Value="20.78" Manual="OFF" Min="0.00" Max="100.00" Units="degC" Name="Workshop Temp" />
</Data>

Any ideas would be much appreciated!

Answers


You might find the documentation of SimpleXMLElement::attributes useful,

SimpleXMLElement::attributes — Identifies an element's attributes

Return Values

Returns a SimpleXMLElement object that can be iterated over to loop through the attributes on the tag.

Returns NULL if called on a SimpleXMLElement object that already represents an attribute and not a tag.

here is how you should use it:

$str = <<< XML
<?xml version="1.0" encoding="UTF-8"?>
<Data>
    <Item Type="AI" Chan="3" Value="20.78" Manual="OFF" Min="0.00" Max="100.00" Units="degC" Name="Workshop Temp" />
</Data>
XML;
$xml = simplexml_load_string($str);
foreach($xml->Item[0]->attributes() as $key => $att) {
    echo $att."\n";
}

Need Your Help

How to embed a matplotllib histogram onto a tkinter GUI?

python user-interface python-3.x matplotlib

I have this program that creates a histogram from a list of medians from randomly generated numbers within specific parameters (upper limit, sample size). How can I make it so that the histogram th...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.