Closed GoogleCodeExporter closed 9 years ago
Use this code below to test it out. It can be put in the first viewcontroller
of the iPhone app.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
GDataXMLElement *elm = [GDataXMLNode elementWithName:@"Message"];
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithRootElement:elm];
[elm addAttribute:[GDataXMLNode attributeWithName:@"Version" stringValue:@"1.0"]];
NSLog(@"XML is:%@", [[[NSString alloc] initWithData:[doc XMLData] encoding:NSUTF8StringEncoding] autorelease] );
[doc release];
}
Original comment by sureshscribnar
on 3 Nov 2010 at 11:29
The output is XML is:<?xml version="1.0"?>
<Message/>
But the expected is including the added attribute
XML is:<?xml version="1.0"?>
<Message Version="1.0"/>
Original comment by sureshscribnar
on 3 Nov 2010 at 11:30
Alright, got it resolved. The issue is that GDataXMLNode.m internally copies
xmlNode struct pointed by the passed in element/object to addChild and
initWithRootElement methods. This seems to have been done to keep the
namespaces same as parent node's namespace when copying new child nodes to a
parent node; based on the implementation of GDataXMLNode.m file. That means,
We can set the reference of the passed object's variable to new node by
querying the doc/element to which the node was added as child. This way, the
attributes we add to an element after adding that element to parent element are
added to the copy struct object.
Below is the method given in comment 1 with one new line of code that re-refers
to an element object that refers to copied struct of xmlNode. This fixes the
issue.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
GDataXMLElement *elm = [GDataXMLNode elementWithName:@"Message"];
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithRootElement:elm];
elm = [doc rootElement]; //newline 1
[elm addAttribute:[GDataXMLNode attributeWithName:@"Version" stringValue:@"1.0"]];
NSLog(@"XML is:%@", [[[NSString alloc] initWithData:[doc XMLData] encoding:NSUTF8StringEncoding] autorelease] );
[doc release];
}
Conclusion: So, its not completely object oriented code in GDataXMLNode as it
uses struct internally to hold sub objects. Its how its supposed to work anyway
as libxml2 is used; there is no pure object oriented way when using libxml2 as
GDataXMLNode is not independent xml library. So, we need to query the doc
object to get object reference of nodes to which we want to add properties or
child nodes before adding the child nodes.
With this understanding, its not an issue but a design choice and lack of
documentation for each method. So, it would be good if these details are added
as part of GDataXMLNode documentation for each method in that file. Lack of
documentation means many developers may have to rediscover the same things over
and over again.
Note: I'm not sure if I can add this documentation in google code; if possible,
I will in few days. Thanks for this wonderful library by the way. Cheers.
Original comment by sureshscribnar
on 3 Nov 2010 at 12:30
Thanks for the suggestion. I have added comments to the methods that copy their
arguments.
http://code.google.com/p/gdata-objectivec-client/source/detail?r=596
Original comment by gregrobbins
on 9 Nov 2010 at 10:35
Original issue reported on code.google.com by
sureshscribnar
on 2 Nov 2010 at 8:25