Simulate mongodb using xml + json to implement small-sized Databases

Source: Internet
Author: User

Mini-mongodb

Simulate mongodb using Xml + json to implement small databases; 1. create a database. 2. table creation 3. the addition, deletion, modification, and query of table data are for your reference. This helps you better understand the implementation principles of MongoDB!
Code: http://zhangdaiscott.github.io/mini-mogodb
Package org. jeecgframework; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import java. util. UUID; import org. jdom. document; import org. jdom. element; import org. jdom. JDOMException; import org. jdom. input. SAXBuilder; import org. jdom. output. XMLOutputter; import com. google. gson. gson; public class MiniMogodb {private static final String _ uuid = "_ uuid "; /*** get all table data ** @ param path * @ param tablename * @ return * @ throws Exception */public ListLoadTableDatas (String path, String tablename) throws Exception {Gson gson = new Gson (); FileInputStream file = new FileInputStream (path); SAXBuilder saxBuilder = new SAXBuilder (); document document = saxBuilder. build (file); Element root = document. getRootElement (); ListL = new ArrayList (); List
    
     
List = root. getChildren (); for (Element x: list) {if (x. getAttributeValue ("name"). equals (tablename) {List
     
      
Al = x. getChildren (); for (Element s: al) {String data = s. getText (); if (data = null) {break;} Map mp = gson. fromJson (data, Map. class); l. add (mp) ;}} return l ;} /*** Insert table data ** @ param path * @ param tablename * @ param po * @ return * @ throws JDOMException * @ throws IOException */public boolean addData (String path, string tablename, Object po) throws JDOMException, IOException {Gson gson = new Gson (); boolean flag = false; FileInputStream file = new FileInputStream (path ); SAXBuilder saxBuilder = new SAXBuilder (); Document document = saxBuilder. build (file); Element root = document. getRootElement (); List
      
        List = root. getChildren (); for (Element x: list) {if (x. getAttributeValue ("name "). equals (tablename) {Map base = new HashMap (); base. put (_ uuid, UUID. randomUUID (). toString (); Element data = new Element ("data"); String json = gson. toJson (po); Map mp = gson. fromJson (json, Map. class); base. putAll (mp); data. addContent (gson. toJson (base); x. addContent (data) ;}} XMLOutputter out = new XMLOutputter (); out. output (document, new FileOutputStream (path); flag = true; System. out. println ("---------- insert --- data --- success ----------------------"); return flag ;} /*** Modify Table Data ** @ param path * @ param tablename * @ param po * @ throws JDOMException * @ throws IOException */public void updateData (String path, String tablename, object po) throws JDOMException, IOException {FileInputStream file = new FileInputStream (path); SAXBuilder saxBuilder = new SAXBuilder (); Document document = saxBuilder. build (file); Element root = document. getRootElement (); Gson gson = new Gson (); List
       
         List = root. getChildren (); for (Element x: list) {if (x. getAttributeValue ("name"). equals (tablename) {List
        
          Al = x. getChildren (); for (Element s: al) {// if Data is located [uniquely identified by UUID] Map mp = gson. fromJson (s. getText (), Map. class); Map newmp = gson. fromJson (gson. toJson (po), Map. class); if (mp. get (_ uuid ). equals (newmp. get (_ uuid) {mp. putAll (newmp); Element data = new Element ("data"); data. setText (gson. toJson (mp); x. removeContent (s); x. addContent (data); break ;}}} XMLOutputter out = new XMLOutputter (); out. output (document, new FileOutputStream (path); System. out. println ("---------- update --- data --- success ----------------------");} /*** table data deletion ** @ param path * @ param tablename * @ param po * @ throws JDOMException * @ throws IOException */public void deleteData (String path, String tablename, object po) throws JDOMException, IOException {FileInputStream file = new FileInputStream (path); Gson gson = new Gson (); SAXBuilder saxBuilder = new SAXBuilder (); Document document = saxBuilder. build (file); Element root = document. getRootElement (); List
         
           List = root. getChildren (); for (Element x: list) {List
          
            Al = x. getChildren (); if (x. getAttributeValue ("name "). equals (tablename) {for (Element s: al) {// if Data is located [uniquely identified by UUID] Map mp = gson. fromJson (s. getText (), Map. class); Map newmp = gson. fromJson (gson. toJson (po), Map. class); if (mp. get (_ uuid ). equals (newmp. get (_ uuid) {x. removeContent (s); break ;}}} XMLOutputter out = new XMLOutputter (); out. output (document, new FileOutputStream (path); System. out. println ("---------- delete --- data --- success ----------------------");}/*** create database *** @ param path * @ throws Exception */public void createDataBase (String path) throws Exception {FileOutputStream file1 = new FileOutputStream (path); Document document = new Document (); Element root = new Element ("database "); element sort1 = new Element ("table"); sort1.setAttribute ("name", "test"); Element sort2 = new Element ("table"); sort2.setAttribute ("name ", "system. indexs "); Element sort3 = new Element (" table "); sort3.setAttribute (" name "," system. users "); root. addContent (sort1); root. addContent (sort2); root. addContent (sort3); document. setRootElement (root); XMLOutputter out = new XMLOutputter (); out. output (document, file1); System. out. println ("---------- create --- database --- success -------------------");}}
          
         
        
       
      
     
    

  
  
   {"Sex": "male", "age": 20.0, "name": "lisan", "money": 2000.98, "_ uuid": "a2b64d1a-63ea-4a1b-b1e3-67adcc687c0a "}
   
  

* Author: Zhang Dai Hao * Technical Forum: [www.jeecg.org] (http://www.jeecg.org) * mailbox: jeecg@sina.com * chat group: 325978980,14.0350

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.