使用 TinyXml 将 XML 写入 txt 文件

发布于 2024-12-18 03:48:12 字数 1118 浏览 1 评论 0原文

我有一个简单的 XML 文件,但很大。假设

<products>
    <product_id>98667</product_id>
        <name>Hiking Boots</name>
            <price>34.99</price>

    <product_id>10123</product_id>
        <name>Work Gloves</name>
            <price>12.99</price>

    <product_id>15773</product_id>
        <name>Belt</name>
            <price>14.99</price>
</products>

我想将整个文件的数据(例如 500 个条目)写入制表符分隔的文本文件。为了更好地理解这个过程,假设我只想写产品名称和产品价格。我看不到典型的 TinyXml 教程在哪里涵盖了这种类型的编写。

我的代码:

void MyProducts::writeProducts(const char *pFilename) {
  TiXmlDocument doc(pFilename);
  bool loadOkay = doc.LoadFile();
  if (loadOkay) {
    cout<<"The file loaded"<<endl;
    cout<<pFilename<<endl;
    cout<<doc<<endl;
  }
  else {
    cout<<"Failed to load file \"%s\"\n"<< pFilename<<endl;
  }
} // end function

我不确定我是否想像普通文件一样逐行解析,或者这是否对这个 api 有任何好处。我可以打印 doc 的全部内容,这样我就知道文件正在加载。 任何帮助表示赞赏。我确信这只是知道要调用什么函数的问题。

I have a simple XML file but large. Let's say

<products>
    <product_id>98667</product_id>
        <name>Hiking Boots</name>
            <price>34.99</price>

    <product_id>10123</product_id>
        <name>Work Gloves</name>
            <price>12.99</price>

    <product_id>15773</product_id>
        <name>Belt</name>
            <price>14.99</price>
</products>

I want to write the data of the entire file(say 500 entries) to a tab delimited text file. To understand the process even better, lets say I only want to write the products name and products price. I could not see where the typical TinyXml tutorials were covering this type of write.

My code:

void MyProducts::writeProducts(const char *pFilename) {
  TiXmlDocument doc(pFilename);
  bool loadOkay = doc.LoadFile();
  if (loadOkay) {
    cout<<"The file loaded"<<endl;
    cout<<pFilename<<endl;
    cout<<doc<<endl;
  }
  else {
    cout<<"Failed to load file \"%s\"\n"<< pFilename<<endl;
  }
} // end function

I'm not sure if I want to parse line by line like a normal file or if that even does any good with this api. I can print the entire contents of doc so I know the file is loading.
Any help is appreciated. I'm sure its just a matter of knowing what function calls to make.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑饮青盏花 2024-12-25 03:48:12

我首先会浏览他们的教程。它展示了一些基本的 XML 写入和读取。

上面的注释是正确的...您将加载 XML 文档(如代码示例中所示),然后遍历 XML。

查看文档,由于您已经加载了 XMLDocument,因此您需要调用 RootElement 方法。这将返回一个 元素。您可以从元素中提取有关当前节点的信息。要“更深入”地进入树,您必须使用 TiXmlNode::IterateChildren< /a> 遍历根元素子元素。当你遍历树时,会返回一个 TiXmlNode。然后,您可以使用 Nodes 方法获取不同的类类型,从而提取出不同的信息。

I would first go through their tutorial. It shows some basic XML writing as well as reading.

The above comments are correct... You would load the XML document (as you have in your code example) and then traverse the XML.

Looking at the documentation, since you already have the XMLDocument loaded, you'd need to call the RootElement method. This returns an element. From an element you're able to pull out information about the current node. To go 'farther' into the tree, you'd have to use the TiXmlNode::IterateChildren to go through the root elements children. When you tranverse through the tree, a TiXmlNode is returned. You then use the Nodes methods to get different class types which will then pull out different information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文