用iks解析XML

来自于网络资源

 

步骤:

 

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();
 }

 

你可能感兴趣的:(c,xml,网络,tree,delete,include)