1

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());
                  }         
         }
manish payasi
  • 77
  • 1
  • 10
  • what you have tried...? – smali Aug 21 '14 at 10:48
  • I'm currently going the way of `xml -JAXB-> Java -Jackson-> json -> DBObject -> mongodb` might try that or any other way you can use to read an xml file to java and then to produce an DBObject out of – Trudbert Aug 21 '14 at 10:50
  • Why using json and dbobject? Wouldn't it be much simpler if you just did an sql insert after parsing the xml? – Michael Kleimann Aug 21 '14 at 10:56
  • @mk2301 mongodb is nosql and stores bson (binary json) documents and I also use the json stuff for other things too so jackson annotations are there anyway. The mongo driver provides a method to transform json to a dbobject which can then be inserted. – Trudbert Aug 21 '14 at 11:11
  • Thanks, I just learned something new! – Michael Kleimann Aug 21 '14 at 11:12
  • That's one of the nice things about stackoverflow ^^ – Trudbert Aug 21 '14 at 11:15
  • @mk2301 your answer was correct anyways the steps are read the xml (jaxb) bring them into the right format (jackson) and then insert into the database. Those steps will be the same no matter if it's sql or nosql and he will have to figure out the best way to do this in his special case form the millions of differnt ways available for each step. So no need to delete it – Trudbert Aug 21 '14 at 11:20
  • @manishpayasi what error message do you get or that goes wrong? – Trudbert Aug 21 '14 at 13:22

2 Answers2

2
  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)
Michael Kleimann
  • 502
  • 2
  • 10
  • could you provide me a sample application to store xml data into MongoDB because i tried it but i didn't able to resolve it. I'll be thankful to you. – manish payasi Aug 21 '14 at 12:20
  • @manishpayasi see [this question](http://stackoverflow.com/questions/1823264/quickest-way-to-convert-xml-to-json-in-java) for xml to json and [this question](http://stackoverflow.com/questions/5699323/using-json-with-mongodb) to insert json into mongodb – Trudbert Aug 21 '14 at 13:12
  • @mk2301 see the above code which i have edited, but it throw the exception "java.lang.ClassCastException: com.mongodb.BasicDBList cannot be cast to [Lcom.mongodb.DBObject" So please provide some sample applications which can help something to me. – manish payasi Aug 21 '14 at 13:15
  • @Trudbert how to convert xml to json and then store that json into mongodb – manish payasi Aug 21 '14 at 13:17
  • Sorry but I have never done anything with MongoDB. I assume the exception is thrown at this line: `DBObject[] dbObject = (DBObject[])JSON.parse(json);` The exception says that the type returned by JSON.parse() cannot be casted to DBObject. I assume that parse() returns some kind of JsonObject and this has nothing to do with DBObject. I think you have to convert the JsonObject to a normal object and in the next step you convert this object to a DBObject. BUT I don't know which apis you use and what functions they have, so I would look up in the documentation how a DBObject should be created. – Michael Kleimann Aug 21 '14 at 13:30
  • Oh and do you really want to get a DBObject array? – Michael Kleimann Aug 21 '14 at 13:32
  • 1
    No JSON.parse is ok that's from the mongo driver my guess would be the array thing. But without any information on what goes wrong there is no way of telling why it goes wrong... – Trudbert Aug 21 '14 at 14:09
  • @Trudbert Now i've removed the array kind of thing from DBObject, see the line **DBObject dbObject = (DBObject)JSON.parse(json); in above code. Now it throw the exception: 'Exception in thread "main" java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not: [_id]' – manish payasi Aug 22 '14 at 05:05
  • Thanks to all of you guys, I got the solution **List dbObject =(List) JSON.parse(json);** – manish payasi Aug 22 '14 at 06:14
0

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

manish payasi
  • 77
  • 1
  • 10