i want to show the output of my programe written in c++ to a xml file
int countNodes( TreeNod开发者_如何学JAVAe *root ) {
// Count the nodes in the binary tree to which
// root points, and return the answer.
if ( root == NULL )
return 0; // The tree is empty. It contains no nodes.
else {
int count = 1; // Start by counting the root.
count += countNodes(root->left); // Add the number of nodes
// in the left subtree.
count += countNodes(root->right); // Add the number of nodes
// in the right subtree.
return count; // Return the total.
}
} // end countNodes()
output........ ........... some thing like this
Tinyxml is pretty easy to use and its free or if its a simple XML layout you can write your own.
http://www.grinninglizard.com/tinyxml
Your question is unclear, so I guess you want to "output data into an XML file"
Outputing data into a file stream in an XML format could be something like:
#include <iotream>
#include <fstream>
int main(int argc, char * argv[])
{
TreeNode * root = doWhataverGizmoYouWantToCreateThat() ;
int count = countNodes(root) ;
delete root ;
std::fstream output("output.xml", std::ios_base::out)
output << "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" ;
output << "<count value=\"" << count << "\" />\n" ;
return 0 ;
}
Please read the iostream help for more information about the fstream object and its uses:
http://www.cplusplus.com/reference/iostream/
Now, if you want to parse and modify an existing XML, you'll need an XML parser. You can google those parsers, for example with:
http://www.google.com/search?q=XML+c%2B%2B+parser
Edit:
After reading the comments :
XML is a kind of organized text file. Somewhat like HTML, if is about elements, attributes, and data. For example, this is an XML file content:
<?xml version="1.0" encoding="utf-8" ?>
<!-- This is a comment -->
<!-- The first line will declare the XML file, as well as
its version, and its encoding -->
<my_element>
<!-- this is an element. It can contain others elements,
as well as text data and attributes -->
<my_other_element my_attribute="some_value" />
<!-- my_other_element has an attribute whose name is
my_attribute, and whose value is some_value -->
<my_another_element>Some text value</my_another_element>
<!-- my_another_element has an attribute whose content
is the following text "Some text value" -->
<my_element>
<!-- this is the end of my_element, closing it -->
For more information, read :
http://www.google.com/search?q=XML
XML files are just text files with special formatting and control characters.
Without more information ragarding what exactly it is that you want or what c++ libraries or version that you're using it's difficult to advise you directly.
Are you after something like this for example?
<RootNode>
<Node>
<Property Name="Node1"/>
<Node>
<Property Name="Sub-node1">
</Node>
<Node/>
<Node Name="Node2"/>
</RootNode>
精彩评论