28 #include <xercesc/framework/LocalFileFormatTarget.hpp>
31 #include <boost/regex.hpp>
33 XERCES_CPP_NAMESPACE_USE
44 if (fileName.empty()) {
45 cout <<
"No file name was given. Enter a file name for the tree." << endl;
46 getline(cin, fileName);
50 DOMImplementation * impl = doc->getImplementation();
52 DOMLSSerializer *theSerializer = ((DOMImplementationLS*) impl)->createLSSerializer();
55 DOMLSOutput *theOutputDesc = ((DOMImplementationLS*) impl)->createLSOutput();
58 fileName =
"dist/" + fileName +
".xml";
59 const char* xfileName = fileName.c_str();
60 XMLCh * filePath = XMLString::transcode(xfileName);
63 XMLFormatTarget *myFormTarget =
new LocalFileFormatTarget(XMLString::transcode(filePath));
64 theOutputDesc->setByteStream(myFormTarget);
68 DOMConfiguration* serializerConfig = theSerializer->getDomConfig();
69 serializerConfig->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint,
true);
70 serializerConfig->setParameter(XMLUni::fgDOMXMLDeclaration,
true);
73 theSerializer->write(doc, theOutputDesc);
76 theOutputDesc->release();
77 theSerializer->release();
78 myFormTarget->flush();
82 cout <<
"Exported the DOM tree to '" << fileName <<
"'. " << endl;
91 cout <<
"'help': Gives a list of all commands and their uses." << endl << endl;
93 cout <<
"'add' {element|attribute} parent_node {child_node|attribute_name} "
94 "{content|value} : Adds an element or attribute to the specified "
95 "parent node. Use the parent node 'null' to create a root." << endl << endl;
97 cout <<
"'remove' {element|attribute} parent_node {child_node|attribute_name}: "
98 "Removes the specified child node or attribute belonging to the specified"
99 " parent node. Use the parent node 'null' to remove the entire tree." << endl << endl;
101 cout <<
"'rename' {element|attribute} target_node {new_name|target_attribute}"
102 " {new_content|new_attribute}: Renames a specified node (or attribute "
103 "belonging to that node). If renaming an attribute the user will be "
104 "prompted for the new attribute value." << endl << endl;
106 cout <<
"'print' {list} {subtree-root}: Displays the current tree in an XML format or as a simple"
107 " list. Enter an optional existing node name to display a subtree from that node."
108 " Default format is XML." << endl << endl;
110 cout <<
"'export' {file_name}: Export the tree as an external XML file. " << endl << endl;
112 cout <<
"'quit': Ends the program." << endl << endl;
124 DOMImplementation * impl = doc->getImplementation();
127 DOMLSSerializer *theSerializer = ((DOMImplementationLS*) impl)->createLSSerializer();
130 DOMLSOutput *theOutputDesc = ((DOMImplementationLS*) impl)->createLSOutput();
131 XMLFormatTarget *myFormTarget =
new StdOutFormatTarget();
132 theOutputDesc->setByteStream(myFormTarget);
136 DOMConfiguration* serializerConfig = theSerializer->getDomConfig();
137 serializerConfig->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint,
true);
138 serializerConfig->setParameter(XMLUni::fgDOMXMLDeclaration,
false);
141 if (subRoot.empty()) {
143 theSerializer->write(doc, theOutputDesc);
148 const char* xInput = subRoot.c_str();
149 XMLCh* xSubRoot = XMLString::transcode(xInput);
150 DOMNodeList * nodeList = doc->getElementsByTagName(xSubRoot);
152 if (nodeList->getLength() != 0) {
155 DOMNode * subTreeRootNode = nodeList->item(0);
156 theSerializer->write(subTreeRootNode, theOutputDesc);
161 cout <<
"No matching node named '" << subRoot <<
"' found." << endl;
166 theOutputDesc->release();
167 theSerializer->release();
180 const XMLCh * tag = NULL;
183 for (XMLSize_t j = 0; j < attributes->getLength(); j++) {
184 DOMNode * nextAtt = attributes->item(j);
186 DOMAttr* att =
dynamic_cast<DOMAttr*
> (nextAtt);
187 tag = att->getName();
190 for (
int i = 0; i <= (level - 1); i++) {
194 cout <<
"Attribute " << (j + 1) <<
": ";
195 cout << XMLString::transcode(tag);
196 tag = att->getValue();
197 cout <<
" = " << XMLString::transcode(tag) << endl;
219 DOMElement* rootElem = NULL;
222 if (subRoot.empty()) {
223 rootElem = doc->getDocumentElement();
227 const char* xInput = subRoot.c_str();
228 XMLCh* xSubRoot = XMLString::transcode(xInput);
229 DOMNodeList * nodeList = doc->getElementsByTagName(xSubRoot);
231 if (nodeList->getLength() != 0) {
233 DOMNode * subTreeRootNode = nodeList->item(0);
234 rootElem =
dynamic_cast<DOMElement*
> (subTreeRootNode);
239 cout <<
"No matching node named '" << subRoot <<
"' found." << endl;
250 DOMTreeWalker *walker = doc->createTreeWalker(rootElem, DOMNodeFilter::SHOW_ALL, NULL,
true);
253 const XMLCh * tag = NULL;
255 DOMNode * node = walker->getRoot();
257 DOMElement* elemNode = NULL;
262 node = walker->getCurrentNode();
264 switch (node->getNodeType()) {
269 case DOMNode::ELEMENT_NODE:
271 for (
int i = 0; i < level; i++) {
277 if (node != rootElem) {
278 elemNode =
dynamic_cast<DOMElement*
> (node);
284 tag = elemNode->getTagName();
285 cout << XMLString::transcode(tag) <<
": ";
286 if (elemNode->getFirstChild()->getNodeType() != 3) {
294 case DOMNode::TEXT_NODE:
296 tag = node->getTextContent();
297 cout << XMLString::transcode(tag) << endl;
301 if (node->getParentNode()->hasAttributes()) {
302 DOMNamedNodeMap* attributes = node->getParentNode()->getAttributes();
309 cout <<
"ERROR!" << endl;
314 if (walker->getCurrentNode()->hasChildNodes()) {
315 walker->firstChild();
320 }
else while (walker->nextSibling() == NULL && walker->getCurrentNode() != rootElem) {
322 walker->parentNode();
327 }
while (walker->getCurrentNode() != rootElem);
void displayAttributes(DOMNamedNodeMap *attributes, int level)
void treeWalker(DOMDocument *doc, string subRoot)
void exportTree(DOMDocument *doc, string fileName)
void printTree(DOMDocument *doc, string subRoot)