Dynamic xml into mongoDB

2019-05-11 02:02发布

问题:

I'am currently working on an application (Java), and can figured out the best way to solve my issue. I need to store data in mongoDB (with the actual data type when supported by bson format), I get the data in an xml file, and his schema (both are created dynamically at runtime, so I have no idea what's in it).

To be more specific, I didn't have any information on the fields, names of the data. A user can create new "object" (for which there is no java class in the application) dynamically. When a user create a new object, I receive a xml schema which describe the object. So when a user try to add an object of this type (data are in a xml format for the new entity), I validate it with the xml schema and now I need to store the object in mogoDB. So I need to be able to transform my xml in bson (or basic java object with mongo java driver) and back into xml after a query.

Example:

If a user want to manage people, he will define the people schema:

<People>
   <Name>...</Name>
   <Lastname>...</Lastname>
   <Age>...</age>
   ...
</People>

Here I got the xsd (a valid xsd format with all informations). Then when a user add a People I get the data like that:

<People>
   <Name>John</Name>
   <Lastname>Smith</Lastname>
   <Age>32</Age>
   ...
</People>

So i wonder if the best approach will be something like jackson: xml -> Pojo -> bson, or with XSLT xml -> json/bson (with encoding for data types). Or simply by reading the xml file and my basic java objects manually.

Did anyone have some advice on how to implements one of those solutions or better solutions?

回答1:

Best approach would seem to go XML <-> JSON See: Quickest way to convert XML to JSON in Java

Then you can go JSON <-> BSON using com.mongodb.util.JSON parse and serialize.



回答2:

I believe you can use MongoJack to magically turn your XML into something that MongoDB understands (and vice-versa)