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?
I believe you can use MongoJack to magically turn your XML into something that MongoDB understands (and vice-versa)
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.
Related
I have xml based on fpml schema.
Used xjc command line tool to generate corresponding pojo classes.
Then I am using JAXB to unmarshal xml into java objects.
I converted this to objects as an intermediate step because then it is easy to read values of some fields.
But problem is fpml schema generated ~1200 classes.
so I am not sure if this is correct approach as jar size will also increase.
My problem statement : convert one xml based on one schema to another xml based on another schema. Both involves fpml. While populating another xml I need to validate few fields from database.
please give me suggestions
Data binding technologies such as JAXB work fine for simple cases, but when the schema is large, complex, or frequently changing, they become quite unwieldy, as you have discovered.
This is a task for XSLT. Use schema-aware XSLT if possible, because it makes your stylesheet easier to debug.
I have a JSON which I am using as a fabricated data to create a POJO using GSON. This is expected data for my tests. Next, I have data from the cucumber feature file which would override some of the fields in the already created POJO i.e. update expected data object. I was wondering if anybody has done this earlier or is aware of pattern which I can use to achieve this. In terms of approaches, in was wondering if makes more sense to create an updated json first from both the data sources or to create POJO from the first JSON first and then apply mutation.
I have found a way around - using Apache Velocity template instead of vanilla json files simplifies the implementation and provides flexibility.
How to convert from json into java objects data and save into database? As I understand the only one way is using DTO? Or I'm wrong?
There is application which returns something like "dump" of database. My aim is to deserialize it and save into database. The problems: in some "object" fields there are ids, there are some exceptions regarding constraints during saving into database.
You need to use google Gson Api, following links show how-tos:
http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
Make sure DTO implements Serializable interface before persisting object into database... i would rather serialize on file system and store path into database because i do not like storing in CLOB or BLOB columns... too expensive queries.
Another option is to use Jackson, some examples could be found here. We use it in production for almost two years and find it powerful and handy.
I'm trying to find some tutorial examples on how to exchange data between databases and XML files using Java, from getting and setting specific data from a database to (if possible) change how the database is structured.
I have conducted research into this, but I'm unsure on whether its JDBC I should look into, XML:DB, JAXB, or if any of them are even relevant to what I'm trying to do.
I plan to create a database example, and then see if I can exchange data to and from an XML file using Java, just to see how it works; what should I look into in order to accomplish this?
Many thanks.
You can do this in many other ways but I do this way
Get data from Databases
Convert it to HashMap
Create a JaxB detail class matching your schema
Create a constructor in the JaxB class which accepts the HashMap and assign the data to the variables in JaxB
Convert JaxB object to XML/JSON by Marshaling
Write to a file if you want
If your new to Jax-B view this tutorial here!
You could do the following:
Use a JPA implementation (EclipseLink, Hibernate, Open JPA, etc) to convert the database data to/from Java objects.
Use a JAXB implementation (EclipseLink MOXy, reference implementation, etc) to convert the Java objects to/from XML.
After further research into my query, I found JDBC (database manipulations) and XStream (XML conversions) to be my most preferred solutions.
For JDBC, I referred to this link
For XStream, I referred to this link
Thank you for your replies.
I'm able to create java object from an xml schema and creating a new xml is also working.
Now using my java object how can i search for a particular tag and update it back into xml?
You can use an instance of Marshaller to write the object back to XML. If you want to apply changes back to an existing DOM then you can use an instance of Binder. Binder is useful when there is unmapped content in your document that you wish to preserve.
For More Information
http://blog.bdoughan.com/2010/09/jaxb-xml-infoset-preservation.html