473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

PHP and XML - Read/Structure help wanted

Hi,
I'm trying to implement a simple MVC app, and I want to have the site
map in an XML file.
Anyways, here is the test xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<site>
<page id="" name="Home">
</page>

<page id="music" name="Music">
</page>

<page id="pix" name="Pictures">
<page id="news" name="News">
</page>
</page>
</site>
And here is what I get in PHP:
Array ( [tag] => PAGE [type] => complete [level] => 2 [attributes] =>
Array ( [ID] => [NAME] => Home ) [value] => )

Array ( [tag] => SITE [value] => [type] => cdata [level] => 1 )

Array ( [tag] => SITE [value] => [type] => cdata [level] => 1 )

Array ( [tag] => PAGE [type] => complete [level] => 2 [attributes] =>
Array ( [ID] => music [NAME] => Music ) [value] => )

Array ( [tag] => SITE [value] => [type] => cdata [level] => 1 )

Array ( [tag] => SITE [value] => [type] => cdata [level] => 1 )

Array ( [tag] => PAGE [type] => open [level] => 2 [attributes] => Array
( [ID] => pix [NAME] => Pictures ) [value] => )

Array ( [tag] => PAGE [type] => complete [level] => 3 [attributes] =>
Array ( [ID] => news [NAME] => News ) [value] => )

Array ( [tag] => PAGE [value] => [type] => cdata [level] => 2 )

Array ( [tag] => PAGE [type] => close [level] => 2 )

Array ( [tag] => SITE [value] => [type] => cdata [level] => 1 )
Skipping the first and last tag and using the following code:
// convert to php array/parse
$this->siteMap = array();
for ($i=1; $i<count($xmlStruc)-1; $i++) {
$tag = $xmlStruc[$i];
print_r($tag); echo "<p />";
}

What I really want is something like this:

site = array(
"id1" => array("name"=>"namw1","options"=>"",...),
"id2" => array(...),
etc.
);
Perhaps I am not so familiar with how XML is parsed, I get really
frustrated that I'd have to parse all these cdata things and all. Isn't
there a simplier way to create a tree of objects from an XML file? Or do
I really have to take everything into account?

Thanks,
Lüph
Aug 1 '05 #1
1 1478

Lüpher Cypher wrote:
Hi,
I'm trying to implement a simple MVC app, and I want to have the site
map in an XML file.
Anyways, here is the test xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<site>
<page id="" name="Home">
</page>

<page id="music" name="Music">
</page>

<page id="pix" name="Pictures">
<page id="news" name="News">
</page>
</page>
</site>
And here is what I get in PHP:
Array ( [tag] => PAGE [type] => complete [level] => 2 [attributes] =>
Array ( [ID] => [NAME] => Home ) [value] => )

Array ( [tag] => SITE [value] => [type] => cdata [level] => 1 )

Array ( [tag] => SITE [value] => [type] => cdata [level] => 1 )

Array ( [tag] => PAGE [type] => complete [level] => 2 [attributes] =>
Array ( [ID] => music [NAME] => Music ) [value] => )

Array ( [tag] => SITE [value] => [type] => cdata [level] => 1 )

Array ( [tag] => SITE [value] => [type] => cdata [level] => 1 )

Array ( [tag] => PAGE [type] => open [level] => 2 [attributes] =>Array
( [ID] => pix [NAME] => Pictures ) [value] => )

Array ( [tag] => PAGE [type] => complete [level] => 3 [attributes] =>
Array ( [ID] => news [NAME] => News ) [value] => )

Array ( [tag] => PAGE [value] => [type] => cdata [level] => 2 )

Array ( [tag] => PAGE [type] => close [level] => 2 )

Array ( [tag] => SITE [value] => [type] => cdata [level] => 1 )
Skipping the first and last tag and using the following code:
// convert to php array/parse
$this->siteMap = array();
for ($i=1; $i<count($xmlStruc)-1; $i++) {
$tag = $xmlStruc[$i];
print_r($tag); echo "<p />";
}

What I really want is something like this:

site = array(
"id1" => array("name"=>"namw1","options"=>"",...),
"id2" => array(...),
etc.
);
Perhaps I am not so familiar with how XML is parsed, I get really
frustrated that I'd have to parse all these cdata things and all. Isn't
there a simplier way to create a tree of objects from an XML file? Or do
I really have to take everything into account?

Thanks,
Lüph


a (maybe) stupid question 1st: it looks like you're just parsing xml to
create a navigation: why not use an array in the 1st place, which saves
you parsing xml at all?

if that is not an option: the parsing results lokk like you're using
the more simple xml parser functions, which are fast but do not really
deliver a tree.

try using the DOM xml functions instead, which is slower, but delivers
a tree.

micha

Aug 1 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
3
by: Michael Van Altena via .NET 247 | last post by:
I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayout" attribute with both LayoutKind.Explicit andLayoutKind.Sequential...
7
by: Lynn | last post by:
I am rewriting some memory management code and I need to have a forward declaration of a data structure. I am not converting this data structure into a class (yet). How do I generate a forward...
2
by: Blankdraw | last post by:
.... somewhere, a newbie is dying ... Is there anybody out there who can help me get the right input for the following segment? I am trying to read entire records of 5 (2-digit) integers at a...
19
by: Holger Hasselbach | last post by:
- The value of the object allocated by the malloc function is used (7.20.3.3). - The value of any bytes in a new object allocated by the realloc function beyond the size of the old object are used...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
2
by: Eric | last post by:
Hi, I need to send data from a file into a structure. In Vb6 this was done like so. public Type Struct1 v1 as string v2 as string end type
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
24
by: rudranee | last post by:
hi there, can anyone tell me how to lines from a file which are odd numbered i.e. 1st,3rd,5th...lines. i tried incrementing file pointer by 2 (fp=fp+2) but it does'nt work Can someone give me...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.