我想将用c ++编写的程序的输出显示为xml文件

时间:2010-09-01 07:40:51

标签: c++ xml

int countNodes( TreeNode *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()

输出........                                                                                                         ........... 像这样的事情

3 个答案:

答案 0 :(得分:2)

Tinyxml非常易于使用且免费,或者如果它是简单的XML布局,您可以自己编写。

http://www.grinninglizard.com/tinyxml

答案 1 :(得分:1)

XML文件只是具有特殊格式和控制字符的文本文件。

如果没有更多信息,您可以了解自己想要什么,或者您使用的c ++库或版本很难直接为您提供建议。

你是不是喜欢这样的事情?

<RootNode>
  <Node>
     <Property Name="Node1"/>
     <Node>
       <Property Name="Sub-node1">
     </Node>
  <Node/>
  <Node Name="Node2"/>
</RootNode>

答案 2 :(得分:1)

你的问题不清楚,所以我想你想“将数据输出到XML文件”

以XML格式将数据输出到文件流中可能类似于:

#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 ;
}

请阅读iostream帮助以获取有关fstream对象及其用途的更多信息:

http://www.cplusplus.com/reference/iostream/

现在,如果要解析和修改现有XML,则需要XML解析器。你可以谷歌那些解析器,例如:

http://www.google.com/search?q=XML+c%2B%2B+parser

修改

阅读评论后:

XML是一种有组织的文本文件。有点像HTML,如果是关于元素,属性和数据。例如,这是一个XML文件内容:

<?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 -->

有关详细信息,请阅读:

http://www.google.com/search?q=XML