Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
xml file
-
hi,
how to display the content in xml file in a text browser in QT
-
@siva9111 What's wrong with https://doc.qt.io/qt-5/qtextbrowser.html#source-prop ?
-
HI SIR
how to display the content in xml file in a text browser in QT
in console application i gettingthe xml data in command prompt...but in widget application i dont know how to do ...please help me
my concern is printing xml data in textbrowser(ui) in qt
-
@siva9111 Did you read the link I posted? If you did - what exactly is the problem? If not - then please do, I'm not going to write the code for you...
-
''below program is in console application''
#include <QtCore>
#include <QtXml>
#include <QDebug>void listelements(QDomElement root,QString tagname,QString atttr)
{QDomNodeList items=root.elementsByTagName(tagname);
qDebug()<<"total items="<<items.count();
for(int i=0;i<items.count();i++)
{
QDomNode itemnode=items.at(i);
if(itemnode.isElement())
{
QDomElement itemle=itemnode.toElement();qDebug()<<itemle.attribute(atttr); }
}
}int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDomDocument document;QFile file("C:/Users/Administrator/Desktop/siva.xml");
if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
{
qDebug() << "failed to open";
return -1;
}
else
{
if(!document.setContent(&file))
{
qDebug() << "failed to load";
return -1;
}
file.close();
}
QDomElement root=document.firstChildElement();
listelements(root,"Book","Name");qDebug()<<"\r\n more\r\n";
QDomNodeList books=root.elementsByTagName("Book");
for(int i=0;i<books.count();i++)
{
QDomNode booknode=books.at(i);
if(booknode.isElement())
{
QDomElement book=booknode.toElement();
qDebug()<<"chapters"<<book.attribute("Name");
listelements(book,"chapter","Name");
}
}qDebug() << "finished";
return a.exec();
}this is the code
my xml file dtata is
<?xml version="1.0"?>
<Books>
<Book ID="0" Name = "my book 0">
<chapter ID="0" Name = "my chapter 0"/>
<chapter ID="1" Name = "my chapter 1"/>
<chapter ID="2" Name = "my chapter 2"/>
<chapter ID="3" Name = "my chapter 3"/>
</Book>
<Book ID="1" Name = "my book 1">
<chapter ID="0" Name = "my chapter 0"/>
<chapter ID="1" Name = "my chapter 1"/>
<chapter ID="2" Name = "my chapter 2"/>
<chapter ID="3" Name = "my chapter 3"/>
</Book>
</Books>i write this on console application
my concern is same data to be printed on text browser on qt
-
@siva9111 Come on - this is really not that hard. You just need to pass the URL to your file to QTextBrowser::setSource and it will load the content of your XML file...
To get the URL to your file use https://doc.qt.io/qt-5/qurl.html#fromLocalFile
-
hi ,
text files are loading but xml files are not loading by QUrl
-
xml files are not loading by QUrl
XML files are text files, they should load same way other files do.
Please show the code where you load the XML file in your text browser.
-
this is working :
QUrl url=QUrl::fromLocalFile("C:/Users/Administrator/Desktop/siva.txt");
ui.textBrowser->setSource(url);this not loading:
QUrl url=QUrl::fromLocalFile("C:/Users/Administrator/Desktop/xmlinfo.xml");
ui.textBrowser->setSource(url);
-
C:/Users/Administrator/Desktop/xmlinfo.xml
Does this file exist?
What does url.isValid() return?
-
file exist
-
@siva9111 What about my second question?
-
if(url.isValid()){
QMessageBox::information(0,"info",url.errorString());
}iam executed above code ....messagebox displayed
(due to new user iam unable to replying to you sir....its taking 10min time for next reply ...see ur inbox sir)
-
@siva9111 Does it work if you use https://doc.qt.io/qt-5/qtextedit.html#setPlainText instead of setSource?
-
does not work
-
does not work
Please show the code. setPlainText() should always work...