Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 1st, 2005, 04:46 PM
Registered User
 
Join Date: Jun 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default xml parse to keyed-array

I feel like I must not understand the correct way to use XML, because I don't understand the xml_parse_into_struct function at all. What I want is a multi-dimensional keyed array. Seems like this funcitonality should be build in?

So, if I start out with:

<menu>
  <name>Products</name>
  <sections>
    <section>
      <name>Classroom</name>
    </section>
    <section>
      <name>Online</name>
    </section>
  </sections>
</menu>

I'd like to end up with something like this:

array (
  ["TAG"] => "menu"
  ["name"] => "Products"
  ["sections"] => array(
     array(["name"]=> "Classroom")
     array(["name"]=> "Online")
                       )
      )


Any ready-made way to do this?

Thanks.


 
Old March 1st, 2005, 07:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, but it should be pretty easy to write one using the XML parser routines. The PHP documentation for xml_parse_into_struct suggests that it doesn't create a nested (aka multidimensional) array that mimics the structure of the XML.

You first need to come up with a reasonable array structure. Looking at your XML, I can't tell how you would even write a simple algorithm to come up with that array to begin with.

For starters, your root element, <menu>, is represented in the array version as the VALUE of the "TAG" index.

Other tags, however, are represented as INDEXES (aka KEYS) into the array. E.g., <name> and <sections>.

Still other tags are not represented by name in the array at all... where are your <section> tags represented in your array?


What you need to do is put some thought into what your resulting array should look like. Then, whipping up the code to create that array shouldn't be too difficult...

You can also create an object-oriented tree structure where each tag is represented by a TreeNode class. If you have attributes in the tag, they can be represented by members of that class. Child tags can be stored in an array of child TreeNodes.


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
parse xml data everest SQL Server 2005 0 March 6th, 2008 02:40 AM
Parse xml (with validation) blackbird XML 2 October 2nd, 2007 08:27 AM
Parse XML in ASP delaneyp Classic ASP XML 1 February 6th, 2006 08:42 AM
about XML receive by tomcatand XML parse by java taianmhzy Servlets 0 May 20th, 2004 01:59 AM
about XML receive ang XML parse by tomcat taianmhzy Apache Tomcat 0 May 20th, 2004 01:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.