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());
                  }         
         }
8
  • what you have tried...? Commented Aug 21, 2014 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 Commented Aug 21, 2014 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? Commented Aug 21, 2014 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. Commented Aug 21, 2014 at 11:11
  • Thanks, I just learned something new! Commented Aug 21, 2014 at 11:12

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

9 Comments

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.
@manishpayasi see this question for xml to json and this question to insert json into mongodb
@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.
@Trudbert how to convert xml to json and then store that json into mongodb
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...
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.