PHP, how Get Value from xml

I have xml data like this

$message= '<Messages name="AccountData" MessageID="IN202209090001" type="IncomingMessage"><Record name="recordAccount"><Field name="externalReference">er20220909001a</Field><Field name="memberID">abc</Field><Field name="participantCode">AA</Field><Field name="description">John woo</Field><Field name="investorMemberUID">AA01</Field><Field name="uniqueIdentifier">AA0166</Field><Field name="accountTax">ID</Field><Field name="accountingBalanceSide">PASSIVE</Field><Field name="type">DEPOSITORY</Field><Field name="status">AVAILABLE</Field><Field name="paymentBankUID">KSEPB</Field><Field name="correspondentAccountNumber">123</Field><Field name="lastUpdateTimestamp">20220999161</Field><Field name="number">AA00166</Field></Record></Messages>' ;

i try to get all value from that message,
then i write code like this

$json = json_encode($message,true);
$xml = json_decode( str_replace( ‘`’, ‘"’, $json ), true );

if i add code
echo $xml[‘MessageID’] ,after run i got message "PHP Warning: Illegal string offset ‘MessageID’ "

if i add code
echo $xml->MessageID; , after run i got message "Notice: Trying to get property ‘MessageID’ of non-object "

please help

1 Like

These string is neither XML nor JSON format. So you cannot parse it with any library

It’s not great XML, but it is XML.

$xml_parser = xml_parser_create();

So there is no need of a XML header tag?

Not to parse the string, no.
The prolog (the <?xml ... ?> tag) is optional. (the specification says it “SHOULD” begin with the XML declaration, but not “MUST”.)

1 Like

close ,i got it with echo $xml[“@attributes”][“name”];

For reference, there is an RFC that explains what MUST, SHOULD and MAY mean in RFCs:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.