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
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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.