0

I'm working with this XML data, and I'm trying to loop through the Order elements within the OrderArray element.

I'm able to target the elements using this:

$ebay_orders = $DOM->getElementsByTagName("Order");

This returns the number of Orders in the array, for example:

DOMNodeList Object ( [length] => 2 )

It's been awhile since I've worked with XML like this, and I'm struggling to remember how I can get into these Order Elements and pull individual element values (ie. OrderID, OrderStatus, etc.)

Any information on this would be greatly appreciated. Thanks!

Drew Angell
  • 25,968
  • 5
  • 32
  • 51
  • Maybe these solutions will be of benefit on parsing the xml client side .... https://stackoverflow.com/questions/7949752/cross-browser-javascript-xml-parsing – GetSet Aug 09 '20 at 03:23

1 Answers1

1

In PHP XML, we can use Node Values to get Specific Elements. For e.g., I am trying to get OrderID for the first order in the following xml. You can fetch xml data from either hosted url or a sample file. Its upto your requirements. For demonstration purposes, I am going to use hosted file. The code can be:

$file = "sample.xml";
$url = "https://www.tekbuff.com/ebay-apps/logs/GetOrdersResponse_1596940374.xml";

$xml = simplexml_load_file($url); /* Replace $url with $file */

echo $xml->OrderArray->Order[0]->OrderID; 

Here $xml->OrderArray->Order[0]->OrderID; will retrieve OrderID like 19-05533-xxxx for first key. Similarly, you can retrieve as many node values as you like.

my_workbench
  • 300
  • 3
  • 8
  • Well, I'm not loading from a file in reality. That was just a sample to look at. I'm working within a script that's pulling this data back via an API, so the XML string is available directly as opposed to loading a file. Is there something similar for that? – Drew Angell Aug 09 '20 at 04:41
  • Ah, simplexml_load_string. I'm going to play with that and then I'll accept your answer. thanks. – Drew Angell Aug 09 '20 at 04:42
  • sure. I have updated the answer in case you have xml stored in a file or a variable – my_workbench Aug 09 '20 at 04:52