Jump to content

[SOLVED] xml to array


benjaminbeazy

Recommended Posts

OK guys, I've been working with PHP for some time now but I have very little experience with XML related code.

 

I have access to the Pownce API which returns an XML document for the specified query and I'd like to convert that into a multidimensional array following the structure of the XML. Not sure if there is something already is PHP to do this or what. I played around a little with xml_parse but it wasn't digging into each node for subnodes and returning the whole shebang as I'd like.

 

Example XML:

<note>
<id>a number</id>
<permalink>a url</permalink>
<type>message</type>
<body>text</body>
<timestamp>1193827966</timestamp>
<seconds_since>2395</seconds_since>
<display_since>19 min ago</display_since>
<num_recipients>19</num_recipients>
<stars>0.0</stars>
<is_public>1</is_public>
<sender>
<username>someuser</username>
<permalink>a url</permalink>
<first_name>someuser's name</first_name>
<short_name>someuser's name</short_name>
<is_pro>0</is_pro>
<profile_photo_urls>
<tiny_photo_url>a url</tiny_photo_url>
<small_photo_url>a url</small_photo_url>
</profile_photo_urls>
<blurb>some text</blurb>
<location>some text</location>
<country>some text</country>
<gender>some gender</gender>
</sender>
<recipients>
<user>
<username>someuser</username>
<permalink>a url</permalink>
<first_name>someuser's name</first_name>
<short_name>someuser's name</short_name>
<is_pro>0</is_pro>
<profile_photo_urls>
<tiny_photo_url>a url</tiny_photo_url>
<small_photo_url>a url</small_photo_url>
</profile_photo_urls>
<blurb>some text</blurb>
<location>some text</location>
<country>some text</country>
<gender>some gender</gender>
</user>
</recipients>
</note>

<note>
<id>a number</id>
<permalink>a url</permalink>
<type>message</type>
<body>some text</body>
<timestamp>1193827966</timestamp>
<seconds_since>2395</seconds_since>
<display_since>19 min ago</display_since>
<num_recipients>19</num_recipients>
<stars>0.0</stars>
<is_public>1</is_public>
<sender>
<username>someuser</username>
<permalink>a url</permalink>
<first_name>someuser's name</first_name>
<short_name>someuser's name</short_name>
<is_pro>0</is_pro>
<profile_photo_urls>
<tiny_photo_url>a url</tiny_photo_url>
<small_photo_url>a url</small_photo_url>
</profile_photo_urls>
<blurb>some text</blurb>
<location>some text</location>
<country>some text</country>
<gender>some gender</gender>
</sender>
<recipients>
<user>
<username>someuser</username>
<permalink>a url</permalink>
<first_name>someuser's name</first_name>
<short_name>someuser's name</short_name>
<is_pro>0</is_pro>
<profile_photo_urls>
<tiny_photo_url>a url</tiny_photo_url>
<small_photo_url>a url</small_photo_url>
</profile_photo_urls>
<blurb>some text</blurb>
<location>some text</location>
<country>some text</country>
<gender>some gender</gender>
</user>
</recipients>
</note>

 

Would like this to return a multidimensional array follow same structure such as...

 

[Note] =>array([id] => a number, etc.. [sender] => array([username] => someuser, etc... [profile_photo_urls] => array([tiny_photo_url] => a url, etc..))),

 

[Note] =>array([id] => a number, etc.. [sender] => array([username] => someuser, etc... [profile_photo_urls] => array([tiny_photo_url] => a url, etc..)))

 

Hope this is clear. Like I said, little XML experience so there could be something blatantly obvious that I've never seen. I just don't wanna have to write a verbose datamining script if I don't have to...

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.