Jump to content

PHP and XML - Get Attributes


spec36

Recommended Posts

I have the following xml file called -> test.xml

<?xml version="1.0"?>
<OrderCollection xmlns:xsd="http://www.org/2009/XMLSchema" xmlns:xsi="http://www.w3.org/2009/XMLSchema-instance">
  <Orders>
    <Order Id="130" Model=" Optimized" CampaignName="Promo" OrderName="1074" CreationDate="2013-05-29T16:30:03.41" StartDate="2013-06-03T04:00:00" EndDate="2014-04-22T03:59:59.997" OrderStatus="Active">
      <Client Id="5" ExternalId="5" Name="CNL" />
      <Lines>
		<Line Id="699" Status="Canceled" EstimatedReach="0" Desired="1000" Actual="370" MaxViewings="0" Separation="0">
		<Line Id="700" Status="Canceled" EstimatedReach="10830" Desired="1000" Actual="0" MaxViewings="0" Separation="0">
		<Line Id="701" Status="Canceled" EstimatedReach="0" Desired="1000" Actual="0" MaxViewings="0" Separation="0">
		<Line Id="714" Status="Canceled" EstimatedReach="10830" Desired="1000" Actual="1478410" MaxViewings="0" Separation="0">
		<Line Id="908" Status="Active" EstimatedReach="0" Desired="1000" Actual="1520" MaxViewings="0" Separation="0">
		<Line Id="916" Status="Canceled" EstimatedReach="0" Desired="1000" Actuals="5260" MaxViewings="0" Separation="0">
	  </Lines>
    </Order>
	 </Orders>
</OrderCollection>

For each <Line> node with a entry that has a status attribute of (Status="Active") I need to list The corresponding ID number. So from the XML above I need to pull out only Line Id="908", since it is the only one with a Status equal to Active.

 

I have so far wrote this PHP code to attempt to accomplish this task.

<?php
$url = 'test.xml';
$xml = simplexml_load_file($url);

foreach ($xml->xpath("//Line/@Status") as $t)
{
        echo $t . PHP_EOL;
}


?>

The above code will print out all the "Status" attribute values properly from each xml LINE node, but I do not know how to put in a check on the xpath command to see if the @Status is set to active and then echo out, only the corresponding Id attribute (i.e 908 in this case)

 

 

Any help would be greatly appreciated. Thanks,

Link to comment
https://forums.phpfreaks.com/topic/287826-php-and-xml-get-attributes/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.