Xml Php Dom help

Hi,
Im stuck when trying to retrieve a value from a xml file using DOM.

The portion of XML it is pertaining to is below and the values I am trying to retrieve are
ClientID href and OrderStatusType. Now I can retrieve OrderStatusType using the following line of code

$OrderStatus = $apiResultXML->getElementsByTagName('OrderChargeStatusType'); 
    			$OrderStatusResult  = $OrderStatus->item(0)->nodeValue; 

which works and I get the required value however when I try to use the same couple of lines to get the ClientID it doesnt work and returns the following error

PHP Catchable fatal error: Object of class DOMNodeList could not be converted to string in /home/xxxxx/xxxxx/_xxxx/OneShopNotificationListener.php on line 44


<?xml version="1.0" encoding="utf-8" ?> 
<Response success="true">
<OrderInfo>
  <Id>123456789789</Id> 
  <ClientId href="https://www.mcssl.com/API/xxxxxxx/Clients/987456321">987456321</ClientId> 
  <GrandTotal>0.01</GrandTotal> 
  <OrderPaymentType>PaypalStandard</OrderPaymentType> 
  <OrderChargeStatusType>Accepted</OrderChargeStatusType> 
  <OrderStatusType>Accepted</OrderStatusType>


Can anyone shed any light on why I cant work this out…
Thanks
Chris

Looks fine, in fact, tested it here (although I’ve had to guess the XML response structure, so this maybe an issue) too.

I also included a SimpleXML example, which maybe an option.


<?php
$xml = '<?xml version="1.0" encoding="utf-8" ?>
<Response success="true">
    <OrderInfo>
      <Id>123456789789</Id>
      <ClientId href="https://www.mcssl.com/API/xxxxxxx/Clients/987456321">987456321</ClientId>
      <GrandTotal>0.01</GrandTotal>
      <OrderPaymentType>PaypalStandard</OrderPaymentType>
      <OrderChargeStatusType>Accepted</OrderChargeStatusType>
      <OrderStatusType>Accepted</OrderStatusType>
    </OrderInfo>
</Response>
';

$doc = new DOMDocument();
$doc->loadXML($xml);

echo $client_id = $doc->getElementsByTagName('ClientId')->item(0)->nodeValue; #987456321


#simplexml
$sxe = new SimpleXMLElement($xml);
echo $sxe->OrderInfo->ClientId; #987456321

Your totally correct, It was my fault completey, I had a typo in my original code and once I typed it in here, I spotted it and corrected it - Its now working fine.

Thanks very much…
Chris

Can you show us the code you’re using to obtain the clientId?

Thats actually what I am struggling with at the moment. I assumed that it would work using the same couple of lines that I used to retrieve the OrderStatusType which is


$clientIDLine= $apiResultXML->getElementsByTagName('ClientId'); 
$clientIDResult  = $clientIDLine->item(0)->nodeValue;

but all i get back is the error message

PHP Catchable fatal error: Object of class DOMNodeList could not be converted to string in /home/xxxxx/xxxxx/_xxxx/OneShopNotificationListener.php on line 44

Thanks Very Much
Chris

Yay, good news! :smiley:

Ok, I have come up against another hurdle now,

This is another section of the same XML file and I need to retrieve the sku’s


- <LineItems>
- <LineItemInfo>
  <Id>241455047</Id> 
  <OrderId>154162455</OrderId> 
  <ProductId href="https://www.mcssl.com/API/xxxxxx/Products/xxxxxxx">xxxxxxx</ProductId> 
  <Quantity>1</Quantity> 
  <Sku>cmdlc</Sku> 
  <ProductName>s</ProductName> 
  <ProductType>Digital</ProductType> 
  <UnitPrice>77.00</UnitPrice> 
  <IsRecurring>false</IsRecurring> 
  <IsTaxable>false</IsTaxable> 
  <IsCommissionable>true</IsCommissionable> 
  <CreatedAt>2010-01-05T14:00:18.736224</CreatedAt> 
  <ModifiedAt>2010-01-05T14:00:18.736224</ModifiedAt> 
  <CreatedFromIp>174.16.80.83</CreatedFromIp> 
  <ModifiedFromIp>174.16.80.83</ModifiedFromIp> 
  <SelectedOptions /> 
  <ProductTaxes /> 
  <Discounts /> 
  </LineItemInfo>
- <LineItemInfo>
  <Id>241455048</Id> 
  <OrderId>154162455</OrderId> 
  <ProductId href="https://www.mcssl.com/API/xxxxxx/Products/xxxxxxx">xxxxxxx">xxxxx</ProductId> 
  <Quantity>1</Quantity> 
  <Sku>teammaker</Sku> 
  <ProductName>Team Maker</ProductName> 
  <ProductType>Digital</ProductType> 
  <UnitPrice>17.00</UnitPrice> 
  <IsRecurring>false</IsRecurring> 
  <IsTaxable>false</IsTaxable> 
  <IsCommissionable>true</IsCommissionable> 
  <CreatedAt>2010-01-05T14:00:18.736224</CreatedAt> 
  <ModifiedAt>2010-01-05T14:00:18.736224</ModifiedAt> 
  <CreatedFromIp>174.16.80.83</CreatedFromIp> 
  <ModifiedFromIp>174.16.80.83</ModifiedFromIp> 
  <SelectedOptions /> 
  <ProductTaxes /> 
  <Discounts /> 
  </LineItemInfo>
- <LineItemInfo>
  <Id>241455049</Id> 
  <OrderId>154162455</OrderId> 
  <ProductId href="https://www.mcssl.com/API/xxxxxx/Products/xxxxxxx">xxxxxxx">xxxxxx</ProductId> 
  <Quantity>1</Quantity> 
  <Sku>tatlyl1</Sku> 
  <ProductName>Teambuilding Activities for Teachers, Lecturers and Youth Leaders</ProductName> 
  <ProductType>Digital</ProductType> 
  <UnitPrice>17.00</UnitPrice> 
  <IsRecurring>false</IsRecurring> 
  <IsTaxable>false</IsTaxable> 
  <IsCommissionable>true</IsCommissionable> 
  <CreatedAt>2010-01-05T14:00:18.736224</CreatedAt> 
  <ModifiedAt>2010-01-05T14:00:18.736224</ModifiedAt> 
  <CreatedFromIp>174.16.80.83</CreatedFromIp> 
  <ModifiedFromIp>174.16.80.83</ModifiedFromIp> 
  <SelectedOptions /> 
  <ProductTaxes /> 
  <Discounts /> 
  </LineItemInfo>
  </LineItems>

I have just tried to use this below


$LineItem = $apiResultXML->getElementsByTagName('LineItems');
				
foreach( $LineItem as $key => $LineItemss)
{
$ThisSku = $LineItemss->getElementsByTagName('Sku');
$CurrentSku = $ThisSku->item(0)->nodeValue;
$CurrentSku = $CurrentSku . chr(13) . $CurrentSku;
}


The problem with this is that it only returned the first ‘sku’ 3 times instead of the 3 different ‘sku’s’

Anyone see what I am doing wrong here.

Thanks
Chris

//$thisSku will be a list of Sku elements
$ThisSku = $LineItemss->getElementsByTagName(‘Sku’);

//you’re fetching the first item in the list only
$CurrentSku = $ThisSku->item(0)->nodeValue;

You probably want another foreach loop in there to loop all Sku elements, instead of just the element at position 0 in your list.