Re: NSXML Example Help
Re: NSXML Example Help
- Subject: Re: NSXML Example Help
- From: ChrisB <email@hidden>
- Date: Thu, 6 Apr 2006 13:26:52 -0700
Thanks for the response Robert. It prompted me to look differently at
how I was dealing with the errors.
I have made some good progress, but I am struggling now to figure out
how to get the value from between xml tags in a child node.
Bellow the function is the sample xml file I am learning with. I have
figured out how to access the child objects with the index, but can't
figure out how to get the Name, or the Price out of it. It looks like
XPath or XQuery are the way to go here from the docs, but I can't
figure out how to make it fly.
My function:
- (IBAction)createXMLDocument:(id)sender
{
NSXMLDocument *xmlDoc;
NSError *err=nil;
NSURL *furl = [NSURL URLWithString:[NSString
stringWithFormat:@"file://localhost/Users/me/Desktop/simple.xml"]];
if (!furl) {
NSLog(@"Can't create an URL from path");
return;
}
xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl
options:(NSXMLNodePreserveWhitespace|
NSXMLNodePreserveCDATA)
error:&err];
if (xmlDoc == nil) {
xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl
options:NSXMLDocumentTidyXML
error:&err];
}
if (xmlDoc == nil) {
if (err) {
[self handleError:err];
}
return;
}
if (err) {
[self handleError:err];
}
int i, count = [[xmlDoc rootElement] childCount];
for (i=0; i < count; i++) {
NSXMLNode *child = [[xmlDoc rootElement] childAtIndex:i];
NSLog(@"%i - %@\n",i,[child stringValue]);
// WOULD LIKE TO SEE HOW TO JUST DISPLAY THE NAME DESCRIPTION
FROM THE XML TREE
}
}
The XML File:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v2006 (http://www.altova.com) -->
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>two of our famous Belgian Waffles with plenty of real
maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>light Belgian waffles covered with strawberries and
whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>light Belgian waffles covered with an assortment of
fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>thick slices made from our homemade sourdough bread</
description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>two eggs, bacon or sausage, toast, and our ever-
popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
On Apr 6, 2006, at 9:04 AM, Robert Walker wrote:
I am working on a small project that needs to read a very simple
XML / RSS file with a few simple items. I need to be able to step
through them, and get the information out of the tags like the
title description, etc.. I just can't find a simple example showing
how to do this part. One section in the docs about Creating a
Document Object From Existing XML is not working for me, so
Traversing an XML Tree with nodes is not working either.
Can you give us a hint (maybe an error message) as to what you mean
by "not working?" I'm using NSXMLDocument to read XML from my web
services. I am using the NSXML classes to serialize/deserialize my
custom objects within my SOAP calls.
After creating the NSXMLDocument object you should be able to use
[myXML nextNode] and [myXML nextSibling] to traverse your document
and extract any data you want.
Are you sure you have a well formed XML document to begin with? If
you do then NSXMLDocument should be able to read it.
Here is a code snippet show how I'm reading my XML data:
...
...
...
// Call web service to authenticate user
id resultValue = [ProcessorService authenticateUser:[self username]
in_password:[self password]];
if (resultValue != nil) {
// Create an NSXMLDocument
xmlDoc = [[[NSXMLDocument alloc] initWithXMLString:resultValue
options:NSXMLDocumentTidyXML error:&err] autorelease];
// Get the root element
rootElement = [xmlDoc rootElement];
// Start processing from the first child node
objectNode = (NSXMLElement *)[rootElement nextNode];
...
...
...
}
...
...
...
On Apr 6, 2006, at 12:43 AM, ChrisB wrote:
I am very new to Cocoa / Obj-C development, but have been making
good headway with Apple's documentation and a couple of books I
have been working through. I am currently struggling with
understanding the NSXML. I have worked through the "Tree-Based XML
Programming Guide for Cocoa," and looked at the XMLBrowser example
project. My problem is I am just not getting it from the example
project. I need to find an example that is a little less involved
and build on that, rather than start with such an evolved sample.
I am working on a small project that needs to read a very simple
XML / RSS file with a few simple items. I need to be able to step
through them, and get the information out of the tags like the
title description, etc.. I just can't find a simple example showing
how to do this part. One section in the docs about Creating a
Document Object From Existing XML is not working for me, so
Traversing an XML Tree with nodes is not working either.
It really is amazing how well these dev tools work, and how fun it
is to write apps with OS X. Coming from only a VB, perl, and some
PHP, I have made a lot of headway in such a short time.
Thanks for the input.
--
Robert Walker
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden