使用java将数据存储在mongodb中

时间:2016-05-03 11:48:02

标签: java xml mongodb

我正在尝试在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 ? ? ? 

但我不知道如何从mongodb中的对象(json)列表中插入? 有没有其他方法从xml中提取数据并将其存储在mongo中(保留XML文件的结构,无需解析文件)

1 个答案:

答案 0 :(得分:-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);