Reputation: 87
how can i insert xml file data/contents (which is already exist in a disk) into MongoDB database using java?
please any one can resolve it.
//Edited code
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());
}
}
Upvotes: 1
Views: 5819
Reputation: 512
Upvotes: 2