K
K
Kamikaze10242021-03-31 15:57:52
C++ / C#
Kamikaze1024, 2021-03-31 15:57:52

How to modify xml file?

Good afternoon friends! How to modify xml file?
I have an XML file data.xml with the following content:

<?xml version="1.0" encoding="utf-8"?>
<Controls PageSize_Width="70">
  <PictureBox index="1" hashName="d6deadf9-d41a-4583-9d9b-95fae10baca0" Width="12,319" Height="12,319" Roate="0" />
  <PictureBox index="2" hashName="90df0e11-9dc3-4439-bb54-3e5a16da41a2" Width="12,319" Height="12,319" Roate="0"/>
</Controls>


There can be any number of PictureBox tags. I'm trying to change the value of pictureBox at index N with the
following C++ code, but an unmodified version is saved to a file. What am I doing wrong?

#include <iostream>
#include <string>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/foreach.hpp>
#include <boost/range/iterator_range.hpp>

//путь к файлу 
const std::string PRT_PATT_XML = "./data.xml";
const std::string MODIFE = "./modified.xml";

int main() {
    using boost::property_tree::ptree;
    ptree pt1;
    read_xml(PRT_PATT_XML, pt1);

    BOOST_FOREACH( const ptree::value_type & childTree, pt1.get_child("Controls") ) {
        std::string subtreeCaption = childTree.first;
        ptree sub_pt               = childTree.second;

        if (subtreeCaption == "PictureBox") {
            for(ptree::value_type &v : sub_pt.get_child("<xmlattr>")) {
                std::cout << "\t" << v.first << " = " << v.second.data() << std::endl;
                if(v.first == "hashName") {
                    v.second.put<std::string>((v.first), "ukrop");
                }
            }
        }
    }

    write_xml(MODIFE, pt1);

    return 0;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question