如何使用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());
}
}
答案 0 :(得分:2)
答案 1 :(得分:0)
<强> List<DBObject> dbObject =(List<DBObject>) JSON.parse(json)
强>