Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zonger

macrumors newbie
Original poster
Oct 20, 2010
5
0
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~~~
 
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
 
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

?>
 
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.
 
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?
 
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>
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.