Hi, everyone! I wanted to thank everybody for the help they've gave me while I've been on these forums. Now, it's my turn to contribute toward this community. I recently wrote a function to read from an XML file using a path. This supports attributes AND indexing. Here it is:
{
doc.setContent(xmlFile);
node = docElement.firstChild();
pathFiles = path.split("/");
int curindex = 0;
for(int i = 0; i < pathFiles.length(); i++)
{
if(curpath.startsWith('@'))
{
node = node.parentNode();
curpath = curpath.remove('@');
returnString = node.attributes().namedItem(curpath).nodeValue();
}
else
{
if(curpath.contains('['))
{
subIndex = subIndex.remove(']');
for(int x = 0; x < node.parentNode().childNodes().length(); x++)
{
if(node.nodeName() == subNode)
{
if(pathFiles[i] == pathFiles.back())
{
if(node.parentNode().childNodes().item(subIndex.toInt()).nodeName() == subNode)
returnString = node.parentNode().childNodes().item(subIndex.toInt()).firstChild().nodeValue();
break;
}
else
node = node.parentNode().childNodes().item(subIndex.toInt());
}
}
node = node.firstChild();
}
else
{
for(int x = 0; x < node.parentNode().childNodes().length(); x++)
{
if(node.nodeName() == pathFiles[i])
{
if(pathFiles[i] == pathFiles.back())
{
if(node.nodeName() == pathFiles[i])
returnString = node.firstChild().nodeValue();
break;
}
}
else
node = node.nextSibling();
}
node = node.firstChild();
}
}
}
return returnString;
}
QString XmlUtils::GetData(QString path)
{
QDomDocument doc("myDocument");
QFile *xmlFile = new QFile("file.xml");
doc.setContent(xmlFile);
QDomElement docElement = doc.documentElement();
QDomNode node;
node = docElement.firstChild();
QStringList pathFiles;
QString returnString;
pathFiles = path.split("/");
int curindex = 0;
for(int i = 0; i < pathFiles.length(); i++)
{
QString curpath = pathFiles[i];
if(curpath.startsWith('@'))
{
node = node.parentNode();
curpath = curpath.remove('@');
returnString = node.attributes().namedItem(curpath).nodeValue();
}
else
{
if(curpath.contains('['))
{
QStringList subPathFile = curpath.split('[');
QString subIndex = subPathFile[1];
subIndex = subIndex.remove(']');
QString subNode = subPathFile[0];
for(int x = 0; x < node.parentNode().childNodes().length(); x++)
{
if(node.nodeName() == subNode)
{
if(pathFiles[i] == pathFiles.back())
{
if(node.parentNode().childNodes().item(subIndex.toInt()).nodeName() == subNode)
returnString = node.parentNode().childNodes().item(subIndex.toInt()).firstChild().nodeValue();
break;
}
else
node = node.parentNode().childNodes().item(subIndex.toInt());
}
}
node = node.firstChild();
}
else
{
for(int x = 0; x < node.parentNode().childNodes().length(); x++)
{
if(node.nodeName() == pathFiles[i])
{
if(pathFiles[i] == pathFiles.back())
{
if(node.nodeName() == pathFiles[i])
returnString = node.firstChild().nodeValue();
break;
}
}
else
node = node.nextSibling();
}
node = node.firstChild();
}
}
}
return returnString;
}
To copy to clipboard, switch view to plain text mode
How to use it:
Say you have the xml file:
<xml>
<Family>
<Foley>Justin</Foley>
<Foley>Ashley</Foley>
<Foley>Gavin</Foley>
</Family>
</xml>
And you want to get the value of 'Ashley.'
What you would do is:
QString name = XmlUtils->GetData("Family/Foley[1]");
The variable, 'name' would now be index [1] of the nodeset, 'Foley'. Please, enjoy and let me know if anybody does not understand. Praise is also accepted.
Bookmarks