2017-11-16 1 views
1

XML 형식의 파일이 여러 개 있습니다. 각 파일에는 XML 헤더와 약 15-20 개의 항목이 있습니다. 이 파일을 MongoDB에 저장하려고합니다.이 파일은 JSON 형식으로 저장됩니다. 다음은 MongoDB에 저장하고자하는 데이터 샘플입니다.MongoDB에 XML 형식 데이터를 저장하는 방법은 무엇입니까?

<?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에 이러한 데이터를 저장하는 방법을 몇 가지 예제와 샘플 코드를 사용하여 도움을 줄 수 있습니까? 참고 : CRUD 작업에는 Node/Javascript가 사용됩니다.

편집 : 이 형태이며 (채널 제목처럼, 채널 이름) 카테고리를 선택하고 방금 그 채널에 <item>를 추가 같은 것 데이터를 저장하는 과정. 그러나 전체 데이터는 MongoDB에 저장됩니다.

+0

당신에게 장면을 만듭니다. –

+0

코드는 어디에 있습니까? –

+0

질문을 이해하는 데 혼란이 있습니까? 좀 더 자세히 알려 드리겠습니다. –

답변

0

mongodb에는 xml 유형이 없습니다. 모든 것은 몽고에서 내부적으로 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 문자열을 다시 얻을 수 있습니다.

+0

실제로 채널 이름에 따라 각 항목을 밀고 MongoDB에 저장하고 싶습니다. DSA와 CN이라는 두 개의 채널이있는 것처럼 각 카테고리에서 항목을 푸시합니다. 예를 들어 두 채널의 경우 두 개의 XML 파일이 있고 각 XML 파일에는 채널 이름에 따라 항목을 추가하려고합니다. 또한 MongoDB와 javascript에서 CRUD 작업을 수행하는 방법에 대한 링크를 제공 할 수 있습니까? –

+0

xml 파싱을 묻는 질문은 무엇입니까? CRUD 작업은 node.js의 http://mongoosejs.com/docs/에서 확인할 수 있습니다. 또는 https://www.npmjs.com/package/mongodb를 사용할 수 있습니다. 나는 몽구스를 사용한다. mongodb 가이드를 위해 다른 훌륭한 사이트가 없다. –

관련 문제