1 /****************************************************************************
4 Representation of an XML element with methods for getting/setting
5 attributes and generating "opening" and "closing" tags.
8 (C) 2004 by Paul Giannaros <ceruleanblaze@gmail.com>
10 Original author: Paul Giannaros <ceruleanblaze@gmail.com>
11 ******************************************************************************/
13 /***************************************************************************
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; version 2 of the License. *
19 ***************************************************************************/
21 #include "xmlelement.h"
25 XMLElement::XMLElement() {
27 m_attrmapper
= AttributeToValueMap();
29 XMLElement::XMLElement(const QString
&name
) {
31 m_attrmapper
= AttributeToValueMap();
34 XMLElement::~XMLElement() {
39 XMLElement::XMLElement(const XMLElement
&element
) {
40 m_attrmapper
= element
.m_attrmapper
;
41 m_name
= element
.m_name
;
44 /// Assignement operator
45 XMLElement
XMLElement::operator=(const XMLElement
&element
) {
46 m_attrmapper
= element
.m_attrmapper
;
47 m_name
= element
.m_name
;
51 QString
XMLElement::name() {
54 QString
XMLElement::startTag() {
55 QString output
= '<' + m_name
+ ' ';
56 for(AttributeToValueMap::Iterator it
= m_attrmapper
.begin(); it
!= m_attrmapper
.end(); ++it
) {
57 output
.append(it
.key() + "=\"" + it
.data() + "\" ");
59 output
= output
.left(output
.length() - 1);
60 // Get rid of the space at the end and then append a '>'
65 QString
XMLElement::endTag() {
66 return "</" + m_name
+ '>';
69 void XMLElement::setAttribute(const QString
&attr
, const QString
&value
) {
70 m_attrmapper
[attr
] = value
;
72 QString
XMLElement::attribute(const QString
&attr
) {
73 return m_attrmapper
[attr
];
76 QString
XMLElement::toQString() {
77 QString tag
= startTag();
78 return tag
.left(tag
.length() - 1).right(tag
.length() - 2);
81 XMLElement
XMLElement::fromQString(const QString
&str
) {
82 QStringList sections
= str
.split( " ");
83 QString tagname
= sections
[0];
84 XMLElement
e(tagname
.latin1());
87 // Loop over the remaining strings which are attributes="values"
88 if(sections
.count()) {
89 const int sectionsCount
= sections
.count();
90 for(int i
= 0; i
< sectionsCount
; ++i
) {
91 QStringList list
= sections
[i
].split( "=");
92 if(list
.count() != 2) {
93 std::cerr
<< "XMLElement::fromQString: Cannot convert list: " << list
.join("|") << ". `" << str
<< "' is not in valid format.\n";
94 return XMLElement(" ");
96 e
.setAttribute(list
[0], list
[1].left(list
[1].length() - 1).right(list
[1].length() -2));