【问题标题】:How to store XML format data in MongoDB? [closed]如何在 MongoDB 中存储 XML 格式的数据? [关闭]
【发布时间】:2017-11-16 12:17:38
【问题描述】:

我有几个 XML 格式的文件。每个文件包含 XML 标头和大约 15-20 项。我想将这些文件存储在我认为以 JSON 格式存储数据的 MongoDB 中。这是我要存储在 MongoDB 中的数据示例。

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">
    <channel>
        <title>Channel Title</title>
        <link>Channel link</link>
        <description>Channel desc</description>
        <!-- Item 1 -->
        <item>
          <title>Your Title</title>
                    <link>Your link</link>
          <pubDate>Published date</pubDate>
          <description>Your description</description>
        </item>
        <!-- Item 2 -->
        <item>
          <title>Your Title</title>
                    <link>Your link</link>
          <pubDate>Published date</pubDate>
          <description>Your description</description>
        </item>

    </channel>
</rss>

如何进行?谁能帮助使用一些示例和示例代码如何将这些数据存储在 MongoDB 中? 注意:我将使用 Node/Javascript 进行 CRUD 操作。

编辑: 存储数据的过程就像有一个表格,您选择一个类别(频道名称,如频道标题),然后将&lt;item&gt; 添加到该频道。但整个数据将存储在 MongoDB 中。

【问题讨论】:

  • 让你眼前一亮。
  • 你的代码在哪里?
  • 理解题有什么困惑吗?让我添加更多细节。

标签: javascript node.js xml mongodb


【解决方案1】:

mongodb中没有xml类型。在 mongodb 内部,每件事都是 BSON。

您可以保存为字符串形式。

    var str = '<?xml version="1.0" encoding="utf-8"?>\
    <rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"\
        xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">\
        <channel>\
            <title>Channel Title</title>\
            <link>Channel link</link>\
            <description>Channel desc</description>\
            <!-- Item 1 -->\
            <item>\
              <title>Your Title</title>\
                        <link>Your link</link>\
              <pubDate>Published date</pubDate>\
              <description>Your description</description>\
            </item>\
            <!-- Item 2 -->\
            <item>\
              <title>Your Title</title>\
                        <link>Your link</link>\
              <pubDate>Published date</pubDate>\
              <description>Your description</description>\
            </item>\
    \
        </channel>\
    </rss>'
  console.log(str);

这是字符串。现在你可以将它保存在 mongodb 中了。

如果来自外部的数据是某个字符串或任何其他类型,唯一的方法是转换为字符串,然后在需要时获取它。你会得到你想要的相同的 xml 字符串。

【讨论】:

  • 其实我是想把每个item按照channel name推送到MongoDB中。就像如果有两个频道DSA 和“CN”,那么我想在各自的类别中推送一个项目。例如,对于两个通道,将有两个 XML 文件,并且在每个 XML 文件中我想根据它们的通道名称添加项目。另外,你能提供一些关于如何在 MongoDB 和 javascript 中进行 CRUD 操作的链接吗?
  • 你在问什么是 xml 解析?对于 CRUD 操作,您可以在 node.js 中查看mongoosejs.com/docs。或者可以使用 npmjs.com/package/mongodb 。我用猫鼬。对于 mongodb 指南,除了其官方网站之外,没有其他最佳指南。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 1970-01-01
  • 2013-06-07
  • 2020-11-18
  • 2013-08-03
  • 2011-05-15
  • 1970-01-01
相关资源
最近更新 更多