CDATA Block Parsing - iphone

i was searched for this and am getting brain fire.
i am gettig
<description><![CDATA[<img src='http://behance.vo.llnwd.net/profiles22/700504/projects/2335700.jpg' style='float:left; margin-right:15px;' /><br /> NIL]]></description>
i dont know parse the Particular Link (http://behance.vo.llnwd.net/profiles22/700504/projects.jpg).
even Though i have tried to use
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
if([sElementName isEqualToString:#"description"])
{
NSMutableString *someString= [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
NSLog(#"%#",str);
}
}
it is get printed like
<img src='http://behance.vo.llnwd.net/profiles22/700504/projects/2335700.jpg' style='float:left; margin-right:15px;' /><br /> NIL
help me to get the particular link. Any links or answer may help..,
Thanks in Advance.,

The CDATA function is exactly for this purpose - if you have some XML that you want to embed into another XML as text (as opposed to as nested XML that modifies the structure itself). So, after obtaining this particular string, the <img> tag, you can use another XML parser to obtain the value of the src attribute.

Related

Displaying Different Languages in Label using html parsing

I have an URL -----> 182.72.253.75/test/surya/telugu.html
And this url contains some information in telugu language..
Now I need help in the following aspects---->
1) How to parse that html url..
2) How to display the information in that link in a UILabel in iphone simulator...(Because the information is in telugu language not in english language) ..
Thanks in Advance....
Here I have solved my problem...
Actually the URL contains...
ాష్ట్రపతి ప్రణబ్ ముఖర్జీ అధ్యక్షతన రాష్ట్రపతి భవన్లో గవర్నర్ల సదస్సు సోమవారం ప్రారంభమైంది. రెండు రోజుల పాటు జరిగే ఈ సదస్సులో 30 మంది గవర్నర్లు, లెప్ట్నెంట్‌ గవర్నర్లు పాల్గొన్నారు. ప్రధాని మన్మోహన్‌సింగ్‌తోపాటుగా, రక్షణ, ఆర్థిక, వ్యవసాయం, హోం, మానవ వనరుల అభివృద్ధి, పట్టణాభివృద్ధి, జలవనరులశాఖల మంత్రులతోపాటుగా, పౌరసర ఫరాలు, ఆహార, ప్రజాపంపిణీ, తాగునీరు, పారిశుద్ధ్య శాఖల స్వతంత్ర మంత్రులు హాజరయ్యారు. రాష్ట్రాల ఆర్థికాభివృద్ధితో పాటు మహిళల భద్రతపై ఈ సదస్సులో చర్చించనున్నారు.
The background source code of this This HTML page is as follows...
http://ashokios.blogspot.in/2013/02/background-sourcetemporary-post.html
But this is not in correct formate
so..,
1)Need to get the correct formate..
2)Need to convert into Dictionary.
Here we need two custom classes
XMLReader and GDataXMLDocument..
Now the solution is...
NSString *serverResponseStr=[XMLReader getResponseForService:#"http://182.72.253.75/test/surya/telugu.html" withParametersString:nil];
NSString *validXMLstring=[[NSString alloc]init];
GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithHTMLString:serverResponseStr error:nil];
if (doc)
{
validXMLstring=[[doc rootElement] XMLString];
}
NSDictionary *responseDictionary=[[NSDictionary alloc]init];
responseDictionary=[XMLReader dictionaryForXMLString:validXMLstring error:nil];
NSLog(#"%#",responseDictionary);
NSLog(#"%#",[[[[[[responseDictionary objectForKey:#"html"]objectForKey:#"body"]objectForKey:#"table"]objectForKey:#"tr"]objectForKey:#"td"]objectForKey:#"text"]);
self.myTextView.text=[[[[[[responseDictionary objectForKey:#"html"]objectForKey:#"body"]objectForKey:#"table"]objectForKey:#"tr"]objectForKey:#"td"]objectForKey:#"text"];

Subscript and Superscripts in CDATA of an xml file. Using UILabel to display the parsed XML contents

I need to display subscripts and superscripts (only arabic numerals) within a UILabel. The data is taken from an XML file. Here is the snippet of XML file:
<text><![CDATA[Hello World X\u00B2 World Hello]]></text>
Its supposed to display X2 (2 as superscript). When I read the string from the NSXMLParser and display it in the UILabel, it displays it as X\u00B2. Any ideas on how to make it work?
I think you can do something like this, assuming the CDATA contents have been read into an NSString and passed into this function:
-(NSString *)removeUnicodeEscapes:(NSString *)stringWithUnicodeEscapes {
unichar codeValue;
NSMutableString *result = [stringWithUnicodeEscapes mutableCopy];
NSRange unicodeLocation = [result rangeOfString:#"\\u"];
while (unicodeLocation.location != NSNotFound) {
// Get the 4-character hex code
NSRange charCodeRange = NSMakeRange(unicodeLocation.location + 2, 4);
NSString *charCode = [result substringWithRange:charCodeRange];
[[NSScanner scannerWithString:charCode] scanHexInt:&codeValue];
// Convert it to an NSString and replace in original string
NSString *unicodeChar = [NSString stringWithFormat:%C", codeValue];
NSRange replacementRange = NSMakeRange(unicodeLocation.location, 6);
[result replaceCharactersInRange:replacementRange withString:unicodeChar];
unicodeLocation = [result rangeOfString:#"\\u"];
}
return result;
}
I haven't had a chance to try this out, but I think the basic approach would work
\u00B2 is not any sort of XML encoding for characters. Apparently your data source has defined their own encoding scheme (which, frankly, is pretty stupid as XML is capable of encoding these directly, using entities outside of CDATA blocks).
In any case, you'll have to write your own parser that handles \u#### and converts that to the correct character.
I asked the question to my colleague and he gave me a nice and simple workaround. Am describing it here, in case others also get stuck at this.
Firstly goto this link. It has a list of all subscripts and superscripts. For example, in my case, I clicked on "superscript 0". In the following HTML page detailing "superscript 0", goto "Java Data" section and copy the "⁰". You can either place this directly in XML or write a simple regex in obj-c to replace \u00B2 with "⁰". And you will get nice X⁰. Do the same fro anyother superscript or subscript that you might want to display.

NSXMLParser processing complex units of information

I'm processing a response from the server using NSXMLParser successfuly.
Something like this
<data>
<company id="">
<name>XXX</name>
<latitude></latitude>
<longitude></longitude>
</company>
<company id="">
<name>XXX</name>
<latitude></latitude>
<longitude></longitude>
</company>
</data>
I've been using the next methods
didStartElement:namespaceURI: ... to detect when the new company need to be parsed, then I allocate a new instance. And also, detect when an attribute starts
foundCharacters: process the content of every attribute
didEndElement: ... the company has been parsed completely and could be added to the internal list. And also, detect when an attribute has been processed, then set the value processed on the foundCharacters: method
Now, I also need to get the complete XML for one company, and store it in a local cache, anybody knows if there is any way using NSXMLParser to get all the content just for one company? Or maybe without using NSXMLParser. don't know.
<company id="">
<name>XXX</name>
<latitude></latitude>
<longitude></longitude>
</company>
Thank you,
Finally I decided to re-create the XML usign the SAX methods
parser:didStartElement:
// Adding the initial TAG of the xml
accountXML = [[NSString alloc] initWithFormat:#"<%#", elementName];
for (NSString *key in [attributeDict allKeys]){
accountXML = [accountXML stringByAppendingFormat:#" %#=\"%#\"", key
, [attributeDict valueForKey:key]];
}
accountXML = [accountXML stringByAppendingString:#">\n"];
and
parser:didEndElement:
// Add the xml to the account and release it
accountXML = [accountXML stringByAppendingFormat:#"</%#>\n", elementName];
[account setCompleteXML:accountXML];

xml Tableview nsxmlparsing [duplicate]

I think I read every single web page relating to this problem but I still cannot find a solution to it, so here I am.
I have an HTML web page which is not under my control and I need to parse it from my iPhone application. Here is a sample of the web page I'm talking about:
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</HEAD>
<BODY>
<LI class="bye bye" rel="hello 1">
<H5 class="onlytext">
<A name="morning_part">morning</A>
</H5>
<DIV class="mydiv">
<SPAN class="myclass">something about you</SPAN>
<SPAN class="anotherclass">
Bye Bye è un saluto
</SPAN>
</DIV>
</LI>
</BODY>
</HTML>
I'm using NSXMLParser and it is going well till it find the è html entity. It calls foundCharacters: for "Bye Bye" and then it calls resolveExternalEntityName:systemID:: with an entityName of "egrave".
In this method i'm just returning the character "è" trasformed in an NSData, the foundCharacters is called again adding the string "è" to the previous one "Bye Bye " and then the parser raise the NSXMLParserUndeclaredEntityError error.
I have no DTD and I cannot change the html file I'm parsing. Do you have any ideas on this problem?
Update (12/03/2010). After the suggestion of Griffo I ended up with something like this:
data = [self replaceHtmlEntities:data];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
[parser parse];
where replaceHtmlEntities:(NSData *) is something like this:
- (NSData *)replaceHtmlEntities:(NSData *)data {
NSString *htmlCode = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
NSMutableString *temp = [NSMutableString stringWithString:htmlCode];
[temp replaceOccurrencesOfString:#"&" withString:#"&" options:NSLiteralSearch range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:#" " withString:#" " options:NSLiteralSearch range:NSMakeRange(0, [temp length])];
...
[temp replaceOccurrencesOfString:#"À" withString:#"À" options:NSLiteralSearch range:NSMakeRange(0, [temp length])];
NSData *finalData = [temp dataUsingEncoding:NSISOLatin1StringEncoding];
return finalData;
}
But I am still looking the best way to solve this problem. I will try TouchXml in the next days but I still think that there should be a way to do this using NSXMLParser API, so if you know how, feel free to write it here.
After exploring several alternatives, it appears that NSXMLParser will not support entities other than the standard entities <, >, &apos;, " and &
The code below fails resulting in an NSXMLParserUndeclaredEntityError.
// Create a dictionary to hold the entities and NSString equivalents
// A complete list of entities and unicode values is described in the HTML DTD
// which is available for download http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent
NSDictionary *entityMap = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:#"%C", 0x00E8], #"egrave",
[NSString stringWithFormat:#"%C", 0x00E0], #"agrave",
...
,nil];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:YES];
[parser parse];
// NSXMLParser delegate method
- (NSData *)parser:(NSXMLParser *)parser resolveExternalEntityName:(NSString *)entityName systemID:(NSString *)systemID {
return [[entityMap objectForKey:entityName] dataUsingEncoding: NSUTF8StringEncoding];
}
Attempts to declare the entities by prepending the HTML document with ENTITY declarations will pass, however the expanded entities are not passed back to parser:foundCharacters and the è and à characters are dropped.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
[
<!ENTITY agrave "à">
<!ENTITY egrave "è">
]>
In another experiment, I created a completely valid xml document with an internal DTD
<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE author [
<!ELEMENT author (#PCDATA)>
<!ENTITY js "Jo Smith">
]>
<author>< &js; ></author>
I implemented the parser:foundInternalEntityDeclarationWithName:value:; delegate method and it is clear that the parser is getting the entity data, however the parser:foundCharacters is only called for the pre-defined entities.
2010-03-20 12:53:59.871 xmlParsing[1012:207] Parser Did Start Document
2010-03-20 12:53:59.873 xmlParsing[1012:207] Parser foundElementDeclarationWithName: author model:
2010-03-20 12:53:59.873 xmlParsing[1012:207] Parser foundInternalEntityDeclarationWithName: js value: Jo Smith
2010-03-20 12:53:59.874 xmlParsing[1012:207] didStartElement: author type: (null)
2010-03-20 12:53:59.875 xmlParsing[1012:207] parser foundCharacters Before:
2010-03-20 12:53:59.875 xmlParsing[1012:207] parser foundCharacters After: <
2010-03-20 12:53:59.876 xmlParsing[1012:207] parser foundCharacters Before: <
2010-03-20 12:53:59.876 xmlParsing[1012:207] parser foundCharacters After: <
2010-03-20 12:53:59.877 xmlParsing[1012:207] parser foundCharacters Before: <
2010-03-20 12:53:59.878 xmlParsing[1012:207] parser foundCharacters After: <
2010-03-20 12:53:59.879 xmlParsing[1012:207] parser foundCharacters Before: <
2010-03-20 12:53:59.879 xmlParsing[1012:207] parser foundCharacters After: < >
2010-03-20 12:53:59.880 xmlParsing[1012:207] didEndElement: author with content: < >
2010-03-20 12:53:59.880 xmlParsing[1012:207] Parser Did End Document
I found a link to a tutorial on Using the SAX Interface of LibXML. The xmlSAXHandler that is used by NSXMLParser allows for a getEntity callback to be defined. After calling getEntity, the expansion of the entity is passed to the characters callback.
NSXMLParser is missing functionality here. What should happen is that the NSXMLParser or its delegate store the entity definitions and provide them to the xmlSAXHandler getEntity callback. This is clearly not happening. I will file a bug report.
In the meantime, the earlier answer of performing a string replacement is perfectly acceptable if your documents are small. Check out the SAX tutorial mentioned above along with the XMLPerformance sample app from Apple to see if implementing the libxml parser on your own is worthwhile.
This has been fun.
A possibly less hacky solution is replace the DTD with a local modified one with all external entity declaration replaced with local one.
This is how I do it:
First, find and replace the document DTD declaration with a local file. For example, replace this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><body><a href='a.html'>hi!</a><br><p>Hello</p></body></html>
with this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "file://localhost/Users/siuying/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/17065C0F-6754-4AD0-A1EA-9373F6476F8F/App.app/xhtml1-transitional.dtd">
<html><body><a href='a.html'>hi!</a><br><p>Hello</p></body></html>
```
Download the DTD from the W3C URL and add it to your app bundle. You can find the path of the file with following code:
NSBundle* bundle = [NSBundle bundleForClass:[self class]];
NSString* path = [[bundle URLForResource:#"xhtml1-transitional" withExtension:#"dtd"] absoluteString];
Open the DTD file, find any external entity reference:
<!ENTITY % HTMLlat1 PUBLIC
"-//W3C//ENTITIES Latin 1 for XHTML//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent">
%HTMLlat1;
replace it with the content of the entity file ( http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent in the above case)
After replacing all external reference, NSXMLParser should properly handle the entities without the need to download every remote DTD/external entities each time it parse a XML file.
You could do a string replace within the data before you parse it with NSXMLParser. NSXMLParser is UTF-8 only as far as I know.
I think your going to run into another problem with this example as it isn't vaild XML which is what the NSXMLParser is looking for.
The exact problem in the above is that the tags META, LI, HTML and BODY aren't closed so the parser looks all the way though the rest of the document looking for its closing tag.
The only way around this that I know of if you don't have access to change the HTML is to mirror it with the closing tags inserted.
I would try using a different parser, like libxml2 - in theory I think that one should be able to handle poor HTML.
Since I've just started doing iOS development I've been searching for the same thing and found a related mailing list entry: http://www.mail-archive.com/cocoa-dev#lists.apple.com/msg17706.html
- (NSData *)parser:(NSXMLParser *)parser resolveExternalEntityName: (NSString *)entityName systemID:(NSString *)systemID {
NSAttributedString *entityString = [[[NSAttributedString alloc] initWithHTML:[[NSString stringWithFormat:#"&%#;", entityName] dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL] autorelease];
NSLog(#"resolved entity name: %#", [entityString string]);
return [[entityString string] dataUsingEncoding:NSUTF8StringEncoding];
}
This is fairly similar to your original solution and also causes a parser error NSXMLParserErrorDomain error 26; but it does continue parsing after that. The problem is, of course, that it's harder to tell real errors apart ;-)

xml parsing iphone, objective C?

i want to get data between xml tags? how to navigate? and get values..
im using wsdl2objc from google code:http://code.google.com/p/wsdl2objc/
output soapbody follows:
read instruction here: http://code.google.com/p/wsdl2objc/wiki/UsageInstructions
my header file: #import "MService.h"
how to get image source and text value????
please help me....
if([bodyPart isKindOfClass:[types_getFavoriteColorResponseType class]]) {
types_getFavoriteColorResponseType *body = (types_getFavoriteColorResponseType*)bodyPart;
// Now you can extract the color from the response
q.text = body.color;
continue;
}
Ок as far as I understand this is a part which extracts text data from your SOAP response.
BTW you need response to be processed via SAX or DOM? First example in given URL refers to DOM usage, whereas the second to SAX.
More than that I can not tell. Guess you have to read manual or find someone, who worked with this.
Use NSXMLParser, NSXMLParserDelegate for xml parsing, you can get the callbacks with proper values:
parser:didStartElement:namespaceURI:qualifiedName:attributes:
parser:foundCharacters:
parser:didEndElement:namespaceURI:qualifiedName:
Ref: http://developer.apple.com/library/ios/#documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html
hey i got the result using sudzc.com
if ([result isKindOfClass:[MSalesPages class]]) {
NSLog(#"Response");
NSMutableArray* pageData = result.PageData;
for(MSalesPage* page in pageData){
NSLog(#"Inside for loop %#", page.Id);
NSMutableArray* images = page.Images;
NSMutableArray* textData = page.TextData;
for(MSalesImg* img in images){
NSLog(#"Image url %#",img.Src);
}
for(MSalesText* text in textData){
NSLog(#"Product Name %#",text.Value);
}
}
}
carefully check with the above xml, u will get the answer :)