将XML的标题和内容分离

前端之家收集整理的这篇文章主要介绍了将XML的标题和内容分离前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

帮同学完成的一个实验作业:将XML的标题内容分离。

//栈

  1. #include<iostream>
  2. #include<stack>
  3. using namespace std;
  4. int main(){
  5. string str;
  6. stack<char>tag,contents,xml;
  7. int i,j,k,t;
  8. getline(cin,str);//输入字符串str
  9. for(i=0; i<str.length(); i++)
  10. {
  11. if(str[i]!='/')
  12. {
  13. xml.push(str[i]);
  14. }
  15. else if(str[i]=='/')
  16. {
  17. while(xml.size()!=0)
  18. {
  19. if(xml.top()!='>')
  20. {
  21. contents.push(xml.top());
  22. xml.pop();
  23. }
  24. else if(xml.top()=='>')
  25. {
  26. cout<<"Contents is:";
  27. while(contents.size()!=1)
  28. {
  29. cout<<contents.top();
  30. contents.pop();
  31. }
  32. contents.pop();
  33. cout<<endl;
  34. break;
  35. }
  36. }
  37. tag.push('>');
  38. while(xml.size()!=0)
  39. {
  40. if(xml.top()!='<')
  41. {
  42. tag.push(xml.top());
  43. xml.pop();
  44. }
  45. else if(xml.top()=='<')
  46. {
  47. cout<<"Tag is:<";
  48. while(tag.size()!=1)
  49. {
  50. cout<<tag.top();
  51. tag.pop();
  52. }
  53. tag.pop();
  54. cout<<endl;
  55. break;
  56. }
  57. }
  58. }
  59. }
  60. }

输入

<TITLE>Empire Burlesque</TITLE><ARTIST>Bob Dylan</ARTIST><COUNTRY>USA</COUNTRY> 输出 Tag is:<TITLE> Contents is:Empire Burlesque Tag is:<ARTIST> Contents is:Bob Dylan Tag is:<COUNTRY> Contents is:USA

猜你在找的XML相关文章