QDomElement Class
La classe QDomElement rappresenta un singolo elemento dell'albero DOM. Per saperne di più...
| Intestazione: | #include <QDomElement> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Xml) target_link_libraries(mytarget PRIVATE Qt6::Xml) |
| qmake: | QT += xml |
| Eredita da: | QDomNode |
- Elenco di tutti i membri, compresi quelli ereditati
- QDomElement fa parte delle classi XML.
Nota: Tutte le funzioni di questa classe sono rientranti.
Funzioni pubbliche
| QDomElement() | |
| QDomElement(const QDomElement &element) | |
| QString | attribute(const QString &name, const QString &defValue = QString()) const |
| QString | attributeNS(const QString &nsURI, const QString &localName, const QString &defValue = QString()) const |
| QDomAttr | attributeNode(const QString &name) |
| QDomAttr | attributeNodeNS(const QString &nsURI, const QString &localName) |
| QDomNamedNodeMap | attributes() const |
| QDomNodeList | elementsByTagName(const QString &tagname) const |
| QDomNodeList | elementsByTagNameNS(const QString &nsURI, const QString &localName) const |
| bool | hasAttribute(const QString &name) const |
| bool | hasAttributeNS(const QString &nsURI, const QString &localName) const |
| QDomNode::NodeType | nodeType() const |
| void | removeAttribute(const QString &name) |
| void | removeAttributeNS(const QString &nsURI, const QString &localName) |
| QDomAttr | removeAttributeNode(const QDomAttr &oldAttr) |
| void | setAttribute(const QString &name, const QString &value) |
| void | setAttribute(const QString &name, double value) |
| void | setAttribute(const QString &name, float value) |
| void | setAttribute(const QString &name, int value) |
| void | setAttribute(const QString &name, qlonglong value) |
| void | setAttribute(const QString &name, qulonglong value) |
| void | setAttribute(const QString &name, uint value) |
| void | setAttributeNS(const QString &nsURI, const QString &qName, const QString &value) |
| void | setAttributeNS(const QString &nsURI, const QString &qName, double value) |
| void | setAttributeNS(const QString &nsURI, const QString &qName, int value) |
| void | setAttributeNS(const QString &nsURI, const QString &qName, qlonglong value) |
| void | setAttributeNS(const QString &nsURI, const QString &qName, qulonglong value) |
| void | setAttributeNS(const QString &nsURI, const QString &qName, uint value) |
| QDomAttr | setAttributeNode(const QDomAttr &newAttr) |
| QDomAttr | setAttributeNodeNS(const QDomAttr &newAttr) |
| void | setTagName(const QString &name) |
| QString | tagName() const |
| QString | text() const |
| QDomElement & | operator=(const QDomElement &other) |
Descrizione dettagliata
Gli elementi hanno un nome ( tagName()) e zero o più attributi ad essi associati. Il nome del tag può essere modificato con la funzione setTagName().
Gli attributi degli elementi sono rappresentati da oggetti ` QDomAttr `, che possono essere interrogati utilizzando le funzioni ` attribute()` e ` attributeNode()`. È possibile impostare gli attributi con le funzioni ` setAttribute()` e ` setAttributeNode()`. Gli attributi possono essere rimossi con ` removeAttribute()`. Esistono equivalenti di queste funzioni che tengono conto dello spazio dei nomi, ovvero ` setAttributeNS()`, ` setAttributeNodeNS()` e ` removeAttributeNS()`.
Se si desidera accedere al testo di un nodo, utilizzare ` text()`, ad esempio
QDomElement e = //...
//...
QString s = e.text()La funzione ` text()` opera in modo ricorsivo per individuare il testo (poiché non tutti gli elementi contengono testo). Se si desidera individuare tutto il testo presente in tutti i figli di un nodo, è necessario iterare sui figli alla ricerca di nodi ` QDomText `, ad esempio
QString text;
QDomElement element = doc.documentElement();
for(QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())
{
QDomText t = n.toText();
if (!t.isNull())
text += t.data();
}Si noti che si cerca di convertire ogni nodo in un nodo di testo e si utilizza ` text()` anziché ` firstChild()`. Non si deve utilizzare direttamente `toText().data()` o `n.toText().data()` sul nodo, poiché il nodo potrebbe non essere un elemento di testo.
È possibile ottenere un elenco di tutti i discendenti di un elemento che hanno un nome di tag specificato con ` elementsByTagName()` o ` elementsByTagNameNS()`.
Per scorrere gli elementi di un documento DOM, utilizzare firstChildElement(), lastChildElement(), nextSiblingElement() e previousSiblingElement(). Ad esempio, per iterare su tutti gli elementi figli denominati "entry" all'interno di un elemento radice denominato "database", è possibile utilizzare:
QDomDocument doc = // ...
QDomElement root = doc.firstChildElement("database");
QDomElement elt = root.firstChildElement("entry");
for (; !elt.isNull(); elt = elt.nextSiblingElement("entry")) {
// ...
}Per ulteriori informazioni sul Document Object Model, consultare i livelli 1 e 2 Core. Per un'introduzione più generale all'implementazione del DOM, consultare la documentazione relativa a QDomDocument.
Documentazione sulle funzioni membro
QDomElement::QDomElement()
Crea un elemento vuoto. Utilizza la funzione QDomDocument::createElement() per creare elementi con contenuto.
QDomElement::QDomElement(const QDomElement &element)
Crea una copia di element.
I dati della copia sono condivisi (copia superficiale): la modifica di un nodo comporta la modifica anche dell'altro. Se si desidera effettuare una copia profonda, utilizzare cloneNode().
QString QDomElement::attribute(const QString &name, const QString &defValue = QString()) const
Restituisce l'attributo denominato name. Se l'attributo non esiste, viene restituito defValue.
Vedi anche setAttribute(), attributeNode(), setAttributeNode() e attributeNS().
QString QDomElement::attributeNS(const QString &nsURI, const QString &localName, const QString &defValue = QString()) const
Restituisce l'attributo con il nome locale localName e l'URI dello spazio dei nomi nsURI. Se l'attributo non esiste, viene restituito defValue.
Vedi anche setAttributeNS(), attributeNodeNS(), setAttributeNodeNS() e attribute().
QDomAttr QDomElement::attributeNode(const QString &name)
Restituisce l'oggetto ` QDomAttr ` corrispondente all'attributo denominato ` name`. Se tale attributo non esiste, viene restituito un ` null attribute `.
Vedi anche setAttributeNode(), attribute(), setAttribute() e attributeNodeNS().
QDomAttr QDomElement::attributeNodeNS(const QString &nsURI, const QString &localName)
Restituisce l'oggetto ` QDomAttr ` corrispondente all'attributo con nome locale ` localName ` e URI dello spazio dei nomi ` nsURI`. Se tale attributo non esiste, viene restituito un oggetto ` null attribute `.
Vedi anche setAttributeNodeNS(), setAttributeNode(), attribute() e setAttribute().
QDomNamedNodeMap QDomElement::attributes() const
Restituisce un oggetto ` QDomNamedNodeMap ` contenente tutti gli attributi di questo elemento.
Vedi anche attribute(), setAttribute(), attributeNode() e setAttributeNode().
QDomNodeList QDomElement::elementsByTagName(const QString &tagname) const
Restituisce un oggetto ` QDomNodeList ` contenente tutti i discendenti di questo elemento denominati ` tagname ` incontrati durante una traversata in preordine del sottoalbero dell'elemento con questo elemento come radice. L'ordine degli elementi nell'elenco restituito corrisponde all'ordine in cui vengono incontrati durante la traversata in preordine.
Vedi anche elementsByTagNameNS() e QDomDocument::elementsByTagName().
QDomNodeList QDomElement::elementsByTagNameNS(const QString &nsURI, const QString &localName) const
Restituisce un oggetto ` QDomNodeList ` contenente tutti i discendenti di questo elemento con nome locale localName e URI dello spazio dei nomi nsURI individuati durante una traversata in preordine del sottoalbero dell'elemento avente come radice questo elemento. L'ordine degli elementi nell'elenco restituito corrisponde all'ordine in cui vengono individuati durante la traversata in preordine.
Vedi anche elementsByTagName() e QDomDocument::elementsByTagNameNS().
bool QDomElement::hasAttribute(const QString &name) const
Restituisce ` true ` se questo elemento possiede un attributo denominato ` name`; in caso contrario, restituisce ` false`.
Nota: questa funzione non tiene conto della presenza di spazi dei nomi. Di conseguenza, il nome specificato verrà confrontato con nomi di attributi pienamente qualificati che includono eventuali prefissi di spazio dei nomi presenti.
Utilizzare ` hasAttributeNS()` per verificare esplicitamente la presenza di attributi con spazi dei nomi e nomi specifici.
bool QDomElement::hasAttributeNS(const QString &nsURI, const QString &localName) const
Restituisce " true " se questo elemento presenta un attributo con il nome locale " localName " e l'URI dello spazio dei nomi " nsURI"; in caso contrario, restituisce "false".
QDomNode::NodeType QDomElement::nodeType() const
Restituisce ` ElementNode`.
void QDomElement::removeAttribute(const QString &name)
Rimuove l'attributo denominato “name” name da questo elemento.
Vedi anche setAttribute(), attribute() e removeAttributeNS().
void QDomElement::removeAttributeNS(const QString &nsURI, const QString &localName)
Rimuove l'attributo con il nome locale localName e l'URI dello spazio dei nomi nsURI da questo elemento.
Vedi anche setAttributeNS(), attributeNS() e removeAttribute().
QDomAttr QDomElement::removeAttributeNode(const QDomAttr &oldAttr)
Rimuove l'attributo oldAttr dall'elemento e lo restituisce.
Vedi anche attributeNode() e setAttributeNode().
void QDomElement::setAttribute(const QString &name, const QString &value)
Aggiunge un attributo denominato name con valore value. Se esiste già un attributo con lo stesso nome, il suo valore viene sostituito con value.
Vedi anche attribute(), setAttributeNode() e setAttributeNS().
void QDomElement::setAttribute(const QString &name, double value)
La formattazione utilizza sempre QLocale::C.
Si tratta di una funzione sovraccaricata.
void QDomElement::setAttribute(const QString &name, float value)
La formattazione utilizza sempre QLocale::C.
Si tratta di una funzione sovraccaricata.
void QDomElement::setAttribute(const QString &name, int value)
La formattazione utilizza sempre QLocale::C.
Si tratta di una funzione sovraccaricata.
void QDomElement::setAttribute(const QString &name, qlonglong value)
La formattazione utilizza sempre QLocale::C.
Si tratta di una funzione sovraccaricata.
void QDomElement::setAttribute(const QString &name, qulonglong value)
La formattazione utilizza sempre QLocale::C.
Si tratta di una funzione sovraccaricata.
void QDomElement::setAttribute(const QString &name, uint value)
La formattazione utilizza sempre QLocale::C.
Si tratta di una funzione sovraccaricata.
void QDomElement::setAttributeNS(const QString &nsURI, const QString &qName, const QString &value)
Aggiunge un attributo con il nome qualificato qName e l'URI dello spazio dei nomi nsURI con il valore value. Se esiste un attributo con lo stesso nome locale e lo stesso URI dello spazio dei nomi, il suo prefisso viene sostituito dal prefisso qName e il suo valore viene sostituito da value.
Sebbene qName sia il nome qualificato, per decidere se il valore di un attributo esistente debba essere sostituito viene utilizzato il nome locale.
Vedi anche attributeNS(), setAttributeNodeNS() e setAttribute().
void QDomElement::setAttributeNS(const QString &nsURI, const QString &qName, double value)
Questa è una funzione sovraccaricata.
void QDomElement::setAttributeNS(const QString &nsURI, const QString &qName, int value)
Questa è una funzione sovraccaricata.
void QDomElement::setAttributeNS(const QString &nsURI, const QString &qName, qlonglong value)
Questa è una funzione sovraccaricata.
void QDomElement::setAttributeNS(const QString &nsURI, const QString &qName, qulonglong value)
Questa è una funzione sovraccaricata.
void QDomElement::setAttributeNS(const QString &nsURI, const QString &qName, uint value)
Questa è una funzione sovraccaricata.
QDomAttr QDomElement::setAttributeNode(const QDomAttr &newAttr)
Aggiunge l'attributo newAttr a questo elemento.
Se l'elemento possiede un altro attributo con lo stesso nome di ` newAttr`, questa funzione sostituisce tale attributo e lo restituisce; in caso contrario, la funzione restituisce un oggetto ` null attribute`.
Vedi anche attributeNode(), setAttribute() e setAttributeNodeNS().
QDomAttr QDomElement::setAttributeNodeNS(const QDomAttr &newAttr)
Aggiunge l'attributo newAttr a questo elemento.
Se l'elemento possiede un altro attributo con lo stesso nome locale e lo stesso URI dello spazio dei nomi di ` newAttr`, questa funzione sostituisce tale attributo e lo restituisce; in caso contrario, la funzione restituisce un ` null attribute`.
Vedi anche attributeNodeNS(), setAttributeNS() e setAttributeNode().
void QDomElement::setTagName(const QString &name)
Imposta il nome del tag di questo elemento su name.
Vedi anche tagName().
QString QDomElement::tagName() const
Restituisce il nome del tag di questo elemento. Per un elemento XML come questo:
<img src="myimg.png">il nome del tag restituirebbe "img".
Vedi anche setTagName().
QString QDomElement::text() const
Restituisce il testo dell'elemento o una stringa vuota.
Esempio:
<h1>Hello <b>Qt</b> <![CDATA[<xml is cool>]]></h1>La funzione text() dell'QDomElement e per il tag <h1> restituirà il seguente testo:
Hello Qt <xml is cool>I commenti vengono ignorati da questa funzione. Essa valuta solo oggetti di tipo QDomText e QDomCDATASection.
QDomElement &QDomElement::operator=(const QDomElement &other)
Assegna ` other ` a questo elemento DOM.
I dati della copia sono condivisi (copia superficiale): la modifica di un nodo comporterà la modifica anche dell'altro. Se si desidera effettuare una copia profonda, utilizzare cloneNode().
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.