2016-05-03 24 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

谢谢,请你把我的链接检查出来 –