0

Having an issue echo'ing xml tags.

PHP:

$xml = $insureFormResult->returned;
$xml1 = new SimpleXMLElement($xml);
$result = $xml1->xpath("response")[0];
echo $result;

If I echo $xml it gives me:

<?xml version="1.0" encoding="utf-8"?>
<response>
  <errors>
    <error code="7">Your details are already in our system and have been forwarded to our insurance partners who will contact you shortly</error>
  </errors>
</response> 

The xml will always have one response tag. I also want to know how to echo the tag with id 'code'. I tried the php above but there's no result echoed.

Any kind of help will be appreciated!

EDIT

It wasn't working because of the version of PHP on my server.

designtocode
  • 2,215
  • 4
  • 21
  • 35
  • 2
    See http://stackoverflow.com/questions/2811797/get-root-node-of-xml-doc-using-simplexml `` is the root node of your example, and SimpleXML won't traverse it. `xpath("/response")` will work. – Michael Berkowski May 25 '14 at 13:30
  • @MichaelBerkowski: Thanks for the link, was just searching for the dupe. – hakre May 25 '14 at 13:31
  • 1
    @msbodetti You will find more documentation and help about SimpleXML here: [Basic SimpleXML usage](http://www.php.net/manual/en/simplexml.examples-basic.php) – hakre May 25 '14 at 13:32
  • @hakre And I was searching for the docs which explain this before I CV'd – Michael Berkowski May 25 '14 at 13:33
  • @MichaelBerkowski I already tried that and it is blank as well.. – designtocode May 25 '14 at 13:36
  • @msbodetti You should get something... http://codepad.viper-7.com/gHFD6L – Michael Berkowski May 25 '14 at 13:37
  • I know I should get a response because if I take away [0] it echos Array. It's just empty on my side.. so I don't understand what's the issue. – designtocode May 25 '14 at 13:39
  • 1
    1) Do you have display_errors enabled? 2) Are you using PHP 5.4 or later? You need 5.4 for array dereferencing a function call `()[]`. If you get no output, you might actually have a fatal error. `echo $response[0]->errors->error;` returns your string. http://codepad.viper-7.com/62FVWb – Michael Berkowski May 25 '14 at 13:42
  • @MichaelBerkowski that worked.. I guess it's a server issue. – designtocode May 25 '14 at 13:45

1 Answers1

0

I'm not really familiar with PHP, but the xpath to access the error with attribute code with value 7 is:

/response/errors/error[@code='7']
StuartLC
  • 104,537
  • 17
  • 209
  • 285