PDA

View Full Version : XML Dom parsing function



FoleyX90
13th May 2010, 05:46
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:

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.to Int()).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;
}

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. :)

FoleyX90
13th May 2010, 05:58
add the code:
node = node.firstChild(); right after line: 22. This will support early attributing. I.E. Family/@Foley/Foley[0]/@First if your XML looks like this:


<xml>
<Family Name="Foley">
<Foley First="Justin">Justin</Foley>
<Foley First="Ashley">asdf</Foley>
<Foley>Gavin</Foley>
</Family>
</xml>

Lykurg
13th May 2010, 08:17
Did you notice QXmlQuery? It makes exactly what your custom function does...

FoleyX90
13th May 2010, 16:25
No, I did not. Thank you for enlightening me.

UPDATED CODE:


QString XmlUtils::GetData(QString path)
{
QDomDocument doc("myDocument");
QFile *xmlFile = new QFile("personaldata.xml");
doc.setContent(xmlFile);
QDomElement docElement = doc.documentElement();
QDomNode node;
node = docElement.firstChild();

QStringList pathFiles;
QString returnString;
pathFiles = path.split("/");

for(int i = 0; i < pathFiles.length(); i++)
{
QString curpath = pathFiles[i];
if(curpath.startsWith('@'))
{
curpath = curpath.remove('@');
QString rightNode = pathFiles[i-1];
if(rightNode.contains('['))
{
QStringList rightNodeChildren = rightNode.split('[');
rightNode = rightNodeChildren[0];
}
if(node.nodeName() != rightNode)
node = node.parentNode();
return 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(unsigned int x = 0; x < node.parentNode().childNodes().length(); x++)
{
if(node.nodeName() == subNode)
{
if(pathFiles[i] == pathFiles.back())
{
if(node.parentNode().childNodes().item(subIndex.to Int()).nodeName() == subNode)
// return node.parentNode().childNodes().item(subIndex.toInt ()).nodeName();
returnString = node.parentNode().childNodes().item(subIndex.toInt ()).firstChild().nodeValue();
break;
}
else
node = node.parentNode().childNodes().item(subIndex.toInt ());
}
}
if(node.hasChildNodes())
node = node.firstChild();

}
else
{
for(unsigned 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();
}
if(node.hasChildNodes())
node = node.firstChild();
}
}

}
return returnString;
}