用iks解析XML

來源:互聯網
上載者:User

來自於網路資源

 

步驟:

 

1 去http://code.google.com/p/iksemel/下載最新的iksemel1.4;

2 然後把include目錄下的標頭檔和src目錄下的 dom.c,iks.c,ikstack.c,utility.c,sax.c添加到自己工程。

3 記得添加estlib.lib庫,否則在使用 malloc的時候程式會崩潰。

4 estlib.lib在libc目錄下,所以,在mmp中添加。

 

代碼:

ParseXmlL()
 {

 _LIT(KXmlFileName,"C://food.xml");
 RFile readfile;
 RFs fs;
 fs.Connect();

 TInt err = readfile.Open(fs, KXmlFileName, EFileRead);
 if (err != KErrNone)
  {
  readfile.Close();
  fs.Close();
  return;
  }

 TInt filesize(0);
 readfile.Size(filesize);
 HBufC8* xmlData = HBufC8::NewL(filesize);
 TPtr8 ptr8(xmlData->Des());
 readfile.Read(ptr8);

 CTextListBoxModel* model = iListBox->Model();
 CDesCArray* itemArray = static_cast<CDesCArray*> (model->ItemTextArray());

 TInt ok;
 iks* root = iks_tree((char*) xmlData->Des().Ptr(), xmlData->Des().Length(),
   &ok);
 if (ok == IKS_OK)
  {

  iks* userslist = iks_find(root, "channel");
  iks* userlist = iks_find(userslist, "item");
  while (userlist)
   {
   if (iks_type(userlist) == IKS_TAG)
    {
    //    name
    char* valueName = iks_find_cdata(userlist, "category");
    TPtrC8 ptrName((unsigned char *) valueName);
    HBufC* bufName = HBufC::NewL(ptrName.Length());
    TPtr16 ptrName16 = bufName->Des();
    CnvUtfConverter::ConvertToUnicodeFromUtf8(ptrName16, ptrName);
    ptrName16.Trim();

    //    telephone number
    char* valueMobile = iks_find_cdata(userlist, "title");
    TPtrC8 ptrMobile((unsigned char *) valueMobile);
    HBufC* bufMobile = HBufC::NewL(ptrMobile.Length());
    TPtr16 ptrMobile16 = bufMobile->Des();
    CnvUtfConverter::ConvertToUnicodeFromUtf8(ptrMobile16,
      ptrMobile);
    ptrMobile16.Trim();
    _LIT ( KStringHeader2, "/t%S/t%S" );
    TBuf<50> buf2;
    buf2.Format(KStringHeader2(), &ptrName16, &ptrMobile16);

    itemArray->AppendL(buf2);
    delete bufName;
    }
   userlist = iks_next(userlist);
   }
  }
 iks_delete(root);

 delete xmlData;
 readfile.Close();
 fs.Close();

 iListBox->HandleItemAdditionL();
 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.