Objective C ile HTML Kodunu Ayristirma
Merhaba,
Elimde türkce bir siteden cekilmis NSData olarak saklanmis bir html verisi var. Bunu ayristirip arasindaki bazi degerleri almam lazim. Internetteki arastirmamda bir kütüphane kullanarak parsing islemi yapmayla ilgili su sekilde bir tutorial buldum;
http://www.raywenderlich.com/14172/how-to-parse-html-on-ios
Buradaki yönergelere uydum ancak xpath olusturmak konusunda sikinti yasiyorum. Asagida örnegini verdigim parçanin sonundaki string degerlerini çekmeye calisiyorum;
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>Random string here</title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1347.57">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Times; color: #000000; -webkit-text-stroke: #000000}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Times; color: #0000ee; -webkit-text-stroke: #0000ee}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; text-align: right; font: 12.0px Times; color: #000000; -webkit-text-stroke: #000000}
p.p4 {margin: 0.0px 0.0px 12.0px 0.0px; font: 12.0px Times; color: #000000; -webkit-text-stroke: #000000}
p.p5 {margin: 0.0px 0.0px 12.0px 0.0px; font: 11.0px Times; color: #000000; -webkit-text-stroke: #000000; background-color: #ffffff}
p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Times; color: #000000; -webkit-text-stroke: #000000; min-height: 14.0px}
p.p7 {margin: 0.0px 0.0px 12.0px 0.0px; font: 12.0px Times; color: #000000; -webkit-text-stroke: #000000; min-height: 14.0px}
p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Times; color: #ffffff; -webkit-text-stroke: #ffffff}
td.td10 {width: 54.0px; background-color: #425165; margin: 0.5px 0.5px 0.5px 0.5px; padding: 1.0px 1.0px 1.0px 1.0px}
td.td11 {width: 43.0px; background-color: #425165; margin: 0.5px 0.5px 0.5px 0.5px; padding: 1.0px 1.0px 1.0px 1.0px}
td.td12 {width: 48.0px; background-color: #425165; margin: 0.5px 0.5px 0.5px 0.5px; padding: 1.0px 1.0px 1.0px 1.0px}
td.td13 {width: 52.0px; background-color: #425165; margin: 0.5px 0.5px 0.5px 0.5px; padding: 1.0px 1.0px 1.0px 1.0px}
td.td14 {width: 38.0px; background-color: #425165; margin: 0.5px 0.5px 0.5px 0.5px; padding: 1.0px 1.0px 1.0px 1.0px}
td.td15 {width: 55.0px; background-color: #425165; margin: 0.5px 0.5px 0.5px 0.5px; padding: 1.0px 1.0px 1.0px 1.0px}
td.td43 {width: 55.0px; background-color: #f3f3f3; margin: 0.5px 0.5px 0.5px 0.5px; padding: 1.0px 1.0px 1.0px 1.0px}
td.td48 {width: 49.0px; background-color: #f3f3f3; margin: 0.5px 0.5px 0.5px 0.5px; padding: 1.0px 1.0px 1.0px 1.0px}
</style>
</head>
<body>
<table width="787.0" cellspacing="0" cellpadding="0" class="t1">
<tbody>
<tr>
<td valign="top" class="td1">
<p class="p1"><span class="s1"> </span></p>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="middle" class="td2">
<p class="p2"><span class="s1"><a href="http://www.sitenamehere/"><span class="s2"></span></a></span></p>
</td>
<td valign="middle" class="td3">
<p class="p3"><span class="s1">2014-2015 Summer School<span class="Apple-converted-space"> </span></span></p>
</td>
<td valign="middle" class="td4">
<p class="p1"><span class="s1"> </span></p>
</td>
</tr>
</tbody>
</table>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" class="td5">
<p class="p1"><span class="s1"> </span></p>
</td>
<td valign="top" class="td6">
<p class="p4"><span class="s1"><b>random string /COURSE CODE :</b></span></p>
<p class="p5"><span class="s1">some string here/Select</span></p>
<p class="p5"><span class="s1">string1<span class="Apple-converted-space"> </span></span></p>
<p class="p5"><span class="s1">string2<span class="Apple-converted-space"> </span></span></p>
<p class="p5"><span class="s1">string3<span class="Apple-converted-space"> </span></span></p>
<p class="p5"><span class="s1">string4<span class="Apple-converted-space"> </span></span></p>
xPath'i su sekilde olusturmaya calisiyorum;
NSString *myXpath = @"//p[@class='p5']";
Ancak daha sonradan bu xPath'i ilgili kütüphanenin fonksiyonunda kullanmaya calisdigimda nil döndürüyor zaten yukaridaki tanimlamamda sorun cikacagini az çok tahmin ediyordum. Tutorial'a baktim bu sekilde iki class<> <class> arasindaki degerleri cekmeyle ilgili pek bir örnek yok. Bu path dosyasini yukarida verdigim string degerlerini cekecek sekilde nasil duzenleyebilirim fikri olan var mi acaba ?