#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Getting xml data to look right, using php


    Hi, so I have an html form which adds data to an already existing xml file.

    Here's the form


    Code:
    <form name="backupform" action="backup.php" method="post" onSubmit="document.location.reload(true)">
    					<label for="media_cartridge_label">Cartridge Label:</label><input type="text" id="media_cartridge_label" name="media_cartridge_label"/><br/>
    					<label for="backup_tapeof">Backup Tape:</label><input type="text" name="backup_tapeof" id="backup_tapeof"/><br/>
    					<label for="backup_date">Date:</label><input type="text" name="backup_date" id="backup_date"/><br/>
    					<label for="backup_type">Type:</label><input type="text" name="backup_type" id="backup_type"/><br/>
    					<label for="backup_description">Description:</label><input type="text" id="backup_description" name="backup_description"/>
    					<input type="submit" value="Submit"/>	
    				</form>

    Here's the php data:

    PHP Code:
    <?php
    $doc 
    = new DOMDocument();
    $doc->load'Backup Info.xml' );
      
    $backup_inventory $doc->getElementsByTagName"backup" );
    foreach( 
    $backup_inventory as $backup )
    {
      
    $media_cartridge_labels $backup->getElementsByTagName"media_cartridge_label" );
      
    $media_cartridge_label $media_cartridge_labels->item(0)->nodeValue;
      
      
    $backup_tapeofs$backup->getElementsByTagName"backup_tapeof" );
      
    $backup_tapeof$backup_tapeofs->item(0)->nodeValue;
      
      
    $backup_dates $backup->getElementsByTagName"backup_date" );
      
    $backup_date $backup_dates->item(0)->nodeValue;
      
      
    $backup_types $backup->getElementsByTagName"backup_type" );
      
    $backup_type $backup_types->item(0)->nodeValue;
      
      
    $backup_descriptions $backup->getElementsByTagName"backup_description" );
      
    $backup_description $backup_descriptions->item(0)->nodeValue;
      
      
      }
    ?>
    Here's the backup.php file:

    PHP Code:
    <?php

    $backup 
    = array(
        
    'media_cartridge_label' => $_POST['media_cartridge_label'],
        
    'backup_tapeof' => $_POST['backup_tapeof'],
        
    'backup_date' => $_POST['backup_date'],
        
    'backup_type' => $_POST['backup_type'],
        
    'backup_description' => $_POST['backup_description'],
    );
        
    $doc = new DOMDocument();
    $doc->load'Backup Info.xml' );

    $doc->formatOutput true;
    $r $doc->getElementsByTagName("backup_inventory")->item(0);

    $b $doc->createElement("backup");

    $media_cartridge_label $doc->createElement("media_cartridge_label");
    $media_cartridge_label->appendChild(
        
    $doc->createTextNode$backup["media_cartridge_label"] )
    );
    $b->appendChild$media_cartridge_label );

    $backup_tapeof $doc->createElement("backup_tapeof");
    $backup_tapeof->appendChild(
        
    $doc->createTextNode$backup["backup_tapeof"] )
    );
    $b->appendChild$backup_tapeof );

    $backup_date $doc->createElement("backup_date");
    $backup_date->appendChild(
        
    $doc->createTextNode$backup["backup_date"] )
    );
    $b->appendChild$backup_date );

    $backup_type $doc->createElement("backup_type");
    $backup_type->appendChild(
        
    $doc->createTextNode$backup["backup_type"] )
    );
    $b->appendChild$backup_type );

    $backup_description $doc->createElement("backup_description");
    $backup_description->appendChild(
        
    $doc->createTextNode$backup["backup_description"] )
    );

    $b->appendChild$backup_description );
    $r->appendChild$b );
        
    $doc->save("Backup Info.xml");  
    ?>

    Here is what the Backup Info.xml is supposed to look like:

    Code:
    <backup>
    	<media_cartridge_label></media_cartridge_label>
    	<backup_tapeof></backup_tapeof>
    	<backup_date></backup_date>
    	<backup_type></backup_type>
    	<backup_description></backup_description>
    </backup>
    everything works as far as the right data getting stored, but unfortunately the xml file looks like this:

    Code:
    <backup><media_cartridge_label></media_cartridge_label><backup_tapeof></backup_tapeof><backup_date></backup_date><backup_type></backup_type><backup_description></backup_description></backup>
    with all the tags being on the same line. This is not ideal because if data ever needs to be deleted or changed it will be difficult to find. Is there any solution to this? I have found threads with people asking this same question, but never any answers : (

    Thanks
    -Tom
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    a 2 minute Google search revealed this comment in the documentation:

    fcartegnie 31-Oct-2009 06:30

    Be careful with formatOutput().

    Creating an empty node like this:
    createElement('foo','')
    instead of
    createElement('foo')
    will break formatOutput.
    http://php.net/manual/en/class.domdocument.php

    And that's exactly what you do.

    If it still doesn't work, I'd simply try a different library (simpleXML or whatever).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    Maybe I am not understanding what you are suggesting correctly, but it doesn't matter if there is data entered in the form or if it is left blank, the formatting still comes out wrong.

    I am not really a coder or programmer or whatever. It took me a few weeks just to figure out how to get this code to work, so I would rather not start over from the beginning when I am so close. Also I already have 7 different forms and php data written this way. I have seen comments on indenting xml using simpleXML. Are there any solutions using DOM?
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    12
    This was from the comments where they had problem with formatOutput not working

    For anyone else who has been having issues with formatOuput not working, here is a work-around:

    rather than just doing something like:

    <?php
    $outXML = $xml->saveXML();
    ?>

    force it to reload the XML from scratch, then it will format correctly:

    <?php
    $outXML = $xml->saveXML();
    $xml = new DOMDocument();
    $xml->preserveWhiteSpace = false;
    $xml->formatOutput = true;
    $xml->loadXML($outXML);
    $outXML = $xml->
    You could also try and run it through a XML beautifier after you've generated the xml. I think there's a pear library XML_Beautifier, haven't used it so not sure if it does what you are looking for, though it sounds like it
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Could it be that the formatting stuff of DOMDocument is pretty messed up?

    The deeper you dig, the more exceptions and bugs and workarounds you find ...

IMN logo majestic logo threadwatch logo seochat tools logo