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>
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.
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 …
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.
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’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.