Help with XML

I’m trying to read a part of a SimpleXMLElement/XML file. The problem is I can’t get the desired text anyway I try.

I tried debugging the XML I had, so I printed it this way:

[php] foreach($response->items as $item) {
fwrite($file, var_export($item, TRUE));
}[/php]
which outputs

[php]SimpleXMLElement::__set_state(array( ‘item’ => array ( 0 => SimpleXMLElement::__set_state(array( ‘id’ => ‘000-1’, ‘description’ => ‘Notebook Prata’, ‘quantity’ => ‘1’, ‘amount’ => ‘1830.00’, )), 1 => SimpleXMLElement::__set_state(array( ‘id’ => ‘AB01’, ‘description’ => ‘Notebook Preto’, ‘quantity’ => ‘2’, ‘amount’ => ‘1340.00’, )), ), ))[/php]
But when I try to retrieve the data inside the specific items, the return is empty. I tried the following:
[php]
fwrite($file, $item->amount);
//
fwrite($file, $item->{‘amount’});
//
fwrite($file, $items->$item->amount);[/php]
But nothing seems to work.

How can I correct the syntax, to obtain the expected? I would want the file to have written ‘1830.00’ and ‘1340.00’, but I only get blanks.

XML REFERENCE:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> 2011-02-10T16:13:41.000-03:00 9E884542-81B3-4419-9A75-BCC6FB495EF1 REF1234 1 3 1 101 49900.00 0.00 0.40 1644.80 49900.00 0.00 1 2 0001 Notebook Prata 1 24300.00 0002 Notebook Rosa 1 25600.00 José Comprador [email protected] 11 56273440 Av. Brig. Faria Lima 1384 5o andar Jardim Paulistano 01452002 Sao Paulo SP BRA 1 21.50

I haven’t read in a xml file in a long time, but would you not be better in reading it in as an object?

http://php.net/manual/en/function.simplexml-load-file.php

They give this for an example:
[php]<?php
// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.

if (file_exists(‘test.xml’)) {
$xml = simplexml_load_file(‘test.xml’);

echo "<pre>" . print_r($xml,1) . "</pre>";  // will give you what your xml file looks like as objects:

} else {
exit(‘Failed to open test.xml.’);
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service