#include <xmltraversal.h>
Classes | |
| class | iterator |
Public Member Functions | |
| XMLTraversal (QString) | |
| XMLTraversal (QDomDocument) | |
| XMLTraversal (QByteArray) | |
| XMLTraversal (XMLTraversal &x) | |
| bool | isValid () const |
| bool | exists (const QString &path) const |
| QString | pwd () const |
| QDomNode | currentNode (const QString &path=QString()) const |
| QDomElement | currentElement () |
| QString | attribute (const QString &name) const |
| bool | hasAttribute (const QString &attrname) const |
| const QDomDocument & | document () const |
| QString | dump () const |
| QStringList | ls () const |
| iterator | begin () |
| iterator | end () |
| unsigned int | size () const |
| virtual bool | setDocument (QString doc) |
| virtual bool | setDocument (QByteArray doc) |
| virtual bool | setDocument (QDomDocument doc) |
| bool | cd (const QString &path=QString()) |
| bool | cd (const QDomElement &) |
| bool | cdToRoot () |
| QDomElement | mkdir (const QString &path) |
| bool | rmdir (const QString &path) |
| bool | mv (const QString &oldpath, const QString &newpath) |
| bool | cp (const QString &source, const QString &target) |
| bool | setAttribute (const QString &path, const QString &attrname, const QString &attrvalue) |
| bool | setAttribute (const QString &attrname, const QString &attrvalue) |
| void | push () |
| void | pop () |
| void | clearStack () |
| void | setText (QString s) |
| QString | text (QString path) |
| QDomElement | mkdirAndCd (QString s) |
| QDomElement | mkdirSetText (QString s, QString text) |
| bool | loadFromFile (QString s) |
| void | XmlSet (QString s, QVariant v) |
| void | XmlGet (QString s, QVariant &b) |
| void | XmlSet (QString s, QHostAddress a) |
| void | XmlGet (QString s, QHostAddress &b) |
| QVariant | GetVar (QString s) |
| void | initialise (QString s="Document") |
| QVariant | elementToVariant (QDomElement e) |
| void | VariantToElement (QVariant v, QDomElement e) |
| QDateTime | getUpdateTime () const |
| void | markUpdated () |
Private Member Functions | |
| QDomNode | cd_to_node (const QString &path) |
Private Attributes | |
| QStack< QDomNode > | savePoints |
| QDomDocument | doc_ |
| QDomNode | cur_path_node_ |
| QDateTime | updateTime |
QDomElement is the main node type used in this implementation, since it holds attributes, which was important for our requierements.
This class provides an iterator over dom elements of first sibling in a given folder. This iterator is STL conformant, and therefor, STL algorithms can be used.
Sorry but there's no name completion (like with the tab-tab under the unix-like bash shell), because it was too hard to implement ;-)
Example:
XMLTraversal xml ; xml.setDocument(...) ; assert(xml.is_valid()) ;
xml.cd("constants/remote/s11") ;
xml.cd("/config/user_path") ;
xml.cd("..") ;
xml.cd("...") ;
xml.rmdir("/constants/halfspace/enable") ;
std::cout << xml.pwd() << std::endl ;
xml.cd("/config/user_path") ;
QDomElement e = xml.currentNode().toElement() ;
std::cout << e.attribute("value") << std::endl ;
xml.cd("/constants/halfspace") ;
if (!xml.exist("depth"))
xml.mkdir("depth") ;
xml.mv("depth", "profondeur") ;
QStringList list = xml.ls() ;
for (QStringList::iterator it = list.begin(); it != list.end(); ++it) {
std::cerr << (*it) << std::endl ;
}
// Iterate over all chidren of the current directory
xml.cd("/") ;
XMLTraversal::iterator it = xml.begin() ;
for (; it != xml.end(); it++) {
std::cerr << (*it).tagName() << std::endl ;
}
| QString XMLTraversal::attribute | ( | const QString & | name | ) | const [inline] |
Get an attribute for the current node. This method assumes that the current node is a QDomElement. If not, a QString::null value is returned.
| XMLTraversal::iterator XMLTraversal::begin | ( | ) | [inline] |
Iterate over the children (first sibling) of the current node of this XMLTraversal. Get the first child.
Example:
XMLTraversal xml(mydoc) ; xml.cd("/root/tag1") ;
// Iterate over all chidren of "/root/tag1"
XMLTraversal::iterator it = xml.begin() ;
for (; it != xml.end(); it++) {
std::cerr << (*it).tagName() << std::endl ;
}
| bool XMLTraversal::cd | ( | const QDomElement & | e | ) |
| bool XMLTraversal::cd | ( | const QString & | path = QString() |
) |
To cd to a specific node. You can use:
cd("/constants/remote/s11") ;
cd("../..") ;
// Jump to the root:
cd() ;
cd("../../tools/remote/../halfspace") ;
| bool XMLTraversal::cp | ( | const QString & | source, | |
| const QString & | target | |||
| ) |
Copy a branch node into another node.
| source | the node to copy (with its children) | |
| target | the parent for the copy node |
XMLTraversal xml ; ...// init // copy remote to halfspace xml.cp("/constants/remote", "/constants/halfspace") ;
| QDomNode XMLTraversal::currentNode | ( | const QString & | path = QString() |
) | const |
Return the node at the current path or at a specified path of the document. If no path is provided, it return the node of the current path. Otherwise, it returns the node of the provided path, without changing the current path.
| path | the path to node or no argument. |
XMLTraversal xml ; ...// init xml.cd("constants/halfspace/depth") ; QDomNode node = xml.currentNode() ; if (node.isNull()) std::cerr << "invalid current path" << std::endl ;
node = xml.currentNode("/constants/remote/s11") ;
if (node.isNull())
std::cerr << "invalid path" << std::endl ;
| const QDomDocument & XMLTraversal::document | ( | ) | const [inline] |
Return the document making this XMLTraversal
| QString XMLTraversal::dump | ( | ) | const [inline] |
Convert to string for debugging purpose
| XMLTraversal::iterator XMLTraversal::end | ( | ) | [inline] |
Iterate over the children (first sibling) of the current node of this XMLTraversal. Get the end child (i.e. the following is true: QDomElement::isNull()).
| bool XMLTraversal::exists | ( | const QString & | path | ) | const [inline] |
Check if a path exists within the document
| bool XMLTraversal::hasAttribute | ( | const QString & | attrname | ) | const [inline] |
Tells if the current node has the attribute attrname. This method assumes that the current node is a QDomElement. If not, a false value is returned.
| bool XMLTraversal::isValid | ( | ) | const [inline] |
Check the validity of the XMLTraversal instance
| QStringList XMLTraversal::ls | ( | ) | const |
Return the list of the first child nodes of the current directory. Equivalent to the Unix command ls (but with no arguments).
Example:
XMLTraversal xml ; ...// init QStringList list = xml.ls() ; if (list.size()==0) std::cerr << "no child nodes." << std::endl ; else { std::cerr << "Printing all child nodes of the first sibling" << std::endl ; for (QStringList::iterator it=list.begin(); it!=list.end(); ++it) { std::cerr << (*it).latin1() << std::endl ; } }
| QDomElement XMLTraversal::mkdir | ( | const QString & | path | ) |
Create a new directory (a new node). The tail of the path is the name of the new node, and the remaining path have to be a valid path. You can only create one directory at a time.
XMLTraversal xml ; ...// init // Create the s23 node if (xml.mkdir("/constants/remote/s23")) std::cerr << "node successfuly created." << std::endl ; else std::cerr << "error while creating the node." << std::endl ;
| bool XMLTraversal::mv | ( | const QString & | oldpath, | |
| const QString & | newpath | |||
| ) |
Move or rename a node. Several possible commands:
| oldpath | the node to move or rename | |
| newpath | the new parent or the new name |
XMLTraversal xml ; ...// init // rename remote to limits xml.mv("remote", "limits") ; // move limits to another place xml.mv("limits", "../others") ;
| QString XMLTraversal::pwd | ( | ) | const |
Gives the current path within the XMLTraversal from the root (the document)
XMLTraversal xml ; ...// init std::cerr << xml.pwd() << std::endl ;
// print: /constants/halfspace/depth
| bool XMLTraversal::rmdir | ( | const QString & | path | ) |
Remove an existing directory (a node)
XMLTraversal xml ; ...// init if (xml.rmdir("/constants/remote")) std::cerr << "node successfuly removed." << std::endl ; else std::cerr << "error while removing the node." << std::endl ;
| bool XMLTraversal::setAttribute | ( | const QString & | attrname, | |
| const QString & | attrvalue | |||
| ) |
Shortcut to set an attribute for the current element.
| attrname | the name of the attribute | |
| attrvalue | the value of the attribute. |
| bool XMLTraversal::setAttribute | ( | const QString & | path, | |
| const QString & | attrname, | |||
| const QString & | attrvalue | |||
| ) |
Shortcut to set an attribute for an element.
| path | the path to the element | |
| attrname | the name of the attribute | |
| attrvalue | the value of the attribute. |
| bool XMLTraversal::setDocument | ( | QDomDocument | doc | ) | [virtual] |
The the new content of the XMLTraversal
| doc | the document |
| bool XMLTraversal::setDocument | ( | QString | doc | ) | [virtual] |
The the new content of the XMLTraversal
| doc | the stringified version of the document |
| unsigned int XMLTraversal::size | ( | ) | const [inline] |
Return the number of first sibling children