2

As the google doc is not available anymore, I'm lost with those concepts.

What is a node, and what is an element (that inherits the node) ?

How can I switch from nodes to elements. I mean, for example, if I write :

NSError* error;
NSData* xmlData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ForTesting" ofType:@"xml"]];

error = nil;
GDataXMLDocument* XMLDoc = [[[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error] autorelease];
if (XMLDoc == nil) {
    NSLog(@"%@", error.description);
    return;
}

GDataXMLNode* xmlElement = [[XMLDoc nodesForXPath:@"//root/fileVersion" error:nil] objectAtIndex:0];
NSString* fileVersion = xmlElement.stringValue;

GDataXMLNode* xmlList = [[XMLDoc nodesForXPath:@"//root/list" error:nil] objectAtIndex:0];  // single item

After that code, how can I write something like that to switch to GDataXMLElement instead of continuing with GDataXMLNode, that would requires me to continue using XPath (I don't want to use it past that point) :

// code don't work : elementsForName is not defined for GDataXMLNode
for (GDataXMLElement* xmlObject in [xmlList elementsForName:@"object"]) {
    MyClass* obj = [[[MyClass alloc] initWithXMLElement:xmlObject] autorelease];
}
Oliver
  • 23,072
  • 33
  • 138
  • 230

1 Answers1

1

GDataXMLNode is obviously the classes you use for XML parser- GDataXMLNode.h/.m

In the code you have given returns an array. You can use.

NSArray *myArray = [XMLDoc nodesForXPath:@"//root/fileVersion" error:nil];

You can iterate myArray like this.

for (GDataXMLElement *nodeXmlElt in myArray)
{
    //some code
}

Each of my nodeXmlElt will be like given below.

<fileVersion>
<title>San Francisco CBS News</title>
<link>http://news.google.com/news/</link>
<fileVersion>

//getting title
NSArray *elementArray = [nodeXmlElt elementsForName:@"title"];
GDataXMLElement *gdataElement = (GDataXMLElement *)[elementArray objectAtIndex:0];
NSString *title = gdataElement.stringValue; //returns 'San Francisco CBS News'
ArunGJ
  • 2,685
  • 21
  • 27
  • OK, but how do you pass from an array of nodes to elements ? – Oliver Mar 13 '12 at 09:01
  • 1
    Yes, That's what I do, but looking at the header files, you'll see that nodesForXPath returns an array of GDataXMLNode, and GDataXMLElement is a subclass of GDataXMLNode, not a superclass. So this code is not correct even if it seems to work... What do you think about this ? – Oliver Mar 13 '12 at 23:39
  • @Oliver, the answer of ArunGJ, as you have said, is working so why is it not correct or valid? I will give the same solution because that is how I do it in my projects. And, the second code snippet is not working, as you have already figured out, because of how you worked with XPath. I have a solution for this. Check this out: http://stackoverflow.com/questions/9100040/parse-xml-with-namespaces-with-gdata-xml/9411766#9411766 If you find it helpful, please mark the solution up. – yoninja Mar 21 '12 at 05:03
  • 1
    @yoninja:it is not because it seems to work that it is valid. Copy/pasting code without figuring how it works is not a fine way of coding. If you look at the class hierarchy, working with a GDataXMLNode as it was a GDataXMLElement is not fine. You cannot use a vehicle as being a motorcycle. It can also be a car. Or a bus. Or a plane. Using the given code, you assume a vehicle to be a car. – Oliver Mar 21 '12 at 09:23
  • Yes, you "assume" that the elements of the result of [XMLDoc nodesForXPath:...] to be a GDataXMLElement because it is the logical choice (what other subclasses of GDataXMLNode are there?). And it happens to be true. If you follow the GDataXMLNode code, you will eventually discover that the type of the node that is being added is a XML_ELEMENT_NODE. Because of this at the method nodeBorrowingXMLNode of GDataXMLNode, you will see that GDataXMLElement objects are being put on the array. If you are not satisfied with this then you need to dissect at least these codes GDataXMLNode.m and xpath.c. – yoninja Mar 21 '12 at 12:30
  • You get a compiler warning if you try to use a GDataXMLNode as if it were a GDataXMLElement – user798719 Sep 04 '12 at 19:13