c++rapidxmlの使用

18423 ワード

まずRapidXML公式サイトでソースをダウンロードし、rapidxmlをダウンロードします.hpp,rapidxml_iterators.hpp,rapidxml_print.hpp,rapidxml_utils.hppファイルは自分のプロジェクトにコピーすればいいです
1、xmlファイルの元のファイルを読み取る内容は以下の通りです.
<?xml version="1.0" encoding="UTF-8" ?>
<document>222222222
    <English name="name1" value="value">The world has many languages</English>
    <Heavy>changshuhang;</Heavy>
    <csh>Umlaut Element</csh>
    <English1 name="name11" value="value1">The world has many langquagesq</English>
    <English2 name="name12" value="val5ue">The world has many languagesq7</English>
    <English3 name="name13" value="valu23e">The world has many languages1</English>
    <English4 name="name14" value="val4ue">The world has many languages2</English>
    <English5 name="name15" value="val7ue">The world has many languages3</English>
    <English6 name="name17" value="va6lue">The world has many languages4</English>
    <English7 name="name18" value="val9ue">The world has many languages5</English>
    <English8 name="name19" value="val8ue">The world has many languages6</English>

    <phonebook>  
        <!--one item behalfs one contacted person.-->  
        <item>  
            <name>miaomaio</name>  
            <addr>Shaanxi Xi'an</addr>  
            <tel>13759911917</tel>  
            <email>[email protected]</email>  
            </item>  
        <item>  
            <name>gougou</name>  
            <addr>Liaoning Shenyang</addr>  
            <tel>15840330481</tel>  
            <email>[email protected]</email>  
        </item>  
        <!--more contacted persons.-->  
    </phonebook>

</document>

xmlファイルを読み出して表示
file<> fdoc("E://demo.xml");
    //std::cout << fdoc.data() << std::endl;

    xml_document<> doc;           // character type defaults to char
    doc.parse<0>(fdoc.data()); // 0 means default parse flags
    //cout << doc << endl;

    psln(doc.name());
    try {
        doc.parse<0>((char*)fdoc.data()); //        ,tmpbuf            
    }
    catch (rapidxml::parse_error &e) {
        err = "parse xml error. ";
        err += e.what();
        psln(err);
        while(1);
    }
    //  document
    xml_node<>* root = doc.first_node();
    psln(root->name());

visitorAll(root);

メソッドは、再帰的なメソッドを使用してすべて表示されます.
//     node  
void visitorAll(xml_node<>* root)
{
    xml_node<>* English = root->first_node();
    cout << "-----------------------" << endl;
    psln(English->name());
    //  node
    for (; English; English = English->next_sibling())
    {
        if (English->value_size() == NULL)
        {
            visitorAll(English);
        }
        else
        {
            cout << "####################" << endl;
            log(English->name(), English->value());

            xml_attribute<>* attribute = English->first_attribute();
            //  attr
            for (; attribute; attribute = attribute->next_attribute())
            {
                log(attribute->name(), attribute->value());
            }
        }

    }
}

2、xmlファイルを生成して保存する
//  xml     
void save(const char * path)
{
    xml_document<> document;
    xml_node<>* rot = document.allocate_node(rapidxml::node_pi, document.allocate_string("xml version='1.0' encoding='utf-8'"));
    document.append_node(rot);

    //     
    xml_node<>* root = document.allocate_node(node_element, "document", NULL);
    document.append_node(root);

    //    
    xml_node<>* English = document.allocate_node(node_element, "English", "The world has many languages");
    xml_attribute<>* att = document.allocate_attribute("name", "name1");
    English->append_attribute(att);
    xml_attribute<>* att1 = document.allocate_attribute("value", "value123");
    English->append_attribute(att1);
    root->append_node(English);

    //    array  
    xml_node<>* phonebook = document.allocate_node(node_element, "phonebook", NULL);
    for (int i = 0; i < 3; i++)
    {
        xml_node<>* item = document.allocate_node(node_element, "item", NULL);

        //name
        xml_node<>* name = document.allocate_node(node_element, "name", "miaomaio");
        item->append_node(name);

        //addr
        xml_node<>* addr = document.allocate_node(node_element, "addr", "Shaanxi Xi'an");
        item->append_node(addr);

        //tel
        xml_node<>* tel = document.allocate_node(node_element, "tel", "13759911917");
        item->append_node(tel);

        //email
        xml_node<>* email = document.allocate_node(node_element, "name", "[email protected]");
        item->append_node(email);

        phonebook->append_node(item);
    }
    root->append_node(phonebook);

    std::ofstream out(path);
    out << document;
}

生成されたスタイルは次のとおりです.
<?xml version='1.0' encoding='utf-8' ?>
<document>
    <English name="name1" value="value123">The world has many languages</English>
    <phonebook>
        <item>
            <name>miaomaio</name>
            <addr>Shaanxi Xi&apos;an</addr>
            <tel>13759911917</tel>
            <name>[email protected]</name>
        </item>
        <item>
            <name>miaomaio</name>
            <addr>Shaanxi Xi&apos;an</addr>
            <tel>13759911917</tel>
            <name>[email protected]</name>
        </item>
        <item>
            <name>miaomaio</name>
            <addr>Shaanxi Xi&apos;an</addr>
            <tel>13759911917</tel>
            <name>[email protected]</name>
        </item>
    </phonebook>
</document>


3、xmlファイルの修正
xml_node<>* delnode = root->first_node("English");

    xml_node<>* mynode = doc.allocate_node(node_element, "address", "  ");
    root->insert_node(delnode, mynode);
    root->remove_node(delnode);//   address