나는 xml에 그다지 좋지 않지만 기본 xml 파일은 이와 같이 보입니다.
<MAIN_HEADER>
<HEADER>
<TITLE>my_title</TITLE>
<AUTOR>DNL</AUTOR>
<NAME>John</NAME>
<AGE>abc</AGE>
<SEX>male</SEX>
<PLACE>abc</PLACE>
<INI_FILE>abc</INI_FILE>
</HEADER>
내가 원하는 것은 2-3 개의 태그 (예 : NAME & SEX)를 찾아 다른 변수에 속성 (John, Male)을 저장하는 것입니다.
지금까지 xml 파일을 읽을 수있었습니다.
void MainWindow::XMLParser()
{
QString path=MainWindow::getWorkingDirectory()+"\\0_Config\\";
QString string;
string = path + ui->ConfigFiles_combo->currentText(); \\THIS IS WHERE´IT DETERMINES WHICH XML FILE IT IS
qDebug()<<string;
QDomDocument document;
//load the file
QFile file(string);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug()<<"Failed to open the file";
}
else
{
if(!document.setContent(false))
{
qDebug()<<"Failed to load document";
}
file.close();
}
QDomElement root = document.firstChildElement();
qDebug()<<"finished";
}
정확한 태그를 검색하고 다른 변수에 저장하려면 어떻게해야합니까?
왜 setContent(false)
또 있어요? 방금 The Badger 의 코드를 복사 한 것 같습니다 . 이 시도.
void MainWindow::XMLParser()
{
// don't worry about path separator, Qt will take of it
QString string = MainWindow::getWorkingDirectory() + "/0_Config/" + ui->ConfigFiles_combo->currentText();
qDebug()<<string;
QDomDocument document;
//load the file
QFile xmlFile(string);
if (!xmlFile.exists() || !xmlFile.open(QFile::ReadOnly | QFile::Text)) {
qDebug() << "Check your file";
return;
}
QDomDocument domDocument;
domDocument.setContent(&xmlFile);
QDomElement topElement = domDocument.documentElement();
QDomNode domNode = topElement.firstChild();
while (!domNode.isNull()) {
QDomElement domElement = domNode.toElement();
if (!domElement.isNull()) {
//qDebug() << domElement.tagName();
if (domElement.tagName() == "HEADER") {
QDomNode node = domElement.firstChild();
while (!node.isNull()) {
QDomElement element = node.toElement();
if (!element.isNull()) {
const QString tagName(element.tagName());
if (tagName == "NAME") {
qDebug() << "Name is:" << element.text();
} else if (tagName == "SEX") {
qDebug() << "Sex is:" << element.text();
}
}
node = node.nextSibling();
}
}
}
domNode = domNode.nextSibling();
}
}
이것은 내 콘솔 출력입니다.
Name is: "John"
Sex is: "male"
Name is: "Doe"
Sex is: "male"
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다