Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Reading, adding, writing xml...unexpected result

  1. #1
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Reading, adding, writing xml...unexpected result

    I have an .xml with three elements that I use to create an announcement sidebar on my pages.

    Code:
    <sidebar>
      <myquote>
         <id>1111</id>
         <thequote>Hello World</thequote>
        
         <reference>Abby Lee</reference>
      </myquote>

    </sidebar>
    The following code should receive elements from a form, read the.xml, add the new elements on the top, and then write the .xml.
    What I get is a new .xml with just the new data. Funny, I would think it would just give me the opposite. I believe I’m close but I just don’t see the problem. Can anyone point me to the problem? Thanks.

    Code:
    //Adding New Announcement
    //Item comes from form
    if (isset($_POST['iquote']))

    {

      //Save new Announcement to sidebar.xml
    
      $insertcount = $_POST["newcount"];
    
      $insertquote = $_POST["iquote"];
      
      $insertref = $_POST["iref"];
    
      $idoc = array();
    
      $idoc = new DOMDocument();	
    
      $idoc->load( '../sidebar.xml' );
    
      $sidebar = array();
   
      $sidebar[] = array(
                
         'id' => $insertcount,
                
         'thequote' => $insertquote,
                
         'reference' => $insertref
                
         );
    
      //Read sidebar.xml and add to array
    
      $isidebar = $idoc->getElementsByTagName( "myquote" );
    
      foreach( $isidebar as $imyquote )
    
         {
         
         $iids = $imyquote->getElementsByTagName( "id" );
         
         $iid = $iids->iten(0) ->nodeValue;
    
         $ithequotes = $imyquote->getElementsByTagName( "thequote" );
   
         $ithequote = $ithequotes->item(0) ->nodeValue;
    
         $ireferences = $imyquote->getElementsByTagName( "reference" );
 
         $ireference = $ireferences->item(0) ->nodeValue;
    
         $sidebar[] = array(
                 
                'id' => $iid,
                 
                'thequote' => $ithequote,
                 
                'reference' => $ireference
                  );
                }
    
    //rewrite xml
    $wdoc = new DOMDocument();

    $wdoc ->formatOutput = true;
    
    $w = $wdoc->createElement( 'sidebar' );

    $wdoc->appendChild( $w );
    
    foreach ( $sidebar as $myquote )

      {
     
      $x = $wdoc->createElement( "myquote" );
      $wid = $wdoc->createElement( "id" );
     
      $wid->appendChild(
            
         $wdoc->createTextNode( $myquote[ 'id' ] )
            
         );
   
      $x->appendChild( $wid );
    
      $wthequote = $wdoc->createElement( "thequote" );
     
      $wthequote->appendChild(
              
      $wdoc->createTextNode( $myquote[ 'thequote' ] )
             
         );
      
         $x->appendChild( $wthequote );
         $wreference = $wdoc->createElement( "reference" );
      
         $wreference->appendChild(
               
         $wdoc->createTextNode( $myquote[ 'reference' ] )
              
         );
       
        $x->appendChild( $wreference );
           
       $w->appendChild( $x );
    }
    $wdoc->save("../sidebar.xml");
    Last edited by djr33; 04-17-2010 at 06:23 PM. Reason: use [code] tags

  2. #2
    Join Date
    Mar 2008
    Posts
    122
    Thanks
    17
    Thanked 5 Times in 5 Posts

    Default

    Um, to read XML you can use this function: http://www.php.net/simplexml_load_file

    Very easy to use. Need any more help, let me know

  3. #3
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    I'm reading and writing just fine...got the code here.

    My problem is taking what I've read and adding to it before writing. My code is writing the new info but discarding the old, what it just read.
    Last edited by I am Abby; 04-18-2010 at 12:23 AM.

  4. #4
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    I am not a wizz at XML, and don't ever claim to be. but this line makes me wonder:

    Code:
    $wdoc = new DOMDocument();
    If you are creating a new document every time, then technically there is nothing to append too. and since you are naming it the same:

    Code:
    $wdoc->save("../sidebar.xml");
    The old one gets overwritten.

    In the PHP fopen() there is an append code "+a" that opens reads writes at the end, preserving the old data. Perhaps XML has a similar function? Again, just my 2 cents. I will leave now.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  5. #5
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    I don't believe my problem is in an append. If that were the case the second part would write...I'm only getting the first part to write to the xml.

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I looked at DOMdocument functions a while ago, and they just don't seem to be organized intuitively. I far prefer simpleXML.

    It looks to me like you're reading the xml into one object ($idoc), preparing the new data to be submitted, and then assigning it to a different object ($wdoc) to save it. So, I think Blizzard was right that you're overwriting the contents of the old xml file with only the new contents.

  7. #7
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    even if saving idoc to wdoc before writing is wrong...

    I don't see why the first thing I put in idoc is written but the second thing is not.

    the info I get from the user is saved to the .xml
    $sidebar = array();

    $sidebar[] = array(

    'id' => $insertcount,

    'thequote' => $insertquote,

    'reference' => $insertref

    );

    however the part I get from the .xml is not saved
    //Read sidebar.xml and add to array

    $isidebar = $idoc->getElementsByTagName( "myquote" );

    foreach( $isidebar as $imyquote )

    {

    $iids = $imyquote->getElementsByTagName( "id" );

    $iid = $iids->iten(0) ->nodeValue;

    $ithequotes = $imyquote->getElementsByTagName( "thequote" );

    $ithequote = $ithequotes->item(0) ->nodeValue;

    $ireferences = $imyquote->getElementsByTagName( "reference" );

    $ireference = $ireferences->item(0) ->nodeValue;

    $sidebar[] = array(

    'id' => $iid,

    'thequote' => $ithequote,

    'reference' => $ireference
 );
    }

    Because I put this stuff in the array last...if there was any problem at all...would it not have been the part that would save to the .xml?

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by I am Abby View Post
    //Read sidebar.xml and add to array

    $isidebar = $idoc->getElementsByTagName( "myquote" );

    foreach( $isidebar as $imyquote )

    {

    $iids = $imyquote->getElementsByTagName( "id" );

    $iid = $iids->iten(0) ->nodeValue;

    $ithequotes = $imyquote->getElementsByTagName( "thequote" );

    $ithequote = $ithequotes->item(0) ->nodeValue;

    $ireferences = $imyquote->getElementsByTagName( "reference" );

    $ireference = $ireferences->item(0) ->nodeValue;

    $sidebar[] = array(

    'id' => $iid,

    'thequote' => $ithequote,

    'reference' => $ireference
 );
    }
    that's the part of your code that I'm not following at all -as I said, I don't like/use DOMdocument- so I couldn't say. It doesn't seem you would need to use all those variables to re-read/assign everything (in simpleXML, the xml is simply read into a multi-dimensional array, so it's ready to work with right away), but I wouldn't know for sure with DOMdocument.

  9. #9
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Quote Originally Posted by traq View Post
    that's the part of your code that I'm not following at all -as I said, I don't like/use DOMdocument- so I couldn't say. It doesn't seem you would need to use all those variables to re-read/assign everything (in simpleXML, the xml is simply read into a multi-dimensional array, so it's ready to work with right away), but I wouldn't know for sure with DOMdocument.
    This is what Codeexploiter showed us in the thread http://www.dynamicdrive.com/forums/s...ad.php?t=14165
    Reading and Writing XML

    -----------------------------------------------------------

    Ok so how would you read a xml...

    <sidebar>
    <myquote>
    <id>1111</id>
    <thequote>Hello World</thequote>

    <reference>Abby Lee</reference>
    </myquote>

    </sidebar>


    with simplexml and add info from your form...

    $insertcount = $_POST["newcount"];

    $insertquote = $_POST["iquote"];

    $insertref = $_POST["iref"];

    Maybe something like:

    $idoc = simplexml_load_file('sidebar.xml');
    // bring in elements from form to start building array
    $sidebar = array();
    $sidebar[] = array(
    'id' => $insertcount,
    'thequote' => $insertquote,
    'reference' => $insertref
    );
    //run through .xml and add to array
    foreach($idoc->children() as $myquotes)
    {
    foreach($myquotes->children() as $myquote)
    {
    $myquote
    $sidebar = array(
    //ok this is where I get lost...I need to put each of the elements of the array
    Last edited by I am Abby; 04-20-2010 at 03:49 PM.

  10. #10
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Did I loose everyone? Please help me.

    I'm trying to go another way....

    $idoc = simplexml_load_file('sidebar.xml');
    foreach($idoc->children() as $myquotes)
    {
    foreach($myquotes->children() as $child)
    {
    foreach($child->children() as $kid)
    {
    echo "Name:".$kid->getName()."\n";
    echo "Value:".$kid."\n";
    echo "Attributes:".$kid->attributes()."\n\n";
    }
    }
    }

    I get nothing.
    Now if I do an echo on $myquote in the first foreach I get "id", "thequote", and "reference" over and over.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •