problem reading xml with NSArray

Discussion in 'iOS Programming' started by zonger, Oct 20, 2010.

  1. zonger macrumors newbie

    Joined:
    Oct 20, 2010
    #1
    Hi
    I have created an XML with php and uploaded on a free server.
    I would like to read it with NSArray and out put the array list, but the NSArray is not getting anything. please help~~~
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    We can't debug descriptions. Please provide the associated code.
     
  3. zonger thread starter macrumors newbie

    Joined:
    Oct 20, 2010
    #3
    Code:
    #import "WebViewController.h"
    
    @implementation WebViewController
    
    @synthesize webView, output_message;
    
    - (id)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)nibBundleOrNil {
            if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
                    // Initialization code
            }
            return self;
    }
    
    
    /*
     If you need to do additional setup after loading the view, override viewDidLoad. */
    - (void)viewDidLoad
    {
            
            NSString *urlAddress = @"http://bizbook.x10.mx/dictionary.php";
            NSString *message;
            int number;
            
            //Create a URL object.
            NSURL *url = [NSURL URLWithString:urlAddress];
            
            //URL Requst Object
            NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
            
            //Load the request in the UIWebView.
            [webView loadRequest:requestObj];
            
            // Load into an NSDictionary if the highest level is a <dict>
            NSDictionary *myData = [ [ NSDictionary alloc ] initWithContentsOfURL: @"http://bizbook.x10.mx/dictionary.php" ];
            
            // Load into an NSArray if the highest level is an <array>
            //NSMutableArray *myArray = [ [ NSMutableArray alloc ] initWithContentsOfURL: @"http://bizbook.x10.mx/print.php" ];
    
            message = [myData objectForKey: @"Fruit"];
            output_message.text = message;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation
    {
            // Return YES for supported orientations
            return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    
    - (void)didReceiveMemoryWarning
    {
            [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
            // Release anything that's not essential, such as cached data
    }
    
    - (void)dealloc {
            [webView release];
            [output_message release];
            [super dealloc];
    }
    
    
    @end
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Does your PHP return a plist?

    From the documentation for initWithContentsOfURL:
     
  5. zonger thread starter macrumors newbie

    Joined:
    Oct 20, 2010
    #5
    should be cuz I view the source code its returns plist. And I switch to NSDictionary insead of NSArray
    Here is the the php code

    <?php
    // Print the opening of the plist, (Don't forget to escape quotes if needed!)
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist SYSTEM \"file://localhost/System/Library/DTDs/PropertyList.dtd\">\n<plist version=\"1.0\">\n";

    // The data that needs to be transferred:
    $my_data = array();
    $my_data["Fruit"] = array("Apple", "Orange", "Banana", "Pumpkin?");
    $my_data["Soda"] = array("Dr. Pepper", "Pepsi", "Orange Soda");
    $my_data["Cups"] = array("Large", "Medium", "Small");

    // Open the dictionary

    echo "<dict>\n";

    // Putting the data into the plist format
    foreach ($my_data as $key => $array) { // Loop for every value in $my_data
    echo " <key>$key</key>\n"; // Make a key with the key from $my_data and open an array for it's values.
    foreach ($array as $value) { // Go through every value in the current array from $my_data
    echo " <string>$value</string>\n"; // Print the value as a string in the array
    }
    //echo " </array>\n"; // Close the array
    }

    echo
    "</dict>\n</plist>\n"; // Close the dictionary & plist

    ?>
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Time for basic debugging: is myData == nil after the call to initWithContentsOfURL: ?
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Below is the actual data from your php URL:
    http://bizbook.x10.mx/dictionary.php

    I simply read the URL in your code, fetched it in Safari, then chose View Source:
    Code:
     <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
    <plist version="1.0">
    <dict>
        <key>Fruit</key>
            <string>Apple</string>
            <string>Orange</string>
            <string>Banana</string>
            <string>Pumpkin?</string>
        <key>Soda</key>
            <string>Dr. Pepper</string>
            <string>Pepsi</string>
            <string>Orange Soda</string>
        <key>Cups</key>
            <string>Large</string>
            <string>Medium</string>
            <string>Small</string>
    </dict>
    </plist>
    
    Two things:

    1. This data isn't an array.
    So when you said you had a problem reading NSArray, you were wrong. Your code and your data are for NSDictionary.

    2. This data is malformed for a dictionary.
    Every <key> element must be followed by a SINGLE value element, where the value can be any valid element type: string, number, array, dict.
     
  8. zonger thread starter macrumors newbie

    Joined:
    Oct 20, 2010
    #8
    yes this is for NSDictionary. I changed it but did not change the topic title.
    As for the plist, are you saying that I can only have one value in the list?
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Yes, but remember that an array could be considered that one value.
     
  10. zonger thread starter macrumors newbie

    Joined:
    Oct 20, 2010
    #10
    i have inserted values as string
    does that count as single value??
    or i should put them in an array like
    <array>
    <string>value</string>
    <string>llllll</string>
    </array>
     

Share This Page