• 4972阅读
  • 7回复

[提问]如何取出xml问的一块内容 [复制链接]

上一主题 下一主题
离线novesky
 

只看楼主 倒序阅读 楼主  发表于: 2011-08-01
举个例子,我希望取出<items> </items>之间所有的内容保存成另外一个xml,有没有什么简单的方法?

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <programs>
  3.   <program id="0">
  4.     <regions>
  5.       <region id="0">
  6.     <x>0</x>
  7.     <y>0</y>
  8.     <width>64</width>
  9.     <height>32</height>
  10.     <border_color>255</border_color>
  11.     <border_type>0</border_type>
  12.     <items>
  13.       <item id="0">
  14.         <content_type>2</content_type>
  15.         <effect_entry>1</effect_entry>
  16.         <display_time>0</display_time>
  17.         <display_content>/mnt/Program/firmware/programs/text.jpg</display_content>
  18.         <period>2</period>
  19.         <fps>24</fps>
  20.         <loop_count>1</loop_count>
  21.         <clock_type>0</clock_type>
  22.         <clock_style>0</clock_style>
  23.         <timer_value>0</timer_value>
  24.       </item>
  25.       <item id="1">
  26.         <content_type>2</content_type>
  27.         <effect_entry>2</effect_entry>
  28.         <display_time>0</display_time>
  29.         <display_content>/mnt/Program/firmware/programs/kazo.jpg</display_content>
  30.         <period>0</period>
  31.         <fps>20</fps>
  32.         <loop_count>1</loop_count>
  33.         <clock_type>0</clock_type>
  34.         <clock_style>2</clock_style>
  35.         <timer_value>0</timer_value>
  36.       </item>
  37.     </items>
  38.       </region>
  39.     </regions>
  40.   </program>
  41. </programs>




离线ppdayz

只看该作者 1楼 发表于: 2011-08-01
取出items 的element,然后添加到个新的QDomDocument ,xml没有什么简单的方法,只有设计上面的巧妙,代码就只能死写
离线novesky

只看该作者 2楼 发表于: 2011-08-02
回 1楼(ppdayz) 的帖子
你是说遍历一下?
离线ppdayz

只看该作者 3楼 发表于: 2011-08-02
回 2楼(novesky) 的帖子
  1. QDomDocument domDoc;
  2.     QString errorStr;
  3.     int errorLine;
  4.     int errorColumn;
  5.     if (!domDoc.setContent(你的xml, true, &errorStr, &errorLine,
  6.                            &errorColumn)) {
  7.         qDebug() << QString(" Parse error at line %1, column %2:\n%3")
  8.                     .arg(errorLine)
  9.                     .arg(errorColumn)
  10.                     .arg(errorStr);
  11.         return;
  12.     }
  13.     
  14. QDomElement root =domDoc.documentElement;
  15. QDomElement itemsElement = root.firstChildElement("items");
  16. QDomDocument tmp;
  17. tmp.appendChild(itemsElement);


然后就把tmp写入文件就可以了
离线novesky

只看该作者 4楼 发表于: 2011-08-02
回 3楼(ppdayz) 的帖子
貌似不行,
Calling appendChild() on a null node does nothing.
离线ppdayz

只看该作者 5楼 发表于: 2011-08-02
回 4楼(novesky) 的帖子

我写的层级关系是不对的。只是个示例。items是哪一层我也没有去仔细的看。
你去改下就可以了
离线novesky

只看该作者 6楼 发表于: 2011-08-03
回 5楼(ppdayz) 的帖子
我就算tmp.appendChild(root)也提示Calling appendChild() on a null node does nothing.
保存后是空的
离线novesky

只看该作者 7楼 发表于: 2011-08-03
后面改成这样倒是好了
QDomDocument tmp;
QDomProcessingInstruction instruction = tmp.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
tmp.appendChild(instruction);
tmp.appendChild(itemsElement);
估计新的tmp先要建一个节点才能appendChild
快速回复
限100 字节
 
上一个 下一个