28 #include <boost/regex.hpp>
30 XERCES_CPP_NAMESPACE_USE
38 XMLCh*
X(
string input) {
40 const char* xInput = input.c_str();
41 return XMLString::transcode(xInput);
51 void addRoot(DOMDocument* &doc,
string rootName,
string rootContent) {
54 if (!doc->getFirstChild()) {
57 doc = doc->getImplementation()->createDocument(0,
X(rootName), 0);
60 DOMText* txtNode = doc->createTextNode(
X(rootContent));
61 doc->getFirstChild()->appendChild(txtNode);
64 cout <<
"Created root '" << rootName <<
"'";
66 if (rootContent.empty()) {
69 cout <<
" with content '" << rootContent <<
"'." << endl;
73 cout <<
"Root already exists! Do not use 'null' as parent of new node." << endl;
85 void renameNode(DOMDocument* &doc,
string selector,
string oldNameOrParent,
string newNameOrAtt,
string newContentOrNewAtt) {
88 DOMNodeList * nodeList = doc->getElementsByTagName(
X(oldNameOrParent));
91 if (nodeList->getLength() != 0) {
92 DOMNode * oldNode = nodeList->item(0);
94 if (selector ==
"element") {
97 doc->renameNode(oldNode, 0,
X(newNameOrAtt));
100 if (newContentOrNewAtt.empty()) {
101 const XMLCh* xNewContentOrVal = oldNode->getTextContent();
102 oldNode->setTextContent(xNewContentOrVal);
105 oldNode->setTextContent(
X(newContentOrNewAtt));
110 cout <<
"Renamed the element '" << oldNameOrParent <<
"' to '" << newNameOrAtt <<
"'";
111 if (newContentOrNewAtt.empty()) {
114 cout <<
" with the new content '" << newContentOrNewAtt <<
"'." << endl;
118 }
else if (selector ==
"attribute") {
120 if (!oldNode->hasAttributes()) {
121 cout <<
"The element '" << oldNameOrParent <<
"' does not have any attributes." << endl;
125 DOMNamedNodeMap * attributes = oldNode->getAttributes();
126 if (DOMNode * attNode = attributes->getNamedItem(
X(newNameOrAtt))) {
129 doc->renameNode(attNode, 0,
X(newContentOrNewAtt));
130 cout <<
"Please enter a value for the new attribute: " << endl;
134 attNode->setTextContent(
X(input));
137 cout <<
"Attribute '" << newNameOrAtt <<
"' belonging to '" << oldNameOrParent
138 <<
"' renamed to '" << newContentOrNewAtt <<
"' with the new value '"
139 << input <<
"'." << endl;
143 cout <<
"The element '" << oldNameOrParent <<
"' does not have an attribute named '" << newNameOrAtt <<
"'. " << endl;
149 cout <<
"No matching element named '" << oldNameOrParent <<
"' found." << endl;
161 void addNode(DOMDocument* &doc,
string addType,
string parentName,
string childOrAtt,
string contentOrVal) {
164 DOMNodeList * nodeList = doc->getElementsByTagName(
X(parentName));
165 if (nodeList->getLength() != 0) {
166 DOMNode * parentNode = nodeList->item(0);
169 DOMElement * parentElem =
dynamic_cast<DOMElement*
> (parentNode);
172 if (addType ==
"element") {
174 DOMElement* childElem = doc->createElement(
X(childOrAtt));
175 parentElem->appendChild(childElem);
177 DOMText* txtNode = doc->createTextNode(
X(contentOrVal));
178 childElem->appendChild(txtNode);
182 cout <<
"Added the element '" << childOrAtt <<
"' to parent '" << parentName <<
"'";
183 if (contentOrVal.empty()) {
186 cout <<
" with the new content '" << contentOrVal <<
"'." << endl;
190 }
else if (addType ==
"attribute") {
192 parentElem->setAttribute(
X(childOrAtt),
X(contentOrVal));
195 cout <<
"Added the attribute '" << childOrAtt <<
"' to parent '" << parentName <<
"'";
196 if (contentOrVal.empty()) {
199 cout <<
" with the new value '" << contentOrVal <<
"'." << endl;
205 cout <<
"ERROR" << endl;
210 cout <<
"No matching node named '" << parentName <<
"' found." << endl;
213 if (!doc->getFirstChild()) {
215 cout <<
"No root has been created. Use the 'add element' command with the parent 'null' to create a root." << endl;
226 void removeNode(DOMDocument * &doc,
string removeType,
string parentName,
string childOrAtt) {
228 if (parentName ==
"null") {
230 if (removeType ==
"element") {
233 DOMNode * node = doc->getFirstChild();
234 DOMElement * rootNode =
dynamic_cast<DOMElement*
> (node);
237 if (XMLString::transcode(rootNode->getTagName()) == childOrAtt) {
240 doc->removeChild(rootNode);
241 cout <<
"Removed root node '" << childOrAtt <<
"'. Tree is now empty." << endl;
245 cout <<
"'" << childOrAtt <<
"' is not the name of the root." << endl;
250 cout <<
"When using the remove command with the parent 'null, only "
251 "the 'element' selector may be used." << endl;
256 DOMNodeList * nodeList = doc->getElementsByTagName(
X(parentName));
258 if (nodeList->getLength() != 0) {
260 DOMNode * parentNode = nodeList->item(0);
261 DOMElement * parentElem =
dynamic_cast<DOMElement*
> (parentNode);
263 if (removeType ==
"element") {
265 nodeList = parentElem->getElementsByTagName(
X(childOrAtt));
266 if (nodeList->getLength() != 0) {
269 DOMNode * childNode = nodeList->item(0);
270 parentElem->removeChild(childNode);
272 cout <<
"Child element '" << childOrAtt <<
"' belonging to '"
273 << parentName <<
"' has been removed." << endl;
277 cout <<
"No matching child named '" << childOrAtt <<
"' found for the parent '"
278 << parentName <<
"'. Please try again." << endl;
281 }
else if (removeType ==
"attribute") {
284 DOMNamedNodeMap * nodeMap = parentElem->getAttributes();
285 if (nodeMap->getNamedItem(
X(childOrAtt))) {
288 parentElem->removeAttribute(
X(childOrAtt));
290 cout <<
"The attribute '" << childOrAtt <<
"' belonging to parent '"
291 << parentName <<
"' has been removed." << endl;
295 cout <<
"The element '" << parentName <<
"' does not have an attribute named '" << childOrAtt <<
"' ." << endl;
300 cout <<
"ERROR" << endl;
305 cout <<
"No matching node named '" << parentName <<
"' found. Please try again." << endl;
void renameNode(DOMDocument *&doc, string selector, string oldNameOrParent, string newNameOrAtt, string newContentOrNewAtt)
void removeNode(DOMDocument *&doc, string removeType, string parentName, string childOrAtt)
void addNode(DOMDocument *&doc, string addType, string parentName, string childOrAtt, string contentOrVal)
void addRoot(DOMDocument *&doc, string rootName, string rootContent)