manish payasi
manish payasi

Reputation: 87

how to insert xml file data into mongodb database using java?

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

Answers (2)

manish payasi
manish payasi

Reputation: 87

List<DBObject> dbObject =(List<DBObject>) JSON.parse(json)

Upvotes: 0

Michael Kleimann
Michael Kleimann

Reputation: 512

  1. Read the file (FileInputStream)
  2. Parse the file (using DOM, JAXB, etc.)
  3. Bring the contents into the right format (json, DBobject)
  4. Insert the parsed information into db (using the appropriate db drivers)

Upvotes: 2

Related Questions