tags:

views:

100

answers:

3

I get this error:

Notice: Trying to get property of non-object in

Applies to: echo $result->Data;

And this output:

Array ()

Background Informations

A function returns a string which contains an XML file.

I want to get some data from two tags and deal with them on their own.

String Data

$data="
<SyncML xmlns='SYNCML:SYNCML1.0'> 
<SyncHdr> 
</SyncHdr> 
<SyncBody> 
   <betameta>
         WANT 1
   </betameta> 
   <Add> 
      <Data>
         WANT 2
      </Data>
   </Add> 
</SyncBody> 
</SyncML>";

In the above data, I want values "WANT 1" and "WANT 2"

Code so far

$xml = simplexml_load_string($data);
$result = $xml->xpath("/SyncML/SyncBody");
print_r($result);
echo $result->Data;
A: 

Remove the trailing slash.

k_b
Thanks, did that and now I get "Array ( ) "
Sochin
Yes, access item 0 in your array to get your only result.
k_b
I'm using: print_r($result);Doesn't that output all entries in the array?
Sochin
Maybe you need to specify the namespace which you declared in the xmlns attribute?
k_b
A: 

Hi,

The only solution I can find is the following:

<?php

$data= <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<SyncML> 
<SyncHdr> 
</SyncHdr> 
<SyncBody> 
   <betameta>
         WANT 1
   </betameta> 
   <Add> 
      <Data>
         WANT 2
      </Data>
   </Add> 
</SyncBody> 
</SyncML>
XML;

$xml = simplexml_load_string($data);
$result = $xml->xpath("/SyncML/SyncBody");
print_r($result);
echo $result;

is there anyway you can loose the xmlns?

This will output:

Array
(
    [0] => SimpleXMLElement Object
        (
            [betameta] => 
         WANT 1

            [Add] => SimpleXMLElement Object
                (
                    [Data] => 
         WANT 2

                )

        )

)
Martin
Fantastic! I'll chop the xmlns out!Thank you, Martin!
Sochin
@Sochin: I would not recommend this. Deal with the namespace, it's easy enough (see Rik's answer).
Tomalak
+2  A: 
$xml->registerXPathNamespace('default','SYNCML:SYNCML1.0');
$result = $xml->xpath("/default:SyncML/default:SyncBody");
Wrikken
+1, much better than kicking out the namespace from the source.
Tomalak