我如何使用Java将xml文件数据/内容(磁盘中已经存在)插入MongoDB数据库?
请任何人都可以解决。
//编辑代码
XmlMapper xmlMapper = new XmlMapper();
List entries = xmlMapper.readValue(new File("C:/Users/manish/Desktop/BaseX65/xml/books.xml"),List.class);
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writeValueAsString(entries);
try
{
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("newdb");
DBCollection collection = db.getCollection("dummyColl");
DBObject dbObject = (DBObject)JSON.parse(json);
collection.insert(dbObject);
DBCursor cursorDocJSON = collection.find();
while (cursorDocJSON.hasNext()) {
System.out.println(cursorDocJSON.next());
}
}
List<DBObject> dbObject =(List<DBObject>) JSON.parse(json)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句