By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,206 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,206 IT Pros & Developers. It's quick & easy.

PHP and XML - Read/Structure help wanted

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.