简体   繁体   English

如何使用Java将xml文件数据插入mongodb数据库?

[英]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? 如何使用Java将xml文件数据/内容(磁盘中已经存在)插入MongoDB数据库?

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());
                  }         
         }
  1. Read the file (FileInputStream) 读取文件(FileInputStream)
  2. Parse the file (using DOM, JAXB, etc.) 解析文件(使用DOM,JAXB等)
  3. Bring the contents into the right format (json, DBobject) 将内容转换为正确的格式(json,DBobject)
  4. Insert the parsed information into db (using the appropriate db drivers) 将解析的信息插入数据库(使用适当的数据库驱动程序)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM