[Qt-interest] How do you find all the attributes XML?
Andrea Franceschini
therealmorpheu5 at gmail.com
Thu Mar 12 21:58:03 CET 2009
2009/3/12 Knapp <magick.crow at gmail.com>:
> Can see no way to take an unknown element and find out its attributes.
> What did I miss?
Though this is not at all the way to ask something with the intent of
getting help, I'll try to help anyway. I can only assume you're using
the DOM.
Suppose you have a piece of XML like this
<root>
<tag>
<othertag attribute="value" otherattribute="value"/>
<othertag attribute="value" otherattribute="value"/>
</tag>
</root>
In the first place, what I'd do is to obtain the root, obviously after
having opened and parsed the document, that is to say that you already
have a QDomDocument.
QDomDocument doc;
/* ... open, parse ...*/
// Warning, I'm not sure this would be appreciated by purists...
QDomNodeList otherTags = doc.elementsByTagName("othertag");
int i;
// I think that foreach can be used but I'm too lazy to check right
// now and I always had a reason for doing this way.
for(i = 0; i < otherTags.size(); i++) {
QDomElement otherTag = otherTags.at(i).toElement();
// Now you could just use otherTags.at(i).attributes() to have a
// QDomNamedNodeMap of attributes but I'd suggest this other
// way because QDomElement has
// attribute(const QString& name) that lets you refer to a particular
// attribute given its name.
qDebug() << otherTag.attribute("attribute") <<
otherTag.attribute("otherattribute");
}
Sweet, uhu? Once you have a QDomElement you can always reiterate with
elementsByTagName.
Now a question for the trolls: I know that
QDomDocument::elementsById(const QString&) is not implemented because
what the id attribute is unknown in XML but what part of the docs
didn't I read regarding DTD evaluation? If I'm not wrong, it is
possible to specify an attribute as unique id for a document, right?
So what?
--
Andrea
More information about the Qt-interest-old
mailing list