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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-25-2010, 04:30 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 3
Default Parsing XML created in PHP

Hello,

i'm trying to parse a xml string with NSXMLParser which i created in PHP.
My PHP sript is:

$doc = new DomDocument('1.0');
//$doc->preserveWhiteSpace=false;
$root = $doc->createElement("root");
$root = $doc->appendChild($root);
$child = $doc->createElement("child");
$child = $root->appendChild($child);
$value = $doc->createTextNode("testvar");
$value = $child->appendChild($value);
$xml_string = $doc->saveXML();
print $xml_string;


If i open the page in the browser it shows me 'testvar' like it should.
Showing the source it shows me that there're several whitespaces:
begin ->| <?xml version="1.0"?>
<root><child>testvar</child></root>
|<--end

Setting preserveWhitespace to false doesn't change anything.
So i tried with regex in php:
$xml_string1 = preg_replace("/\s+</","<",$xml_string);
$xml_string2 = preg_replace("/>\s+/",">",$xml_string1);
print $xml_string2;

This removes the \n and brings me to this
begin-->| <?xml version="1.0"?><root><child>testvar</child></root> |<--end

In the browser it always shows the wanted result: testvar

On the iphone i get following error:
NSXMLParserErrorDomain Code = 64, Operation could not be completed.

I don't know whats the problem with the xml, i receive it correctly like under This removes the \n ....

I tried to debug but after starting the parser with [parser parse] none of the delegate functions is called (yes, i set the delegate) exept the parserError.
As there're still spaces before and after the xml may this is the reason, but i can't get them out.
In my application on the iPhone i tried different encodings, UTF8 and ASCII.
I also tried to convert the received data to a string, called
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet
and converted back.

I also set the following:
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];

I don't know what i'm doing wrong, please help.

Thanks in advance.
Matt_10 is offline   Reply With Quote
Old 02-25-2010, 05:19 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 16
Default

Apple says, it is Misplaced XML declaration.

Take a look at the following page, under "Constants" title there are error codes for NSXMLParser:

Mac Dev Center: NSXMLParser Class Reference
rexmont is offline   Reply With Quote
Old 02-25-2010, 11:42 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 3
Default

Quote:
Originally Posted by rexmont View Post
Apple says, it is Misplaced XML declaration.

Take a look at the following page, under "Constants" title there are error codes for NSXMLParser:

Mac Dev Center: NSXMLParser Class Reference
Hello,

yes, thanks for the answer but this doesn't help at all.
I forgot to mention that i looked at this, this pointed me in the direction of the whitespaces as i mentioned in my first post. I just don't understand how or what can be misplaced except the whitespaces after (which i get rid of) and before the <?...?>.
I'm aware of working around this by removing the XML declaration. I could just send a comma separated string also and parse it myself as Apple provides no real enhancement in this way anyways.
Still i'd like to use XML as it's standardized and a common format.
Nobody here ever worked with a XML created with PHP?
Why do i get those whitespaces at the beginning?

Oh, by the way, great forum here, helped me more than several books.

Thanks.
Matt_10 is offline   Reply With Quote
Old 02-25-2010, 11:52 AM   #4 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 3
Default

Found the answer after wasting 1.5 days. PHP also uses a similar notation: <?php ... ?>. Note that the final php ?> should be omitted from all PHP code files—modules, includes, etc. The closing delimiter is optional in PHP (but not in XML), and removing it helps prevent unwanted white space at the end of files which can cause problems
I wasn't aware of that, after removing the f***** closing delimiter it works just fine.
Matt_10 is offline   Reply With Quote
Old 02-25-2010, 11:58 AM   #5 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 16
Default

Glad you solved it.
rexmont is offline   Reply With Quote
Old 07-01-2010, 12:04 AM   #6 (permalink)
Senior Member
 
Join Date: Feb 2010
Location: dallas
Posts: 219
Default XML pasrser

right now i am pasring 1 xml from web in appdidfinsih()

but i want to parse 1 more xml before that to get the location of user
so should i edit same xmlparser class for all xml or i have to make new classes for diffrent xml pasrser


plz help

if i use old class then elemnts name will be different
gud4nuthin is offline   Reply With Quote
Reply

Bookmarks

Tags
error, iphone, parse, php, 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: 244
12 members and 232 guests
2WeeksToGo, ADY, BrianSlick, Dani77, Dattee, headkaze, mer10, mgon987, timle8n1, Touchmint, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,879
Threads: 89,228
Posts: 380,745
Top Poster: BrianSlick (7,129)
Welcome to our newest member, mgon987
Powered by vBadvanced CMPS v3.1.0

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