Log in

View Full Version : PHP/simplexml only writes one tag if no data



I am Abby
05-05-2010, 06:06 PM
When the php writes back to the xml...if any of the fields (already existing or ones your php creates) have no data (I have some quotes that do not have references), instead of writing

<reference></reference>
the php/simplexml writes
</reference>

I use a Spry Data Set to place the sidebar on my .html pages but it's very picky about opening and closing tags.

Is there an easy way to tell the code to add both tags?


$dcount = $_POST["dcount"]'

$file = '../sidebar.xml';
$xml = simplexml_load_file($file);

$i = 0;
foreach($xml as $kid){
if(in_array($kid->id, $dcount)){
unset($xml->myquotes[$i]);
break;
}
$i++;
}
$xml = $xml->asXML($file);

or is that something you have to put up with when using simplexml?

my xml looks like...

<sidebar>
<myquotes>
<id></id>
<thequote></thequote>
<reference></thereference>
</myquote>
</sidebar>

traq
05-06-2010, 01:48 AM
Just off the top of my head, couldn't you add some default content (like &nbsp; )? Kinda avoiding the issue, but sometimes that's easier.

I am Abby
05-06-2010, 12:50 PM
I think it may be better to just do the job with simplexml. Spry Data Sets have other issues which made me do things a bit different than I normally would have.

traq
05-06-2010, 03:26 PM
Some testing: are you sure it's writing </reference> and not <reference/> ? I tried it out, and simpleXML seems to write self-closing tags when there's no content (likewise, Firefox -even though it doesn't rewrite the xml- only displays a self-closing tag when viewing xml with no content). You can still add content to them normally using simpleXML.

I haven't found any documentation pointing towards a configuration option to prevent it, so if you need the opening & closing tags for some other application, I think you'd have to add placeholder content.

I am Abby
05-06-2010, 06:36 PM
You are right...it's writing <reference/>
Either way Spry does not like it. That's ok, I'm using what I learned here to put my sidbar on the pages.