0

I have two XML file.

first.xml

<?xml version="1.0" encoding="UTF-8"?>
<Average>
    <source unit="FP">
        <brand name="Add" abbreviation="addition" value="0">            
            <mask value="1" name="Integer"/>
            <description></description>
        </brand>
        <brand name="Sub" abbreviation="substraction" value="1">            
            <mask value="1" name="Integer"/>
            <description></description>
        </brand>
    </source>
</Average>

second.xml

<source unit="PE">
    <brand name="Mul" abbreviation="multiplication" value="0">            
            <mask value="1" name="Integer"/>
            <description></description>
    </brand>
    <brand name="Div" abbreviation="division" value="0">            
            <mask value="1" name="Integer"/>
            <description></description>
    </brand>
</source>

I want to append the content of second.xml file to first.xml file

Expected Output:

<?xml version="1.0" encoding="UTF-8"?>
<Average>
    <source unit="FP">
        <brand name="Add" abbreviation="addition" value="0">            
            <mask value="1" name="Integer"/>
            <description></description>
        </brand>
        <brand name="Sub" abbreviation="substraction" value="1">            
            <mask value="1" name="Integer"/>
            <description></description>
        </brand>
    </source>
    <source unit="PE">
    <brand name="Mul" abbreviation="multiplication" value="0">            
            <mask value="1" name="Integer"/>
            <description></description>
    </brand>
    <brand name="Div" abbreviation="division" value="0">            
            <mask value="1" name="Integer"/>
            <description></description>
    </brand>
</source>
</Average>

Here is what I was trying:

#include<iostream>
#include<string>
#include "tinyxml2.h"

void doAccess(const std::string& first, const std::string& second)
{
    std::string outFile = "output.xml";
    tinyxml2::XMLDocument doc;

    if (tinyxml2::XML_SUCCESS == doc.LoadFile(first.c_str()))
    {
        std::cout << "File is Present\n";
        doc.SaveFile(outFile.c_str());
    }

    if (tinyxml2::XML_SUCCESS == doc.LoadFile(second.c_str()))
    {
        std::cout << "File is Present\n";
        doc.SaveFile(outFile.c_str());
    }
}

int main()
{
    std::string first = "first.xml";
    std::string second = "second.xml";

    doAccess(first , second);

    return 0;
}

Any help would be highly appreciated.

Ratnesh
  • 1,554
  • 3
  • 12
  • 28
  • what are you getting as a result of your try? – Alessandro Teruzzi Feb 23 '21 at 12:42
  • @AlessandroTeruzzi, I am getting the output same as `second.xml`, the `first.xml` content is not present – Ratnesh Feb 23 '21 at 12:46
  • 2
    That's not how tinyxml works (or any other XML dom i've ever worked with). You need to open the first document, get the document root element (Average) open the second document in another DOM, and copy its root element (source) to the child list of the first document root you're already holding. – WhozCraig Feb 23 '21 at 12:57
  • When you call `doc.LoadFile(second.c_str())`, you probably override its previous contents. (This is what I would expect.) You have to load the two files into separate `doc`s and then to merge the contents of the 2nd into the first. Please, note that there can be only one top-level element in an XML doc. Hence, you have to merge contents of 2nd under the root node of the first (in your specific case). – Scheff's Cat Feb 23 '21 at 12:58

0 Answers0