Using C + + to manipulate XML, mainly the use of internal loop methods

Source: Internet
Author: User

This paper mainly shares the use of the loop method, set the XML node properties, using 3 kinds of cyclic methods.

XML file:

<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<root>
<seqs>
<seq name= "A" license= "1" enable= "true"/>
<seq name= "B" license= "1" enable= "true"/>
</seqs>
</root>

The first method: the Vecseq size as a cyclic condition, the most convenient, because you can directly use the int i

#include <iostream>
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_print.hpp"
#include "boost/property_tree/xml_parser.hpp"

using namespace Rapidxml;

struct SEQSTRUCT
{
Std::string Seqname;
Std::string Seqlicense;
};

int main ()
{
Seqstruct Seqs;
std:: vector<seqstruct> vecseq;
Seqs.seqname = "Zhang";
Seqs.seqlicense = "true";
Vecseq.push_back (SEQS);
Seqs.seqname = "Liu";
Seqs.seqlicense = "true";
Vecseq.push_back (SEQS);

for (int i = 0; i < 2; i++)
{
Std::cout <<vecSeq[i].seqName;
Std::cout <<vecSeq[i].seqLicense;
Std::cout << Std::endl;
}

File<> Fdoc ("E:/uimris/branches/umr_main/main/features/win32test/xmlfile6.xml");
Std::cout << fdoc.data () << Std::endl;
Xml_document<> xmldoc;
Xmldoc.parse<0> (Fdoc.data ());

xml_node<>* RootNode = Xmldoc.first_node ();
xml_node<>* Seqsnode = Rootnode->first_node ();

xml_node<>* Seqnode = Seqsnode->first_node ();

for (int i = 0; i < vecseq.size (); ++i)
{
xml_attribute<>* attrseq = Seqnode->first_attribute ();
Attrseq->value (Xmldoc.allocate_string (Vecseq[i].seqname.c_str ()));

Attrseq = Attrseq->next_attribute ();
Attrseq->value (Xmldoc.allocate_string (Vecseq[i].seqlicense.c_str ()));

Seqnode = Seqnode->next_sibling ();
}
std::string text;
Rapidxml::p rint (Std::back_inserter (text), xmldoc, 0);
std::cout<<text<<std::endl;
Std::ofstream out ("E:/uimris/branches/umr_main/main/features/win32test/xmlfile7.xml");
if (!out.good ())
{
std::cout<< "Error";
}
Out << text;
Out.close ();

GetChar ();

return 0;
}

The second method is to point to the loop as a node, but need to redefine an int i

int i = 0;
for (xml_node<>* Seqnode = Seqsnode->first_node (); Seqnode! = NULL; seqnode = seqnode->next_sibling ())
{
xml_attribute<>* attrseq = Seqnode->first_attribute ();
Attrseq->value (Xmldoc.allocate_string (Vecseq[i].seqname.c_str ()));

Attrseq = Attrseq->next_attribute ();
Attrseq->value (Xmldoc.allocate_string (Vecseq[i].seqlicense.c_str ()));
i++;
}

The third method: the node attribute also acts as a loop, because there are 3 properties, only the first two properties, so you have to judge the condition

int i = 0;
for (xml_node<>* Seqnode = Seqsnode->first_node (); Seqnode! = NULL; seqnode = seqnode->next_sibling ())
{

for (xml_attribute<>* attrseq = Seqnode->first_attribute (); Attrseq->next_attribute () = NULL; AttrSeq = Attrseq->next_attribute ())//only Change 2 attribute
{
if (Bfirst)
{
Attrseq->value (Xmldoc.allocate_string (Vecseq[i].seqname.c_str ()));
Bfirst = false;
}
Else
{
Attrseq->value (Xmldoc.allocate_string (Vecseq[i].seqlicense.c_str ()));
Bfirst = true;
}
}
i++;
}

Using C + + to manipulate XML, mainly the use of internal loop methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.