PDA

View Full Version : Reading values from XML



Srikanth.Togara
8th June 2016, 07:55
How can i read this XML file in to lineedits of qt designer



<studentform studentname="srkanth">
<studentform address="1-66-1"/>
<studentform ssc_marks="75%"/>
<studentform inter_marks="82%"/>
<studentform BTech_marks="65%"/>
<studentform mail_id="srikanth.togara@gmail.com"/>
</studentform>



And my code is below:


QDomDocument doc;
QFile file(ui.file->text());

if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
qDebug( "Failed to open file for reading." );
return;
}

QDomDocument document;
if( !document.setContent( &file ) )


file.close();

QDomElement documentElement = document.documentElement();

QDomNode node = documentElement.firstChild();
while( !node.isNull() )
{
if( node.isElement() )
{
QDomElement studentform = node.toElement();

ui.sri1->setText( studentform.tagName());
ui.sri1->setText( studentform.attribute("studentname"));

ui.sri2->setText( studentform.tagName());

ui.sri2->setText( studentform.attribute( "address" ));

ui.sri3->setText( studentform.attribute( "ssc_marks"));

ui.sri4->setText( studentform.attribute("inter_marks"));

ui.sri5->setText( studentform.attribute( "BTech_marks" ));

ui.sri6->setText( studentform.attribute( "mail_id"));
}

if( node.isText() )
{
QDomText text = node.toText();


}

node = node.nextSibling();

}

return ;




output is looks like this:

ui.sri1: srikanth
ui.sri2:
ui.sri3:
ui.sri4:
ui.sri5:
ui.sri6:




not getting data from lineedit 2 to 6 how can i fix this
please help me.

anda_skoa
8th June 2016, 19:33
Your XML is not actually valid XML.
Maybe post some of your actual data instead of trying to manually recreate something that you think looks like the actual data.

Cheers,
_

d_stranz
12th June 2016, 20:29
Your XML is not actually valid XML.

Well, it is valid XML, but it isn't good XML. What you have is an element "<studentform>" that contains 5 child "<studentform>" elements. Your code retrieves the parent (first "studentform" with the attribute "studentname"). That works. You then ask the same element for the values of its attributes "address", "ssc_marks", etc. which it does not have. These are attributes of the "studentform" children of the first node. So of course the GUI text fields are empty since the first node has no attributes by those names.

Your code then asks for the siblings of the top-level node. It doesn't have any, so the loop exits. So, your code is doing exactly what you told it to do.

You probably want your XML to look something like this:



<studentform
studentname="srkanth"
address="1-66-1"
ssc_marks="75%"
inter_marks="82%"
BTech_marks="65%"
mail_id="srikanth.togara@gmail.com"
/>


and you don't want to loop over siblings, because then each sibling will overwrite the information from the previous sibling on your form.

anda_skoa
13th June 2016, 09:05
Well, it is valid XML, but it isn't good XML.

Ah, indeed, I didn't see that the first studentform wasn't closed.
I thought that we didn't see the document element and that the </studentform> was an unmachted close-tag.

Still likely not the XML or the code that is actually being used, otherwise only "ui.sri6" would have a value, not "ui.sri1" as the poster claims.
The claimed output would require a document element of sorts around the outer studentform tag to achieve that with the posted code.

Cheers,
_

d_stranz
13th June 2016, 16:47
otherwise only "ui.sri6" would have a value, not "ui.sri1" as the poster claims.

Well, not quite :(

Presuming that the OP omitted the <?xml?> preamble and the document element, the C++ code as I read it says,

Line 23 - get the first child of the document element (which in this case is the entire "XML" fragment in the original post, the outer <studentform> element and the <studentform> children contained within)

Line 25 - set ui.sri1 to the tag name ("studentform"). I guess that didn't look right to the OP, so in the next line

Line 26 - set ui.sri1 to the value of the attribute "studentname". This works, because the current (outer) element has such an attribute.

Line 28 - set ui.sri2 to the tag name. That still doesn't look right I guess, so in the next line

Line 30 - set ui.sri2 to the value of the attribute "address". Since the code is still looking at the outer element, it doesn't have any such attribute, so the returned string is empty.

Lines 32, 34, 36, and 38 - Likewise, no such attributes in the outer element, so the ui.sriN fields are empty.

Lines 41 - 46 - just for fun, if the node is a text node, get the text and then ignore it.

Line 48 - get the next sibling of the current node. Since there is only one top-level <studentform> node, there is no sibling and the loop exits. As I said previously, if there was a sibling, the code would then proceed to overwrite everything from the previous iteration, with the end result that only the final <studentform> sibling's values would appear in the UI.

anda_skoa
13th June 2016, 17:17
Presuming that the OP omitted the <?xml?> preamble and the document element, the C++ code as I read it says,

Well, true, under the assumption that there is a document element around the outer studentform.

Which makes it even more important to get some info on the actual data.

Which is unlikely to happen since the thread starter didn't respond to the first request in almost as week, so likely is not coming back at all.

Cheers,
_

d_stranz
14th June 2016, 03:25
so likely is not coming back at all

The deadline to turn in the homework assignment has passed already.