PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 29th, 2012, 10:04 AM
tomthebomb231 tomthebomb231 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 tomthebomb231 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m 4 sec
Reputation Power: 0
PHP5 - 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

Reply With Quote
  #2  
Old November 29th, 2012, 10:15 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,872 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 1 h 24 m 30 sec
Reputation Power: 813
Hi,

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

Quote:
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).

Reply With Quote
  #3  
Old November 29th, 2012, 11:11 AM
tomthebomb231 tomthebomb231 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 tomthebomb231 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m 4 sec
Reputation 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?

Reply With Quote
  #4  
Old November 29th, 2012, 12:38 PM
msteudel's Avatar
msteudel msteudel is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 712 msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 4 Days 11 h 4 m 59 sec
Reputation Power: 11
This was from the comments where they had problem with formatOutput not working

Quote:
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

Reply With Quote
  #5  
Old November 29th, 2012, 12:46 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,872 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 1 h 24 m 30 sec
Reputation Power: 813
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 ...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP5 - Getting xml data to look right, using php

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap