Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8679021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:52:05+00:00 2026-06-12T20:52:05+00:00

To start with Cocoa xml read write coding I have created a sample application,

  • 0

To start with Cocoa xml read write coding I have created a sample application, where I am able to write into XML file but getting some errors in reading. Below is the code for that.

NSXMLElement * root = [[NSXMLElement alloc ] initWithName:@"Book"]
NSXMLDocument * xmlDoc = [[NSXMLDocument alloc ] initWithRootElement: root ];
[root addChild:[NSXMLElement elementWithName: @"Name" stringValue:@"book name"]];
[root addChild:[NSXMLElement elementWithName:@"Author" stringValue:@"bool author"]];
 NSData * xmlData = [xmlDoc XMLDataWithOptions:NSXMLNodePrettyPrint];
[xmlData writeToFile:[@"~/Desktop/book.xml" stringByExpandingTildeInPath ]
 atomically:YES];
[xmlDoc release ];

This works fine and book.xml is created.
But when tried to read this file, I am facing issues in opening this file.
Here is the code for that.

NSString *strValue = [@"~/Desktop/book.xml" stringByExpandingTildeInPath];
NSURL * strUrl = [NSURL URLWithString:strValue];
NSError * err = nil;
NSXMLDocument  * xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:strUrl    
options:NSXMLDocumentTidyXML error:&err];
NSXMLNode * rootNode = [xmlDoc rootElement ];
[xmlDoc release ];

value of xmlDoc is nil.

Your answers will be highly appreciated.

Thanks,
Tausif.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:52:07+00:00Added an answer on June 12, 2026 at 8:52 pm

    You’re missing file:// in strValue. See NSURL class documentation … If you don’t want to mess with prepending file:// manually, you should use fileURLWithPath:isDirectory:.

    Why all this?

    Simplified explanation for your case – every URL does consist of scheme/protocol (http, file, https, …), delimiter (://) and path (~/Desktop/…). And it’s mandatory, not optional. When you do use URLWithString: it expects that your string is complete URL, not just part of it (missing scheme/protocol in your case).

    So, if you do want to use URL instead of file path, you have to take care of file:// (when URLWithString: is used) or do use fileURLWithPath:isDirectory: where you can pass just file path.

    You do this, but just warning for others, if your path does contain tilde (~) you must expand it first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that can import an XML file through this terminal command
I have a document-based Cocoa application that has to start up a sub-process before
I'm learning how to build programs with Cocoa. I'm using a sample Apple application
I have never made an app using XCode and Cocoa but I think following
I have the following code to start my own Cocoa HTTP Server . In
In a Mac OS X Cocoa application, I have an application-modal dialog with text
I am working on an application (Cocoa/Objective-c) where I have the blueprint of each
I'm building an application in Cocoa and I want to be able to let
I'd like to start experimenting with Cocoa, Objective-C and Xcode, but the only Apple
I have some experience of Objective-C/Cocoa programming on Mac and I'd like to start

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.