您的位置:首页 > 编程语言 > C语言/C++

c++ 使用 tinyxml中对XML文件进行操作

2017-01-10 14:03 381 查看
tinyXML一款很优秀的操作C++类库,文件不大,但方法很丰富,和apache的Dom4j可以披靡啊!习惯了使用java类库的我看到这么丰富的c++类库,很高兴!它使用很简单,只需要拷贝几个文件到你的工程中,没有STL也可以编译运行。 
    
    下面我从这几个方面谈谈对tinyXML类库的使用以及理解。 

    首先在sourceforge上下载tinyXML类库,地址:http://sourceforge.net/projects/tinyxml/ 

    然后解压缩tinyXML后,将这六个文件添加到你的c++工程中,分别是tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。在需要操作xml文件的地方,使用如下代码,就可以引入tinyXML类库。

 #include "tinyxml.h"

 

 

TiXmlBase是所有类的基类,TiXmlNode、TiXmlAttribute两个类都继承来自TiXmlBase类,其中TiXmlNode类指的是所有被<...>...<.../>包括的内容,而xml中的节点又具体分为以下几方面内容,分别是声明、注释、节点以及节点间的文本,因此在TiXmlNode的基础上又衍生出这几个类TiXmlComment、TiXmlDeclaration、TiXmlDocument、TiXmlElement、TiXmlText、TiXmlUnknown,分别用来指明具体是xml中的哪一部分。TiXmlAttribute类不同于TiXmlNode,它指的是在尖括号里面的内容,像<...
***=...>,其中***就是一个属性。这块我具体用一个xml文档说明一下,内容如下: 

Xml代码  



<?xml version="1.0" encoding="UTF-8"?>  

<phonebook>  

    <!--one item behalfs one contacted person.-->  

    <item>  

        <name>miaomaio</name>  

    <addr>Shaanxi Xi'an</addr>  

    <tel>13759911917</tel>  

    <email>miaomiao@home.com</email>  

    </item>  

    <item>  

        <name>gougou</name>  

    <addr>Liaoning Shenyang</addr>  

    <tel>15840330481</tel>  

    <email>gougou@home.com</email>  

    </item>  

    <!--more contacted persons.-->  

</phonebook>  

 

像TiXmlDeclaration指的就是<?xml version="1.0" encoding="UTF-8"?>,

像TiXmlComment指的就是<!--one item behalfs one contacted person.-->、 <!--more contacted persons.-->,

像TiXmlDocument指的就是整个xml文档,

像TiXmlElement指的就是<phonebook>、<item>、<name>、<addr>等等这些节点,

像TiXmlText指的就是‘gougou’、‘15840330481’这些夹在<item>与</item>、<name>与</name>、<addr>与</addr>之间的文本文字,

像TiXmlAttribute指的就是<?xml version="1.0" encoding="UTF-8"?>节点中version、encoding,

除此之外就是TiXmlUnknown。

 

 

const char* xmlFile = "Profiles\\MX6Q Linux Update\\OS Firmware\\ucl2.xml";  //xml΄???Ă???

  TiXmlDocument doc;

  TiXmlElement* UCL = new TiXmlElement("UCL");

  doc.LinkEndChild(UCL);

  TiXmlElement* CFG = new TiXmlElement("CFG");

  UCL->LinkEndChild(CFG);

  

  TiXmlElement* ST1 = new TiXmlElement("STATE");

  CFG->LinkEndChild(ST1);

  ST1->SetAttribute("name", "BootStrap");

  ST1->SetAttribute("dev", "MX6Q");

  ST1->SetAttribute("vid", "15A2");

  ST1->SetAttribute("pid", "0054");

  TiXmlElement* ST2 = new TiXmlElement("STATE");

  CFG->LinkEndChild(ST2);

  ST2->SetAttribute("name", "Updater");

  ST2->SetAttribute("dev", "MSC");

  ST2->SetAttribute("vid", "066F");

  ST2->SetAttribute("pid", "37FF");

  TiXmlElement* LIST = new TiXmlElement("LIST");

  UCL->LinkEndChild(LIST);

  LIST->SetAttribute("name", "GWI-SPI_NOR");

  LIST->SetAttribute("desc", "Choose SPI-NOR as media");

  

  TiXmlElement* CMD1 = new TiXmlElement("CMD");

  CMD1->SetAttribute("state", "BootStrap");

  CMD1->SetAttribute("type", "boot");

  CMD1->SetAttribute("body", "BootStrap");

  CMD1->SetAttribute("file", "firmware/u-boot.imx");

  CMD1->SetAttribute("ifdev", "MX6Q");

  CMD1->LinkEndChild(new TiXmlText("Loading u-boot"));

  LIST->LinkEndChild(CMD1);

  TiXmlElement* CMD2 = new TiXmlElement("CMD");

  CMD2->SetAttribute("state", "BootStrap");

  CMD2->SetAttribute("type", "load");

  CMD2->SetAttribute("file", "firmware/zImage");

  CMD2->SetAttribute("address", "0x12000000");

  CMD2->SetAttribute("loadSection", "OTH");

  CMD2->SetAttribute("setSection", "OTH");

  CMD2->SetAttribute("HasFlashHeader", "FALSE");

  CMD2->SetAttribute("ifdev", "MX6Q  MX6DL");

  CMD2->LinkEndChild(new TiXmlText("Loading kernel"));

  LIST->LinkEndChild(CMD2);

  TiXmlElement* CMD3 = new TiXmlElement("CMD");

  CMD3->SetAttribute("state", "BootStrap");

  CMD3->SetAttribute("type", "load");

  CMD3->SetAttribute("file", "firmware/initramfs");

  CMD3->SetAttribute("address", "0x12c00000");

  CMD3->SetAttribute("loadSection", "OTH");

  CMD3->SetAttribute("setSection", "OTH");

  CMD3->SetAttribute("HasFlashHeader", "FALSE");

  CMD3->SetAttribute("ifdev", "MX6Q MX6DL");

  CMD3->LinkEndChild(new TiXmlText("Loading Initramfs"));

  LIST->LinkEndChild(CMD3);

  TiXmlElement* CMD4 = new TiXmlElement("CMD");

  CMD4->SetAttribute("state", "BootStrap");

  CMD4->SetAttribute("type", "load");

  CMD4->SetAttribute("file", "firmware/imx6q-gwi-scanner.dtb");

  CMD4->SetAttribute("address", "0x18000000");

  CMD4->SetAttribute("loadSection", "OTH");

  CMD4->SetAttribute("setSection", "OTH");

  CMD4->SetAttribute("HasFlashHeader", "FALSE");

  CMD4->SetAttribute("ifdev", "MX6Q");

  CMD4->LinkEndChild(new TiXmlText("Loading device tree"));

  LIST->LinkEndChild(CMD4);

  TiXmlElement* CMD5 = new TiXmlElement("CMD");

  CMD5->SetAttribute("state", "BootStrap");

  CMD5->SetAttribute("type", "jump");

  CMD5->LinkEndChild(new TiXmlText("Jumping to OS image"));

  LIST->LinkEndChild(CMD5);

  TiXmlComment * comment = new TiXmlComment();

  comment->SetValue(" erase uboot env partition ");

  LIST->LinkEndChild(comment);

  TiXmlElement* CMD6 = new TiXmlElement("CMD");

  CMD6->SetAttribute("state", "Updater");

  CMD6->SetAttribute("type", "push");

  CMD6->SetAttribute("body", "$ flash_erase /dev/mtd1 0 0");

  CMD6->LinkEndChild(new TiXmlText("Erasing uboot env partition"));

  LIST->LinkEndChild(CMD6);

  TiXmlComment * comment1 = new TiXmlComment();

  comment1->SetValue(" erase script partition ");

  LIST->LinkEndChild(comment1);

  TiXmlElement* CMD7 = new TiXmlElement("CMD");

  CMD7->SetAttribute("state", "Updater");

  CMD7->SetAttribute("type", "push");

  CMD7->SetAttribute("body", "$ flash_erase /dev/mtd2 0 0");

  CMD7->LinkEndChild(new TiXmlText("Erasing system partition"));

  LIST->LinkEndChild(CMD7);

  TiXmlComment * comment2 = new TiXmlComment();

  comment2->SetValue(" burn the system to norflash ");

  LIST->LinkEndChild(comment2);

  TiXmlElement* CMD8 = new TiXmlElement("CMD");

  CMD8->SetAttribute("state", "Updater");

  CMD8->SetAttribute("type", "push");

  CMD8->SetAttribute("body", "send");

  CMD8->SetAttribute("file", a );

  CMD8->LinkEndChild(new TiXmlText("Send scanner system Image"));

  LIST->LinkEndChild(CMD8);

  TiXmlElement* CMD9 = new TiXmlElement("CMD");

  CMD9->SetAttribute("state", "Updater");

  CMD9->SetAttribute("type", "push");

  CMD9->SetAttribute("body", "$ dd if=$FILE of=/dev/mtd2");

  CMD9->LinkEndChild(new TiXmlText("write system Image to NOR Flash"));

  LIST->LinkEndChild(CMD9);

  TiXmlElement* CMD10 = new TiXmlElement("CMD");

  CMD10->SetAttribute("state", "Updater");

  CMD10->SetAttribute("type", "push");

  CMD10->SetAttribute("body", "$ echo Update Complete!");

  CMD10->LinkEndChild(new TiXmlText("Done"));

  LIST->LinkEndChild(CMD10);

  

  doc.SaveFile(xmlFile);  //???掄??、

生成的xml 文件

 
<UCL>

  <CFG>

    <STATE name="BootStrap" dev="MX6Q" vid="15A2" pid="0054"/>

    <STATE name="Updater"   dev="MSC" vid="066F" pid="37FF"/>

  </CFG>

<LIST name="GWI-SPI_NOR" desc="Choose SPI-NOR as media">

 <CMD state="BootStrap" type="boot" body="BootStrap" file ="firmware/u-boot.imx" ifdev="MX6Q">Loading U-boot</CMD>

 <CMD state="BootStrap" type="load" file="firmware/zImage" address="0x12000000"loadSection="OTH" setSection="OTH" HasFlashHeader="FALSE" ifdev="MX6Q MX6DL">Loading Kernel.</CMD>

 <CMD state="BootStrap" type="load" file="firmware/initramfs" address="0x12C00000"loadSection="OTH" setSection="OTH" HasFlashHeader="FALSE" ifdev="MX6Q MX6DL">Loading Initramfs.</CMD>

 <CMD state="BootStrap" type="load" file="firmware/imx6q-gwi-scanner.dtb" address="0x18000000" loadSection="OTH" setSection="OTH" HasFlashHeader="FALSE" ifdev="MX6Q">Loading device tree.</CMD>

 <CMD state="BootStrap" type="jump" > Jumping to OS image.</CMD>

 <!--erase uboot env partition-->

 <CMD state="Updater" type="push" body="$ flash_erase /dev/mtd1  0 0">Erasing uboot env partition</CMD>

 <!--burn the system to spinor :-->

 <CMD state="Updater" type="push" body="$ flash_erase /dev/mtd2  0 0">Erasing system partition</CMD>

 <CMD state="Updater" type="push" body="send" file="xxxx">Sending scanner system</CMD>

 <CMD state="Updater" type="push" body="$ dd if=$FILE of=/dev/mtd2">write system Image to NOR flash</CMD>

 <CMD state="Updater" type="push" body="$ echo Update Complete!">Done</CMD>

</LIST>

</UCL>

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  xml c语言 imx6