2012-10-30 48 views
-1

我想在IOS解析這個XML代碼:奇怪的XML文檔解析

<?xml version="1.0" encoding="ISO-8859-1"?> 
<ofertas> 
    <oferta> 
     <id>138</id> 
     <connector/> 
     <codigo>PARMAD</codigo> 
     <titulo><![CDATA[Madrid, BordÈus e Paris]]></titulo> 

     <descricao><![CDATA[ 7 dias c/ Pequeno AlmoÁo - Apenas Circuito Terrestre - Alojamento e Pequeno-AlmoÁo ; 7 dias c/ Pequeno AlmoÁo - SaÌda com voo de Lisboa - Alojamento e Pequeno-AlmoÁo ; 7 dias c/ Pequeno AlmoÁo - SaÌda com voo do Porto - Alojamento e Pequeno-AlmoÁo ; 7 dias c/ Pequeno AlmoÁo - SaÌda com voo de Faro - Alojamento e Pequeno-AlmoÁo]]></descricao> 
     <datas><![CDATA[Consultar programa]]></datas> 
     <data1>2012-07-31</data1> 
     <data2>2013-03-23</data2> 

     <categoria>Europa</categoria> 
     <subcategoria>Circuitos Europa</subcategoria> 
     <zona>Turistica</zona> 
     <tipo>Circuitos Europa</tipo> 

     <valor>575</valor> 
     <dias>6</dias> 

     <imagem>http://www.optitravel.net/optitravel/www/media/custom/cli_202/media/PKT_138_1343738358.jpg</imagem> 

     <link/> 

    </oferta> 

    <oferta> 
     <id>140</id> 
     <connector/> 
     <codigo>PARPRG</codigo> 
     <titulo><![CDATA[Paris, Frankfurt e Praga]]></titulo> 

     <descricao><![CDATA[ 7 dias c/ Pequeno AlmoÁo - Apenas Circuito Terrestre - Alojamento e Pequeno-AlmoÁo ; 7 dias c/ Pequeno AlmoÁo - SaÌda com voo de Lisboa - Alojamento e Pequeno-AlmoÁo ; 7 dias c/ Pequeno AlmoÁo - SaÌda com voo do Porto - Alojamento e Pequeno-AlmoÁo ; 7 dias c/ Pequeno AlmoÁo - SaÌda com voo de Faro - Alojamento e Pequeno-AlmoÁo]]></descricao> 
     <datas><![CDATA[01/Nov, 08/Nov, 15/Nov, 29/Nov, 13/Dez, 27/Dez, 10/Jan/2013, 24/Jan/2013, 07/Fev/2013, 21/Fev/2013, 07/Mar/2013, 21/Mar/2013]]></datas> 
     <data1>2012-08-01</data1> 
     <data2>2013-03-21</data2> 

我不知道這是否是解析大文件最好的選擇,但使用SMXMLDocument IM來解析這個特定的XML。我遇到的問題是我無法解碼這個XML。下面是XML解析器的作者給出的代碼示例,我使用:

//REPLACED WITH MY XML DOC 
NSString *sampleXML = [[NSBundle mainBundle] pathForResource:@"global" ofType:@"xml"]; 
NSData *data = [NSData dataWithContentsOfFile:sampleXML]; 

// create a new SMXMLDocument with the contents of sample.xml 
NSError *error; 
SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error]; 

// check for errors 
if (error) { 
    NSLog(@"Error while parsing the document: %@", error); 
    return; 
} 

// demonstrate -description of document/element classes 
NSLog(@"Document:\n %@", document); 

// Pull out the <books> node 
SMXMLElement *books = [document.root childNamed:@"????"]; 

// Look through <books> children of type <book> 
for (SMXMLElement *book in [books childrenNamed:@"????"]) { 

    // demonstrate common cases of extracting XML data 
    NSString *isbn = [book attributeNamed:@"id"]; // XML attribute 
    NSString *title = [book valueWithPath:@"titulo"]; // child node value 


    // show off some KVC magic 
    NSArray *authors = [[book childNamed:@"authors"].children valueForKey:@"value"]; 

    NSLog(@"Found a book!\n ISBN: %@ \n Title: %@ \n Price: %f \n", isbn, title, price); 
} 

如果解析這個文件請告知更好的XML解析器。

+2

嗨,你的問題沒有被正確地問。你應該添加你嘗試過的代碼,並且給出更多關於什麼工作和什麼不工作的細節。你說「我不能解碼這個XML」,爲什麼?您應該查看與示例代碼關聯的xml文件,並嘗試瞭解它是如何工作的。 – rdurand

+0

我已經嘗試了幾種方法來做這個代碼,但我不能理解XML文檔:( –

+0

你想要什麼樣的結果 – Purva

回答

1

正如@AppleDelegate所說,我也使用TBXML Parser。這是我將使用的代碼。在使用之前,您需要先導入TBXML+HTTP.h。只需在需要的地方調用[self getXML],代碼就可以完成剩下的工作。

首先獲得XML:

-(void)getXML { 

    NSLog(@"Getting XML"); 

    // Create a success block to be called when the async request completes 
    TBXMLSuccessBlock successBlock = ^(TBXML *tbxmlDocument) { 

     // If TBXML found a root node, process element and iterate all children 
     if (tbxmlDocument.rootXMLElement) 
      [self traverseElement:tbxmlDocument.rootXMLElement]; 

    }; 

    // Create a failure block that gets called if something goes wrong 
    TBXMLFailureBlock failureBlock = ^(TBXML *tbxmlDocument, NSError * error) { 
     NSLog(@"Error! %@ %@", [error localizedDescription], [error userInfo]); 
    }; 

    // Initialize TBXML with the URL of an XML doc. TBXML asynchronously loads and parses the file. 
    TBXML *tbxml = [[TBXML alloc] initWithURL: [NSURL URLWithString:@"File.xml"] 
             success: successBlock 
             failure: failureBlock]; 
} 

然後通過文件閱讀:

- (void) traverseElement:(TBXMLElement *)element { 

    TBXMLElement *oferta = element->firstChild; 

    do { 

     // Obtain first attribute from element 
     TBXMLElement *element = oferta->firstChild; 
     NSString *ofertaId = [NSString stringWithString: [TBXML textForElement:element]]; 
     NSLog(@"%@", ofertaId); 

     // Obtain the next element 
     element = element->nextSibling; 
     // Nothing to do (<connector/>) 

     // Obtain the next element 
     element = element->nextSibling; 
     NSString *codigo = [TBXML textForElement:element]; 
     NSLog(@"%@", codigo); 

     // Obtain the next element 
     element = element->nextSibling; 
     NSString *titulo = [TBXML textForElement:element]; 
     NSLog(@"%@", titulo); 

     // Obtain the next element 
     element = element->nextSibling; 
     NSString *descricao = [TBXML textForElement:element]; 
     NSLog(@"%@", descricao); 

     // Obtain the next element 
     element = element->nextSibling; 
     NSString *datas = [TBXML textForElement:element]; 
     NSLog(@"%@", datas); 

     // Obtain the next element 
     element = element->nextSibling; 
     NSString *data1 = [TBXML textForElement:element]; 
     NSLog(@"%@", data1); 

     // Obtain the next element 
     element = element->nextSibling; 
     NSString *data2 = [TBXML textForElement:element]; 
     NSLog(@"%@", data2); 

     // Obtain the next element 
     element = element->nextSibling; 
     NSString *categoria = [TBXML textForElement:element]; 
     NSLog(@"%@", categoria); 

     // Obtain the next element 
     element = element->nextSibling; 
     NSString *subcategoria = [TBXML textForElement:element]; 
     NSLog(@"%@", subcategoria); 

     // Obtain the next element 
     element = element->nextSibling; 
     NSString *zona = [TBXML textForElement:element]; 
     NSLog(@"%@", zona); 

     // Obtain the next element 
     element = element->nextSibling; 
     NSString *tipo = [TBXML textForElement:element]; 
     NSLog(@"%@", tipo); 

     // Obtain the next element 
     element = element->nextSibling; 
     NSString *valor = [TBXML textForElement:element]; 
     NSLog(@"%@", valor); 

     // Obtain the next element 
     element = element->nextSibling; 
     NSString *dias = [TBXML textForElement:element]; 
     NSLog(@"%@", dias); 

     // Obtain the next element 
     element = element->nextSibling; 
     NSString *imagem = [TBXML textForElement:element]; 
     NSLog(@"%@", imagem); 

     // Obtain the next element 
     element = element->nextSibling; 
     // Nothing to do (<link/>) 

     // Save infos 

    } while ((oferta = oferta->nextSibling)); 

    NSLog(@"Done"); 
} 

我已經測試過的代碼,它似乎好工作(運行代碼,並檢查控制檯日誌)。所有你需要做的就是將// Save infos從我的代碼更改爲你保存所有字符串的地方。該do-while將通過xml中的所有<oferta>元素,並將所需的所有元素保存在單獨的字符串中。然後你可以操作字符串來存儲日期,數字和全部。

+0

感謝您的驚人的幫助,但我注意到,這段代碼給出(空)值,是任何解決方案,因爲它具有與這些特定元素相關的數據,代碼:element = element-> nextSibling; NSString * titulo = [TBXML textForElement:element]; NSLog(@「%@」,titulo); //獲取下一個元素 element = element-> nextSibling; NSString * descricao = [TBXML textForElement:element]; NSLog(@「%@」,descricao); –

+0

這很奇怪,我沒有它時我運行代碼,你應該檢查你通過do-而當你得到這個錯誤並檢查xml。你在控制檯中看到*(null)*嗎? – rdurand

+0

這個XML是從windows和im使用mac複製的,因爲有些字符在mac上不可識別,我在我的XML上得到了奇怪的字符,例如:é,â,ã,í。我糾正了他們,解析工作正常。 –