[Qt-interest] How do you find all the attributes XML?
Dan White
ygor at comcast.net
Fri Mar 13 01:05:16 CET 2009
If the attributes are variable, you would need to use the
"attributes" method of QDomElement
This returns a QDomNamedNodeMap which, in this case, is a dynamic
container of all the element's attributes.
It has a length/size method to tell you how many you have and then
you can just iterate thru them, accessing the name and value
On Mar 12, 2009, at 4:58 PM, Andrea Franceschini wrote:
> 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
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list