modify xml file php script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jp.delatorre@gmail.com

    modify xml file php script

    how to create script that modify xml file

    sample xml file:

    <root>
    <parent name="a" id="1">
    <child name="a.a" id="1.1" />
    <child name="a.b" id="1.2" />
    <child name="a.c" id="1.3" />
    </parent>
    <parent name="b" id="2">
    <child name="b.a" id="2.1" />
    </parent>
    <parent name="c" id="3">
    <child name="c.a" id="3.1" />
    <child name="c.b" id="3.2" />
    </parent>
    </root>

    php script should be able to modify the xml file. it should be able to
    insert a parent element say between parent A and parent B. the id of
    the new parent element should be parent B with its child respectively.
    the parent elements below the new element should adjust the ids. in the
    example above, parent B id should be 3 and its child id should be 3.1.
    same goes to parent C and its childs.
    result would be:
    <root>
    <parent name="a" id="1">
    <child name="a.a" id="1.1" />
    <child name="a.b" id="1.2" />
    <child name="a.c" id="1.3" />
    </parent>
    <parent name="newparent " id="2">
    <child name="newparent .a" id="2.1" />
    <child name="newparent .b" id="2.2" />
    </parent>
    <parent name="b" id="3">
    <child name="b.a" id="3.1" />
    </parent>
    <parent name="c" id="4">
    <child name="c.a" id="4.1" />
    <child name="c.b" id="4.2" />
    </parent>
    </root>

    any comments on this...

  • Wolfgang Forstmeier

    #2
    Re: modify xml file php script

    Hi,

    DOM should be your friend, for Documentation read this:



    Have fun ..

    Wolfgang

    jp.delatorre@gm ail.com wrote:[color=blue]
    > how to create script that modify xml file
    >
    > sample xml file:
    >
    > <root>
    > <parent name="a" id="1">
    > <child name="a.a" id="1.1" />
    > <child name="a.b" id="1.2" />
    > <child name="a.c" id="1.3" />
    > </parent>
    > <parent name="b" id="2">
    > <child name="b.a" id="2.1" />
    > </parent>
    > <parent name="c" id="3">
    > <child name="c.a" id="3.1" />
    > <child name="c.b" id="3.2" />
    > </parent>
    > </root>
    >
    > php script should be able to modify the xml file. it should be able to
    > insert a parent element say between parent A and parent B. the id of
    > the new parent element should be parent B with its child respectively.
    > the parent elements below the new element should adjust the ids. in the
    > example above, parent B id should be 3 and its child id should be 3.1.
    > same goes to parent C and its childs.
    > result would be:
    > <root>
    > <parent name="a" id="1">
    > <child name="a.a" id="1.1" />
    > <child name="a.b" id="1.2" />
    > <child name="a.c" id="1.3" />
    > </parent>
    > <parent name="newparent " id="2">
    > <child name="newparent .a" id="2.1" />
    > <child name="newparent .b" id="2.2" />
    > </parent>
    > <parent name="b" id="3">
    > <child name="b.a" id="3.1" />
    > </parent>
    > <parent name="c" id="4">
    > <child name="c.a" id="4.1" />
    > <child name="c.b" id="4.2" />
    > </parent>
    > </root>
    >
    > any comments on this...
    >[/color]

    Comment

    • dem0o8

      #3
      Re: modify xml file php script

      actually, im more concern about the logic behind of the script. how do
      you make the id update in cascading manner. anyway, thanks for the reply

      Comment

      • Wolfgang Forstmeier

        #4
        Re: modify xml file php script

        Hm ..

        this maybe an answer but there could be better solutions that my one :-)

        <?php

        // ... fetch your id with DOM

        $id_array = explode(".", $id);

        // id[0] == first number of ID
        // id[1] == second number of ID
        // count up second one

        id[1]++;

        // connect id that you have back x.x

        $id = implode(".",$id[0].$id[1]);

        // write id back to XML (using DOM) ...

        ?>

        dem0o8 wrote:[color=blue]
        > actually, im more concern about the logic behind of the script. how do
        > you make the id update in cascading manner. anyway, thanks for the reply
        >[/color]

        Comment

        • dem0o8

          #5
          Re: modify xml file php script

          i guess that will work for me though i haven't tried it yet. thanks a
          lot wolfgang.

          Comment

          Working...