Update only Xml node (PHP5)

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

    Update only Xml node (PHP5)

    Hi all

    can I update a xml node AND write the new value in a file witout
    re-write all the file ?

    sample :

    my xml file :

    <sample>
    <exo id="1" value="Test" />
    </sample>

    i want to modify the file like

    <sample>
    <exo id="1" value="Test updated" />
    </sample>

    is it possible ?

  • ImOk

    #2
    Re: Update only Xml node (PHP5)

    An XML file is like an HTML file or TXT file. Normally using the Dom
    XML you cannot read and update part of an XML file directly off disk.
    You must read the whole file in memory, change part of it in memory and
    then you must rewrite the whole file back to disk.

    However there are database engines that do this (I've read about them
    but never used them). But your XML file will have a lot of extra
    attributes used by the database engine.

    It depends what you are doing.

    jhullu@gmail.co m wrote:
    Hi all
    >
    can I update a xml node AND write the new value in a file witout
    re-write all the file ?
    >
    sample :
    >
    my xml file :
    >
    <sample>
    <exo id="1" value="Test" />
    </sample>
    >
    i want to modify the file like
    >
    <sample>
    <exo id="1" value="Test updated" />
    </sample>
    >
    is it possible ?

    Comment

    • Richard Levasseur

      #3
      Re: Update only Xml node (PHP5)


      jhullu@gmail.co m wrote:
      Hi all
      >
      can I update a xml node AND write the new value in a file witout
      re-write all the file ?
      >
      sample :
      >
      my xml file :
      >
      <sample>
      <exo id="1" value="Test" />
      </sample>
      >
      i want to modify the file like
      >
      <sample>
      <exo id="1" value="Test updated" />
      </sample>
      >
      is it possible ?
      No. Think about it: Where are those extra 8 bytes coming from? A
      file is a contiguous piece of data, you can't insert into it randomly,
      it has to be reallocated. (if you wanted to get really technically, it
      could potentially be possible to create a small piece and jumping
      around using i nodes, but thats way beyond the scope of our discussion)

      Comment

      • Noodle

        #4
        Re: Update only Xml node (PHP5)

        You will have to read the whole file to perform this task, but if your
        using PHP5 this can be done in 1 step, using the get
        file_get_conten ts() method to read the xml document into a string.

        The following code will make the value change, and it isn't much
        trouble to do...

        <?php

        # Set Values
        $my_file = 'myfile.xml';
        $new_value = 'Test updated';

        # Get file contents
        $xml = file_get_conten ts($my_file);

        # Regex
        $xml = preg_replace('< exo id="(.*)" value="(.*)" />', "exo id=\"$1\"
        value=\"$new_va lue\" /", $xml);

        # Write file out again
        file_put_conten ts($my_file, $xml);

        ?>


        jhullu@gmail.co m wrote:
        Hi all
        >
        can I update a xml node AND write the new value in a file witout
        re-write all the file ?
        >
        sample :
        >
        my xml file :
        >
        <sample>
        <exo id="1" value="Test" />
        </sample>
        >
        i want to modify the file like
        >
        <sample>
        <exo id="1" value="Test updated" />
        </sample>
        >
        is it possible ?

        Comment

        • jhullu@gmail.com

          #5
          Re: Update only Xml node (PHP5)

          ok, it's works...

          thanks for your help.


          Noodle wrote:
          You will have to read the whole file to perform this task, but if your
          using PHP5 this can be done in 1 step, using the get
          file_get_conten ts() method to read the xml document into a string.
          >
          The following code will make the value change, and it isn't much
          trouble to do...
          >
          <?php
          >
          # Set Values
          $my_file = 'myfile.xml';
          $new_value = 'Test updated';
          >
          # Get file contents
          $xml = file_get_conten ts($my_file);
          >
          # Regex
          $xml = preg_replace('< exo id="(.*)" value="(.*)" />', "exo id=\"$1\"
          value=\"$new_va lue\" /", $xml);
          >
          # Write file out again
          file_put_conten ts($my_file, $xml);
          >
          ?>
          >
          >
          jhullu@gmail.co m wrote:
          Hi all

          can I update a xml node AND write the new value in a file witout
          re-write all the file ?

          sample :

          my xml file :

          <sample>
          <exo id="1" value="Test" />
          </sample>

          i want to modify the file like

          <sample>
          <exo id="1" value="Test updated" />
          </sample>

          is it possible ?

          Comment

          • ImOk

            #6
            Re: Update only Xml node (PHP5)

            Since this is XML you may want to consider using PHP's Dom class (not
            Dom XML) to do the updating.

            Assuming you have this file: book.xml

            <sample>
            <exo id="1" value="Test" />
            <exo id="2" value="Test2" />
            </sample>

            This example code will search for all the tags with 'exo' and change
            the one whose id=='1'. It will then write back the file.

            <?php
            $dom=new DOMDocument();
            $dom->load('book.xml ');
            $dom->formatOutput = true;
            echo $dom->saveXML(); // show before file
            $allnodes = $dom->getElementsByT agName('exo');
            foreach ($allnodes as $node) {
            if ($node->nodeName=='exo ' and $node->getAttribute(' id')=='1') {
            $val=$node->getAttribute(' value') . ' updated';
            $node->setAttribute ( 'value', $val );
            }
            }
            echo $dom->saveXML(); //show after file
            $dom->save('book.xml ');
            exit;
            ?>



            jhullu@gmail.co m wrote:
            ok, it's works...
            >
            thanks for your help.
            >
            >
            Noodle wrote:
            You will have to read the whole file to perform this task, but if your
            using PHP5 this can be done in 1 step, using the get
            file_get_conten ts() method to read the xml document into a string.

            The following code will make the value change, and it isn't much
            trouble to do...

            <?php

            # Set Values
            $my_file = 'myfile.xml';
            $new_value = 'Test updated';

            # Get file contents
            $xml = file_get_conten ts($my_file);

            # Regex
            $xml = preg_replace('< exo id="(.*)" value="(.*)" />', "exo id=\"$1\"
            value=\"$new_va lue\" /", $xml);

            # Write file out again
            file_put_conten ts($my_file, $xml);

            ?>


            jhullu@gmail.co m wrote:
            Hi all
            >
            can I update a xml node AND write the new value in a file witout
            re-write all the file ?
            >
            sample :
            >
            my xml file :
            >
            <sample>
            <exo id="1" value="Test" />
            </sample>
            >
            i want to modify the file like
            >
            <sample>
            <exo id="1" value="Test updated" />
            </sample>
            >
            is it possible ?

            Comment

            Working...