Jump to content

Using SimpleXML, can't get data


Mike521

Recommended Posts

I've probably been looking at this too long and need to just take a break. I have the following data in XML form, brought into an object via simplexml_load_string:

 

object(SimpleXMLElement)#1 (1) {
  ["Dynamics_x0020_NAV_x0020_Data"]=>
  object(SimpleXMLElement)#2 (5) {
    ["Item_x0020_Stock_x0020_3406S"]=>
    object(SimpleXMLElement)#3 (2) {
      ["Item_x0020_Stock_x0020_3406S_Inventory"]=>
      string(5) "22964"
      ["Item_x0020_Stock_x0020_3406S_Qty_x0020_on_x0020_Sales_x0020_Order"]=>
      string(4) "1197"
    }
  }
}

 

When I try to get the data (aka the inventory) it comes up blank. But if I want the value of "Item_x0020_Stock_x0020_3406S" it returns data. For example the following code:

 

		$theInventoryObject = "Item_x0020_Stock_x0020_$style";
	$theSalesOrderObject = "Item_x0020_Stock_x0020_$style";
	$theInventoryName = "Item_x0020_Stock_x0020_$style_Inventory";
	$theSalesOrderName = "Item_x0020_Stock_x0020_$style_Qty_x0020_on_x0020_Sales_x0020_Order";
	$theItemInventory = $theStockReturn->Dynamics_x0020_NAV_x0020_Data->$theInventoryObject->$theInventoryName;
	$theItemSalesOrder = $theStockReturn->Dynamics_x0020_NAV_x0020_Data->$theSalesOrderObject->$theSalesOrderName;
	echo "<br>Item Inventory: $theItemInventory<br>Item Sales Order: $theItemSalesOrder<br>";
	echo "<br>Inventory Object:<br>";
	var_dump( $theStockReturn->Dynamics_x0020_NAV_x0020_Data->$theInventoryObject );
	echo "<br>Sales Object:<br>";
	var_dump( $theStockReturn->Dynamics_x0020_NAV_x0020_Data->$theInventoryObject->$theInventoryName );

 

outputs this:

 

Inventory Object:
object(SimpleXMLElement)#14 (2) { ["Item_x0020_Stock_x0020_3406S_Inventory"]=> string(5) "22964" ["Item_x0020_Stock_x0020_3406S_Qty_x0020_on_x0020_Sales_x0020_Order"]=> string(4) "1197" }
Sales Object:
object(SimpleXMLElement)#12 (0) { }
Item Inventory:
Item Sales Order: 

 

Can anyone see what I'm missing?

Link to comment
https://forums.phpfreaks.com/topic/102409-using-simplexml-cant-get-data/
Share on other sites

this is the original xml:

 

<?xml version="1.0" encoding="utf-8"?>
<Dynamics_x0020_NAV_x0020_Data>
<Item_x0020_Stock_x0020_3406S>
<Item_x0020_Stock_x0020_3406S_Inventory>22964</Item_x0020_Stock_x0020_3406S_Inventory>
<Item_x0020_Stock_x0020_3406S_Qty_x0020_on_x0020_Sales_x0020_Order>1197</Item_x0020_Stock_x0020_3406S_Qty._x0020_on_x0020_Sales_x0020_Order>
</Item_x0020_Stock_x0020_3406S>
</Dynamics_x0020_NAV_x0020_Data>

 

I'm trying to pull the values of 3406S_Inventory and 3406S_Qty

 

I forgot to put in the first post:

$style = 3406S;

I figured it out, the following lines were bugged:

 

$theInventoryName = "Item_x0020_Stock_x0020_$style_Inventory";
$theSalesOrderName = "Item_x0020_Stock_x0020_$style_Qty_x0020_on_x0020_Sales_x0020_Order";

 

The variable was $style, but the way I tried to insert it into this string, PHP thought I was looking for a variable called $style_Inventory, similarly for the next line.

 

I changed them to:

 

$theInventoryName = "Item_x0020_Stock_x0020_" . $style . "_Inventory";
$theSalesOrderName = "Item_x0020_Stock_x0020_" . $style . "_Qty_x0020_on_x0020_Sales_x0020_Order";

 

and it worked perfectly

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.