Xml no empty tags

hi,

  1. get xml data from an api - done
  2. update xml data - done
  3. return the xml data to api - error

i’ve been updating xml data using simplexml.
here’s my xml data


<data>
     <fname>john</fname>
     <lname>reyes</lname>
     <address>manila</address>
     <landline></landline>
     <mobile>09171234567</mobile>
</data>

here is the sample code for xml update


header("content-type:application/xml");
$xml = simplexml_load_file('data.xml', NULL, LIBXML_NOEMPTYTAG);
//updating the address and mobilenumber
//landline stays the same
echo $xml = $xml->saveXML();

now i have to return the xml data to the api but the api return a dtd validation because my php script to update xml data returns the tag landline as <landline/> not the original pair. i know this is a valid xml data but the api wants this kind of tag for empty tags -> <landline></landline>

here is what the saveXML() method returned


<data>
     <fname>john</fname>
     <lname>reyes</lname>
     <address>baguio</address>
     <landline/>
     <mobile>09208651384</mobile>
</data>

i thought LIBXML_NOEMPTYTAG would be a help but i didn’t.
what i need now is a script to convert single empty tag (<landline/>) to empty pair tag (<landline></landline>)

sorry for the xml technical terms, i hope you can help and understand me with my example. thanks…

Before attempting this, are you sure that returning <landline></landline> with some altered data actually works also? I ask because while you imply it, you don’t state this is a fact.

this is the actual php code i am running but without the data update.


<?php
header("content-type:application/xml");
$xmlData = "<data>
     <fname>john</fname>
     <lname>reyes</lname>
     <address>manila</address>
     <landline></landline>
     <mobile>09171234567</mobile>
</data>";
$xml = simplexml_load_string($xmlData, NULL, LIBXML_NOEMPTYTAG);
echo $xml = $xml->saveXML();

and the return is


<?xml version="1.0"?>
<data>
     <fname>john</fname>
     <lname>reyes</lname>
     <address>manila</address>
     <landline/>
     <mobile>09171234567</mobile>
</data>

asXML()/saveXML() i think has a bug or lacks in option for this empty tags as submitted here, http://bugs.php.net/bug.php?id=32307&edit=1
now i want to ask for some work around on this problem. thanks…

You already said that.
Could you answer the question from Cups?
You might think you can solve the problem by doing this, while in actuality the problem lies somewhere else …

The manual states…

Note: This option is currently just available in the DOMDocument::save and DOMDocument::saveXML functions.

However, it actually seems like you’re after the LIBXML_NOBLANKS flag?

LIBXML_NOBLANKS (integer)
Remove blank nodes

Not at all, that is a different flag with results not particularly relevant to this topic (think along the lines of preserveWhiteSpace = FALSE [default is TRUE] with formatOutput = FALSE [default is FALSE] for DOM).

Instead, just “change” the landline’s value to an empty string or NULL.


<?php

$xml = '<data>
     <fname>john</fname>
     <lname>reyes</lname>
     <address>baguio</address>
     <landline/>
     <mobile>09208651384</mobile>
</data>';

$sxml = new SimpleXMLElement($xml);
$sxml->address = 'new address';
$sxml->mobile = 'new mobile';
$sxml->landline = ''; // Magic happens

header('Content-Type: application/xml');
echo $sxml->asXML();

/*
<?xml version="1.0"?>
<data>
     <fname>john</fname>
     <lname>reyes</lname>
     <address>new address</address>
     <landline></landline>
     <mobile>new mobile</mobile>
</data>
*/

Ah, thanks Salathe. :tup:

However, the manual does state LIBXML_NOBLANKS “Removes blank nodes”, awfully confusing as it obviously doesn’t.

Well it does, just not in the way that you were thinking. It refers to removing ‘blank’ nodes as in whitespace-only nodes between others (like you get with nicely formatted XML) rather thank ‘blank’ nodes as in named nodes with no content (like <sarcasm/>).

I’ve had a play around and the ‘empty string’ workaround appears to work even for older versions of PHP (by that, I mean I tested it on one random older version: PHP 5.1.3) so hopefully it’ll be fine for the OP. Do let us know if it is not suitable.

Yes the API would accept anything with complete pair of tags (<landline>2927936</landline>), with or without data. but returns a dtd validation for single closing tags (<landline/>). any other tags in the xml is also validated not only the landline tag.

for Salathe:
i appreciate your suggestion but the xml i have shown you is just the tip of the iceberg. the real xml file contains up to 5 child nodes and more than 50 tags. it would be very tedious to check all tags if they are empty and update with NULL content just to make sure it would return a pair of tag on empty data. but if there is no easier solution i might do it instead.

It might be easier to use something other than simplexml.

It’s not really particularly tedious to loop over all empty nodes and add no content (that sounds weird out of context!) which would be a couple of lines. The worst part is having to do something that really should be unnecessary. On that note, how painful would it be to ask the XML provider to relax their validation? Perhaps that might be an ‘easier’ (and certainly more practical for everyone else using the XML) path to take.

Also, going along Mittineague’s route: DOMDocument::saveXML allows you to pass the LIBXML_NOEMPTYTAG flag.