akomabantot — 2010-03-03T02:26:40-05:00 — #1
- get xml data from an api - done
- update xml data - done
- return the xml data to api - error
i've been updating xml data using simplexml.
here's my xml data
here is the sample code for xml update
$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
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..
cups — 2010-03-03T04:05:26-05:00 — #2
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.
akomabantot — 2010-03-04T09:31:12-05:00 — #3
this is the actual php code i am running but without the data update.
$xmlData = "<data>
$xml = simplexml_load_string($xmlData, NULL, LIBXML_NOEMPTYTAG);
echo $xml = $xml->saveXML();
and the return is
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..
scallioxtx — 2010-03-04T09:53:04-05:00 — #4
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 ...
anthonysterling — 2010-03-04T10:13:24-05:00 — #5
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?
Remove blank nodes
salathe — 2010-03-04T13:59:38-05:00 — #6
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.
$xml = '<data>
$sxml = new SimpleXMLElement($xml);
$sxml->address = 'new address';
$sxml->mobile = 'new mobile';
$sxml->landline = ''; // Magic happens
anthonysterling — 2010-03-04T14:03:52-05:00 — #7
Ah, thanks Salathe. :tup:
However, the manual does state LIBXML_NOBLANKS "Removes blank nodes", awfully confusing as it obviously doesn't.
salathe — 2010-03-04T14:43:55-05:00 — #8
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.
akomabantot — 2010-03-04T23:30:06-05:00 — #9
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.
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.
mittineague — 2010-03-05T02:05:34-05:00 — #10
It might be easier to use something other than simplexml.
salathe — 2010-03-05T08:36:07-05:00 — #11
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.