Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 03-16-2011, 11:56 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 19
JJSaccolo is on a distinguished road
Default using TBXML and parse an attribute that there isn't all the time

I'm parsing an xml file using TBXML. my xml is like this:

Code:
<locations>
   <location>
      <id>1</id>
      <name>hello</name>
   </location>
   <location>
      ...
</locations>
all works fine, but there is a big problem: sometimes the xml can "skip" the "name" tag, so, for example, there is something like this:

Code:
...
</location>
<location>
   <id>43</id>
</location>
<location>
...
where is the problem? that using this code

Code:
TBXMLElement *location = [TBXML childElementNamed:@"location" parentElement:root];
while (location){
   TBXMLElement *id = [TBXML childElementNamed:@"id" parentElement:location];
   TBXMLElement *name = [TBXML childElementNamed:@"name" parentElement:location];
   ... //do something
   location = location -> nextSibling;
}
the app crashes reading the tag "name" because sometimes there isn't...

How can i solve it??

thanks!
JJSaccolo is offline   Reply With Quote
Old 04-06-2011, 12:49 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 26
javadude is on a distinguished road
Default

Quote:
Originally Posted by JJSaccolo View Post
I'm parsing an xml file using TBXML. my xml is like this:

Code:
<locations>
   <location>
      <id>1</id>
      <name>hello</name>
   </location>
   <location>
      ...
</locations>
all works fine, but there is a big problem: sometimes the xml can "skip" the "name" tag, so, for example, there is something like this:

Code:
...
</location>
<location>
   <id>43</id>
</location>
<location>
...
where is the problem? that using this code

Code:
TBXMLElement *location = [TBXML childElementNamed:@"location" parentElement:root];
while (location){
   TBXMLElement *id = [TBXML childElementNamed:@"id" parentElement:location];
   TBXMLElement *name = [TBXML childElementNamed:@"name" parentElement:location];
   ... //do something
   location = location -> nextSibling;
}
the app crashes reading the tag "name" because sometimes there isn't...

How can i solve it??

thanks!
You can just check if your name object is nil (tested this myself, definitely doesn't crash) before using it. Also, "name" is an XML element and not an attribute. An attribute would be the lang in the following example: <name lang="EN">.
javadude is offline   Reply With Quote
Reply

Bookmarks

Tags
parse, parse xml, tbxml, xml

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 404
18 members and 386 guests
Brandt, coolman, Domele, Droverson, Duncan C, fredidf, Free App Monster, givensur, iAppDeveloper, locombiano89, Mah6447, MasterX, Meoz, simplymuzik3, SLIC, stanny, stevenkik, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,646
Threads: 94,111
Posts: 402,864
Top Poster: BrianSlick (7,990)
Welcome to our newest member, locombiano89
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:56 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0