I'm trying to extract some XML from a UIWebView
, and not having much luck. This is what the entire document looks like:
<?xml version="1.0" encoding="utf-8"?>
<authResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<firstName>Name here</firstName>
<lastName>Name here</lastName>
<membershipTier>Membership level</membershipTier>
<errorMessage />
<isValid>1</isValid>
</authResponse>
I have tried a number of variations of the stringByEvaluatingJavaScriptFromString
method, but this is the only one that will return anything for me:
[webView stringByEvaluatingJavaScri开发者_运维技巧ptFromString:@"document.documentElement.textContent"]
However, I need the raw XML, not the text content. Any suggestions?
EDIT: I have control over the XML, so it can be modified if need be.\
EDIT 2: As requested, code I am using to load the request is below. Note that this request is for a login screen. Once the user has entered their credentials, the page after that is the one I am trying to extract the results from. I determine which is the current page in webViewDidFinishLoad
by testing the current mainDocumentURL
for the UIWebView
object.
NSString* urlString = kLoginURL;
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
UIWebView* tmpWebView = [[UIWebView alloc] init];
self.webView = tmpWebView;
[tmpWebView release];
self.webView.delegate = self;
[self.webView loadRequest:request];
Just wrote a little test program to figure this out (fun!). It loads the UIWebView contents via:
NSString * data = @"<?xml version=\"1.0\" encoding=\"utf-8\"?><authResponse xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><firstName>Name here</firstName><lastName>Name here</lastName><membershipTier>Membership level</membershipTier><errorMessage /><isValid>1</isValid></authResponse>";
[self.webview loadHTMLString: data baseURL: nil];
I then passed the string document.getElementsByTagName('body')[0].innerHTML
to stringByEvaluatingJavaScriptFromString
and it returns what you want:
<authresponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><firstname>Name here</firstname><lastname>Name here</lastname><membershiptier>Membership level</membershiptier><errormessage><isvalid>1</isvalid></errormessage></authresponse>
i'd grab the whole html as a string on a seperate thread, or use this as the basis to load the webview:
NSString *xmlString = [NSString stringWithContentsOfURL:yourURL]
You can try using kissxml to retrieve the whole xml structure.
Or you can just save the response into a string from your NSURLRequest or if you are using other libraries to do a http connection
NSURL *url = [[NSURL alloc] initWithString:@"http://www.w3schools.com/xml/note.xml"];
NSString *myString = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:(NSURL*)url] encoding:NSISOLatin1StringEncoding];
The code above is a sample on how you can retrieve the xml structure. This is much easier than using uiwebview. The sample xml encoding is using ISO-8859-1 so you have to set the correct encoding so that the data will be readable
I would suggest going with Nik's suggestion - get the request URL and get its content in an NSString.
OR you can send a request to the login URL (you are already intercepting the requests being sent) and get a response in a string.
If you want to read the entire body tag's content or the html and body tag's content, you might just do this
NSString *html = [yourWebView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
or
NSString *html = [yourWebView stringByEvaluatingJavaScriptFromString:@"document.all[0].innerHTML"];
You can use either of these as per your need:
NSString *html1 = [webView stringByEvaluatingJavaScriptFromString:
@"document.body.innerHTML"];
NSString *html2 = [webView stringByEvaluatingJavaScriptFromString:
@"document.documentElement.outerHTML"];
精彩评论