PDA

View Full Version : QXmlStreamReader not giving all the attributes of an element



rawfool
19th October 2016, 09:30
Hi,
I'm trying to parse an xml file and when I try to read through the first element, I'm not getting all the corresponding attributes of that element. Only a few are getting noticed. Would be of great help if someone help me in finding the issue.

My xml file is

<nvd xmlns="http://scap.nist.gov/schema/feed/vulnerability/2.0" xmlns:cpe-lang="http://cpe.mitre.org/language/2.0" xmlns:vuln="http://scap.nist.gov/schema/vulnerability/0.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:scap-core="http://scap.nist.gov/schema/scap-core/0.1" xmlns:cvss="http://scap.nist.gov/schema/cvss-v2/0.2" xmlns:patch="http://scap.nist.gov/schema/patch/0.1" nvd_xml_version="2.0" xsi:schemaLocation="http://scap.nist.gov/schema/patch/0.1 http://nvd.nist.gov/schema/patch_0.1.xsd http://scap.nist.gov/schema/scap-core/0.1 http://nvd.nist.gov/schema/scap-core_0.1.xsd http://scap.nist.gov/schema/feed/vulnerability/2.0 http://nvd.nist.gov/schema/nvd-cve-feed_2.0.xsd" pub_date="2011-10-23T16:00:27">
<entry id="CVE-2016-4172">
<vuln:vulnerable-configuration id="http://nvd.nist.gov/">
<cpe-lang:logical-test operator="AND" negate="false">
<cpe-lang:logical-test operator="OR" negate="false">
<cpe-lang:fact-ref name="cpe:/a:adobe:flash_player:11.2.202.626" />
</cpe-lang:logical-test>
<cpe-lang:logical-test operator="OR" negate="false">
<cpe-lang:fact-ref name="cpe:/o:linux:linux_kernel" />
</cpe-lang:logical-test>
</cpe-lang:logical-test>
</vuln:vulnerable-configuration>
</entry>
</nvd>

My code to to find the attributes of an element -

#include <QCoreApplication>
#include <QDebug>
#include <QXmlStreamReader>
#include <QFile>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QFile file("C:/Users/shravan/Documents/rahul_prac/test.xml");
if(!file.open(QFile::ReadOnly)) { qDebug() << "Cannot read file" << file.errorString(); }

QXmlStreamReader xml(&file);
while(!xml.atEnd())
{
if(xml.isStartElement())
{
if(xml.name() == "nvd")
{
int count = xml.attributes().count();
for(int i = 0; i < count; i++)
{
qDebug() << xml.attributes().at(i).name().toString()
<< ": "
<< xml.attributes().at(i).value().toString();
qDebug() << "\n\n";
}
}
}
xml.readNextStartElement();
}

return a.exec();
}

My result is

"nvd_xml_version" : "2.0"

"schemaLocation" : "http://scap.nist.gov/schema/patch/0.1 http://nvd.nist.gov/schema/patch_0.1.xsd http://scap.nist.gov/schema/scap-core/0.1 http://nvd.nist.gov/schema/scap-core_0.1.xsd http://scap.nist.gov/schema/feed/vulnerability/2.0 http://nvd.nist.gov/schema/nvd-cve-feed_2.0.xsd"

"pub_date" : "2011-10-23T16:00:27"

There are more attributes in the xml than what I get in the result. I'm thinking what could possibly be wrong. Thanks.

Thanks :)

ChrisW67
19th October 2016, 12:03
You have all the attributes that are not related to declaring a namespace, i.e. All the attributes with names not starting "xmlns".

You can use namespaceDeclarations() to access the namespace information. Alternatively, you can disable namespace handling with a property of the QXmlStreamReader.