PHP Forum

PHP Forum (http://www.selfphp.de/forum/index.php)
-   PHP Grundlagen (http://www.selfphp.de/forum/forumdisplay.php?f=12)
-   -   XML DOM PHP4 nach PHP5 (http://www.selfphp.de/forum/showthread.php?t=21709)

till_ch 27.06.2009 00:13:33

XML DOM PHP4 nach PHP5
 
hallots!

bin neu hier und hab gleich mal ein anliegen:

bitte gebt mir tipps wie ich folgendes script in php 5 zum laufen bring.?!?

Code:

<?php
class MenuMaker {

var $objDom;
var $arrResult;

function MenuMaker($arrResult) {
$this->arrResult = $arrResult;
$this->objDom = domxml_new_doc("1.0");
$ndRoot =
$this->objDom->append_child($this->objDom->create_element("menu"));
foreach($this->arrResult AS $arrTopRow) {
if($arrTopRow["parent_id"] == 0) {
$this->handleMenuItem($arrTopRow,$ndRoot);
}
}
}

function handleMenuItem($arrRow,$nd) {

// display the thing...
$ndItem = $this->printMenuItem($arrRow,$nd);

//{{{ create a temporrary stack of immediate children
$arrTmp = array();
foreach($this->arrResult AS $arrSubRow) {
if($arrRow["id"] == $arrSubRow["parent_id"]) {
$arrTmp[] = $arrSubRow;
}
}
//}}}

// execute the immdiate children in a row (ordered)
foreach($arrTmp AS $arrSubRow){
$this->handleMenuItem($arrSubRow,$ndItem);
}
}

function printMenuItem($arrRow,$nd) {
$ndItem = $nd->append_child($this->objDom->create_element("item"));
foreach($arrRow AS $key=>$val) {
$ndTitle =
$ndItem->append_child($this->objDom->create_element($key));
$ndTitle->set_content($val);
}
return $ndItem;
}

function getXml() {
return $this->objDom->dump_mem(true);
}
}

$arrFakeResult = array();
$arrFakeResult[] =
array("id"=>1,"parent_id"=>0,"title"=>"project","h ref"=>"project/");
$arrFakeResult[] =
array("id"=>2,"parent_id"=>0,"title"=>"faq","href" =>"faq/");
$arrFakeResult[] =
array("id"=>3,"parent_id"=>0,"title"=>"contact","h ref"=>"contact.php");
$arrFakeResult[] =
array("id"=>4,"parent_id"=>1,"title"=>"aims","href "=>"project/aims/");
$arrFakeResult[] =
array("id"=>5,"parent_id"=>1,"title"=>"objectives" ,"href"=>"project/obj/");
$arrFakeResult[] =
array("id"=>6,"parent_id"=>4,"title"=>"easy","href "=>"project/aims/easy.php");
$arrFakeResult[] =
array("id"=>7,"parent_id"=>4,"title"=>"scalable"," href"=>"project/aims/sclable.php");
$arrFakeResult[] =
array("id"=>8,"parent_id"=>4,"title"=>"robust","hr ef"=>"project/aims/robust.php");
$arrFakeResult[] = array("id"=>9,"parent_id"=>5,"title"=>"object
oriented","href"=>"project/obj/oo.php");
$arrFakeResult[] =
array("id"=>10,"parent_id"=>5,"title"=>"xml","href "=>"project/obj/xml.php");

$objMM = new MenuMaker($arrFakeResult);
header("Content-Type: text/xml");
echo $objMM->getXml();

?>

danke.
till.


**edit**

habs selbst hinbekommen.
eine nacht php dom.
falls wers brauchen kann:


Code:


<?php

class MenuMaker {

var $objDom;
var $arrResult;

function MenuMaker($arrResult) {
$this->arrResult = $arrResult;
$this->objDom = new DOMDocument();

  $this->objDom->preserveWhiteSpace = false;


$ndRoot =
$this->objDom->appendChild($this->objDom->createElement("menu"));
foreach($this->arrResult as $arrTopRow) {
if($arrTopRow["parent"] == 0) {
$this->handleMenuItem($arrTopRow,$ndRoot);
}
}
}

function handleMenuItem($arrRow,$nd) {

// display the thing...
$ndItem = $this->printMenuItem($arrRow,$nd);

//{{{ create a temporrary stack of immediate children
$arrTmp = array();
foreach($this->arrResult as $arrSubRow) {

if($arrRow["id"] == $arrSubRow["parent"]) {
$arrTmp[] = $arrSubRow;
}
}
//}}}

// execute the immdiate children in a row (ordered)
foreach($arrTmp as $arrSubRow){
$this->handleMenuItem($arrSubRow,$ndItem);
}
}


function printMenuItem($arrRow,$nd) {
$ndTitle = $nd->appendChild($this->objDom->createElement("item"));
foreach($arrRow as $key=>$val) {

$ndTitle->setAttribute($key,$val);
}
return $ndTitle;
}



function getXml() {
return $this->objDom->saveXML();
}
}

require_once('../array_from_db.php');

$objMM = new MenuMaker($arrFakeResult);

echo $objMM->getXml();

?>


und der array schaut so aus:


Code:


Array
(
    [0] => Array
        (
            [0] => HOME
            [id] => 1
            [parent] => 0
            [title] => HOME
            [display] => 1
            [artist_id] => random
        )

    [1] => Array
        (
            [0] => MALEREI
            [id] => 2
            [parent] => 0
            [title] => MALEREI
        )

    [2] => Array
        (
            [0] => REISETAGEBUCH
            [id] => 3
            [parent] => 0
            [title] => REISETAGEBUCH
        )

    [3] => Array
        (
            [0] => FOTOGRAFIE
            [id] => 4
            [parent] => 0
            [title] => FOTOGRAFIE
        )

    [4] => Array
        (
            [0] => SKULPTUR
            [id] => 5
            [parent] => 0
            [title] => SKULPTUR
        )

    [5] => Array
        (
            [0] => 19. JHDT
            [id] => 7
            [parent] => 2
            [title] => 19. JHDT
        )

    [6] => Array
        (
            [0] => 20. JHDT
            [id] => 8
            [parent] => 2
            [title] => 20. JHDT
        )

    [7] => Array
        (
            [0] => ITALIEN
            [id] => 9
            [parent] => 3
            [title] => ITALIEN
        )

    [8] => Array
        (
            [0] => USA
            [id] => 10
            [parent] => 3
            [title] => USA
        )

    [9] => Array
        (
            [0] => EXPERIMENTS
            [id] => 11
            [parent] => 4
            [title] => EXPERIMENTS
        )

    [10] => Array
        (
            [0] => ALUGUSS
            [id] => 12
            [parent] => 5
            [title] => ALUGUSS
        )

    [11] => Array
        (
            [0] => Claude Monet
            [id] => 13
            [parent] => 7
            [title] => Claude Monet
            [text] =>
            [display] => true
            [artist_id] => 191
        )

    [12] => Array
        (
            [0] =>  Renoir
            [id] => 14
            [parent] => 7
            [title] =>  Renoir
            [text] =>
            [display] => true
            [artist_id] => 180
        )

    [13] => Array
        (
            [0] => Gottfried Helnwein
            [id] => 15
            [parent] => 8
            [title] => Gottfried Helnwein
            [text] =>
            [display] => true
            [artist_id] => 189
        )

    [14] => Array
        (
            [0] => Edward Hopper
            [id] => 16
            [parent] => 8
            [title] => Edward Hopper
            [text] =>
            [display] => true
            [artist_id] => 190
        )

    [15] => Array
        (
            [0] => Pablo Picasso
            [id] => 17
            [parent] => 8
            [title] => Pablo Picasso
            [text] =>
            [display] => true
            [artist_id] => 188
        )

    [16] => Array
        (
            [0] => Otto Dix
            [id] => 18
            [parent] => 8
            [title] => Otto Dix
            [text] =>
            [display] => true
            [artist_id] => 187
        )

    [17] => Array
        (
            [0] => Venedig 
            [id] => 19
            [parent] => 9
            [title] => Venedig 
            [text] =>
            [display] => true
            [artist_id] => 184
        )

    [18] => Array
        (
            [0] => Florenz 
            [id] => 20
            [parent] => 9
            [title] => Florenz 
            [text] =>
            [display] => true
            [artist_id] => 182
        )

    [19] => Array
        (
            [0] => Rom 
            [id] => 21
            [parent] => 9
            [title] => Rom 
            [text] =>
            [display] => true
            [artist_id] => 183
        )

    [20] => Array
        (
            [0] => Philadelphia 
            [id] => 22
            [parent] => 10
            [title] => Philadelphia 
            [text] =>
            [display] => true
            [artist_id] => 186
        )

    [21] => Array
        (
            [0] => New York
            [id] => 23
            [parent] => 10
            [title] => New York
            [text] =>
            [display] => true
            [artist_id] => 185
        )

    [22] => Array
        (
            [0] => Sperrmüll 
            [id] => 24
            [parent] => 11
            [title] => Sperrmüll 
            [text] =>
            [display] => true
            [artist_id] => 179
        )

    [23] => Array
        (
            [0] => Die drei Schwestern II 
            [id] => 25
            [parent] => 12
            [title] => Die drei Schwestern II 
            [text] =>
            [display] => true
            [artist_id] => 181
        )

)



Alle Zeitangaben in WEZ +2. Es ist jetzt 13:23:23 Uhr.

Powered by vBulletin® Version 3.8.3 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.