如何在MongoDB中存储XML格式的数据?

我有几个XML格式的文件。 每个文件包含XML头和约15-20项。 我想将这些文件存储在MongoDB中,我认为这些文件以JSON格式存储数据。 这里是我想要存储在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操作。

编辑:存储数据的过程就像是有一个表单,你select一个类别(频道名称,如频道标题),你只需要添加一个<item>该频道。 但是整个数据将存储在MongoDB中。

在mongodb中没有xmltypes。 MongoDB内部的每个东西都是BSON。

您可以保存为stringforms。

  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); 

这是string。 现在你可以把它保存在mongodb中。

如果数据来自外部是一些string或任何其他types,唯一的办法是转换为string,然后获取它,当你想要的。 你会得到你想要的相同的XMLstring。