Jump to content

XML formatted string to associative array


jsonchiu

Recommended Posts

I'm working on a php socket server for my chat room, and I need to parse some very short XML messages like this:

<msg t="usr"><body a="pubMsg" u="username"><![CDATA[some message]]></body></msg>

 

Right now I'm using a bunch of substrings (start laughing. lol), but it's very inflexible. However somehow it works...

 

The problem arises when I try to store user variables (ignore the indents and line brakes, in reality, there will be non of them; we don't want to waste bandwidth)

<msg t="usr">
<body a="updVar" u="username">
	<var t="s" n="varname"><![CDATA[some variable]]></var>
	<var t="n" n="someNum"><![CDATA[3]]></var>
</body>
</msg>

 

I have no idea how to parse those stuff from a string to an associative array...

What I want is an associative array that I can access those stuff similar to this way:

 

echo myXML['firstChild']['firstChild']['firstChild']['attributes']['t'];

 

and it should output "s"

Any help would be appreciated.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.