2016-05-03 147 views
0

我想MongoDB中插入數據(XML文件的內容):存儲數據

ArrayList<Object> map = new ArrayList<Object>(); 
      try 
      { 

       File file = new File ("test.xml"); 
       InputStream inputStream = new FileInputStream(file); 
       StringBuilder builder = new StringBuilder(); 
       int ptr = 0; 
       while ((ptr = inputStream.read()) != -1) 

        { 
         builder.append((char) ptr); 

        } 

       String xml = builder.toString(); 
       inputStream.close(); 
       org.json.JSONArray jsonarray = JSONML.toJSONArray(xml); 

       // jsonArray to map 
       map= toList(jsonarray); 
       DB db = (new MongoClient("localhost",27017)).getDB("test"); 

       //get a single collection 
       DBCollection dbcollection = db.getCollection("mycoll"); 



      //insert the list of object in mongodb ? ? ? 

,但我不知道如何從對象(JSON)的列表中插入mongodb? 是否有任何其他方式從XML提取數據,並將其(保持XML文件的結構,wihtout解析文件)存儲在蒙戈

回答

-1
 //After Getting collection , you can insert the document. Here is the code snippet. 




BasicDBObject document = new BasicDBObject(); 
     document.put("a", "b"); 
     document.put("c", "d"); 

     BasicDBObject documentDetail = new BasicDBObject(); 
     documentDetail.put("e", new ArrayList<String>()); 

     document.put("xyz", documentDetail); 

     collection.insert(document); 
+0

謝謝您的回覆,問題是,我不有(「a」,「b」)...我只有一個對象列表:org.json.JSONArray jsonarray = JSONML.toJSONArray(xml); // jsonArray to map: map = toList(jsonarray);我沒有價值 –

+0

謝謝,請你把我的鏈接檢查出來 –