PDA

View Full Version : Reading a XML and making a new one



Aashu10
9th December 2015, 12:34
I have a xml template like given below.


<Main_tag>
<SubTag1>
<Content_tag1> ... </Content_tag1>
<Content_tag1> ... </Content_tag1>
<Content_tag1> ... </Content_tag1>
<Content_tag1> ... </Content_tag1>
</SubTag1>
<SubTag2>

</SubTag2>
<SubTag3>

<SubTag3>
<SubTag4>
<Content_tag4> ... </Content_tag4>
<Content_tag4> ... </Content_tag4>
<Content_tag4> ... </Content_tag4>
<Content_tag4> ... </Content_tag4>
</Subtag4>
</Main_tag>
The program should make a copy(template will be read-only) and edit it according to the previous part of program. but basically a final copy should look somthing like.


<Main_tag>
<SubTag1>
<Content_tag1> ... </Content_tag1>
<Content_tag1> ... </Content_tag1>
<Content_tag1> ... </Content_tag1>
<Content_tag1> ... </Content_tag1>
</SubTag1>
<SubTag2>
<Content_tag2> ... </Content_tag2>
<Content_tag2> ... </Content_tag2>
</SubTag2>
<SubTag3>
<Content_tag3> ... </Content_tag3>
<Content_tag3> ... </Content_tag3>
<Content_tag3> ... </Content_tag3>
</SubTag3>
<SubTag4>
<Content_tag4> ... </Content_tag4>
<Content_tag4> ... </Content_tag4>
<Content_tag4> ... </Content_tag4>
<Content_tag4> ... </Content_tag4>
</Subtag4>
</Main_tag>
the tags and remains exactly the same, and the number of childs in and can vary according to previous part of the program. I am open to either DomDocument or XmlStreamReader. it should also make a attribute tab for every attribute like Content_tag2 or Content_tag3

I have a basic domdocument code, where the program opens the xml file and reads, but i am stuck at the part where the program should make a copy and add in attributes in correct header.


void XMLReader::ReadFromXMLFile()
{
QString filename="mypath";
File =new QFile(filename);

File->open(QIODevice::ReadOnly);

if (!File->open(QFile::ReadOnly | QFile::Text))
{
qDebug("Error: Cannot read file ");
}
QDomDocument dom;
dom.setContent(File);
QDomElement FirstElement = dom.documentElement();
QDomNode Node = FirstElement.firstChild();
while (!Node.isNull())
{
QDomElement Element = Node.toElement();
if(!Element.isNull())
{


Any help will be appriciated.