How can I convert xml to java so that it could read the xml document and put it in to a database?
Your question is rather obscure and general. There are a number of options for converting XML to Java objects:
JAXB
XStream
XMLBeans
This article could be useful.
But anyway you will have to read much before getting something more complex to work.
This is all in case you need to map your xml to java objects. If you just need to parse the XML:
dom4j
xerces
JAXP
Check this: http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html
That's how you read xml file. Then you just crate SQL query to insert it into database (JDBC?)
It's not clear at all, but if you are talking about parsing a XML file to do whatever you want with it in Java (also storing it in a database) you have to already ready choices:
using a DOM parser
using a SAX parser
both are covered here just to give you an example, but check documentation for better explaination.
Apparently JAXB can do marshalling/unmarshalling. I've not used it, but it seems to do what you want. From there, you can use an ORM of some type to put your objects in a database, or you can handcraft SQL.
It sounds like you are looking for something like JAXB or Castor. They both let you convert from a Java object -> XML and XML -> Java object.
Check Hyperjaxb3. It is a JAXB plugin which makes schema-derived classes to JPA entities. Thus you can easily do XML <-(JAXB)-> Java <-(JPA)-> RDB.
For this I recommend EclipseLink. EclipseLink offers both JAXB (object-to-XML) and JPA (object-to-Database) support.
The EclipseLink JAXB (MOXy) implementation offers all the extensions you need for mapping JPA entities to XML, for more information see:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA
Use this https://json2csharp.com/xml-to-java in case you're looking for an online tool, you can then deserialize your object and fill it in the database
Related
I am using java, I want to read strings from an XML tag. EX: < blank type="Something">
I need to be able to assign "Something" to a variable. Any ideas?
There are a lot of ways to do this:
You can use the XML APIs provided with Java (SAX or STAX or DOM).
There are libraries that build on the XML APIs (JDOM, DOM4J, or XOM) which are easier to use than the raw APIs.
There's Java-XML databinding, described in Pratik's answer. Java-XML databinding is sometimes overkill, depending on your requirements, and when there are errors they can be hard to figure out. Sometimes it's worthwhile, though. I think JiBX is particularly interesting.
If you don't know where to start, start with XOM. XOM was created by a JDOM contributor, it was designed to be easy to use.
What you want to achieve is referred to as Unmarshalling an XML. Unmarshalling means extracting data from an XML document and using it to construct a Java object or graph of objects. There are various APIs available to do the same. You should have a look at the following links:
JAXB:
http://www.oracle.com/technetwork/articles/javase/index-140168.html
Castor: http://castor.codehaus.org/
JiBX: http://jibx.sourceforge.net/
So my code is a client of an api, the data is returned as xml and Ive been able to create valid xsd file from some examples of that xml and then generate some JAXB classes from the schema so my code can now load and work with the xml data without ever having to work directly with Xml.
But unfortunately in the latest version of the api they have dropped xml support and only return json. Is there a json process I can do analogous to my xml process ?
If its not possible from a schema are there solutions so that if I manually create my json classes, I can them use them to automatically martial in raw json data, so at least I only have to deal with json once.
EDIT:Maybe https://github.com/ko5tik/jsonserializer would be useful
UPDATE:FYI so I looked at jsonschema2pojo but that only creates pojos from a schema, and I didnt actually have a json schema, just the actual json. I had a go at creating a schema from the example json I had but didnt get it working for al but the simplest example.
I then looked at http://wiki.fasterxml.com/JacksonInFiveMinutes , Jackson would have been able to use the pojos created by jsonschema2pojo if Id managed to get it working. So I then tried following the example and created a POJO based on the json data I had and it was quite easy, then I then tried full data binding using this and it worked first time.
So in summary Im going to use Jackson for dealing with json returned by the webservice, I'll have to manually create a POJO for each entity but at least it works.
As to answer, maybe try "jsonschema2pojo".
But similar questions have already been asked before:
How can I parse a JSON schema and create a default json object or generate a java class?
Generate Java class from JSON?
Is there a tool to generate a JSON schema from an XML schema through Java?
If you want to create POJOs from a sample JSON file, I've created a (hacky) ruby script that can help. I've added more details in another answer here: https://stackoverflow.com/a/8585423/1109175
You can find the code on github: https://github.com/wotifgroup/json2pojo
I don't think there is a JSON equivalent to XSD, and that makes what you are asking for really hard to implement. For a start, a generator program cannot reliably deduce the Java types that need to be used for the attributes of a JSON object. (In the XML case, you made those deductions and expressed them in the corresponding XSD.)
No, I don't see a way for doing that... JSON is without a schema definition, so there's no meta-data to generate the classes from.
Edit: I stand corrected, there is a schema definition (draft), it has however expired since the beginning of this year, and I have not encountered any schemas provided.
I'm trying to convert to list of hibernate objects to XML using JAXB. Are there any special considerations OR any reasons a list of hibernate objects with one to many relationship can't be converted?
1.) It seems like PersistentSet provided by Hibernate is not Serializable and hence, can't be converted by JAXB. I would probably would need to cast it to a "Serializable" set. Is this correct? If yes, how do I achieve it?
Edit 1. I'm going to write Set adapter class, similar to this example link. Will update my findings. http://www.objectpartners.com/2010/01/25/using-jpa-and-jaxb-annotations-in-the-same-object/
As mentioned below, there is no need to convert PersistentSet as it is serializable already.
Edit 2 (and Solution). Ok, I'm able to able to make it work.Thanks to Maven's Schemagen which gave the hint that 'java.sql.TimeStamp' is not compatible with JAXB. I wrote an adaptor which converts TimeStamp to Date.
Thanks everyone for guiding on this.
PersistentBag and PersistenceSet, both are serializable. See http://docs.jboss.org/hibernate/core/3.2/api/org/hibernate/collection/PersistentSet.html and http://docs.jboss.org/hibernate/core/3.2/api/org/hibernate/collection/PersistentBag.html
Why don't you use hibernates xml store? It is much easier.
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/xml.html
I'm able to able to make it work.Thanks to Maven's Schemagen which gave the hint that 'java.sql.TimeStamp' is not compatible with JAXB. I wrote an adaptor which converts TimeStamp to Date.
Is it possible to create a class dynamically by reading an xml file ( in java preferably) ? if yes, please provide pointers on how to do it.
In the process of development, we have come up with a class that has 5 attributes, all these attributes correspond to an entry in the xml file, now if the user adds/modifies the xml entry the object corresponding to it must change automatically, one approach would be generate the source code, before compile time.Is there any other way ?
Is there any common pattern to model such changes in the system ?
Thanks,
If you have an XML Schema for your XML there are a number of kits for this. Start with JAX-B.
If you stored your attributes in a HashMap then you could simply parse the XML and then set the attributes accordingly.
Assuming you have a XML schema (xsd), you can feed that schema to JAXB's xjc command to generate Java classes. xjc is included with the Java 6 JDK.
The process usually works the other way around (ie. defining a class with those attributes, and serializing an instance of the class to XML)
If you really need that sort of flexibility, a scripting language would save you a lot of trouble.
I think JAX-B can provide functionality like that.
If you're looking for byte code generation, have a look at cglib, it is the one used in Hibernate.
But maybe some annotations can also fulfill your requirement, just like Google Guice's dependency injection.
I've got to write a multithreaded chat program, using a server and clients but each message sent has to be in XML.
Is it simpler/easier just to write out all the code in java, and then try and somehow alter it so the messages are sent in XMl format, or would it be simpler just to try and go for it in XML and hope it works. I'll admit I don't know that much about XML. :)
Also any links to any relevant online help/tutorials would be much appreciated.
Thanks.
When messing with XML in Java, PLEASE consider using JAXB or something similar. It allows you to work with a normal object graph in memory and then serialize that to XML in one operation (and the other way around).
Manipulating XML through the DOM API is a slow way to lose your sanity, do not do it for any non-trivial amount of XML.
I fail to see what the program being multithreaded or a server have to do with it though...
Check out XStream. You can use this to marshall a normal Java object into XML, and back again into an object, without having to do anything instrusive like define interfaces or specify schema etc. i.e. it works out of the box for objects you already have defined. For most cases it's seamless in its default mode.
XStream produces a direct XML serialised representation of a Java object (i.e. XML elements represent each field of a Java object directly). You can customise this further as/when you require. If you want to define persisted objects in terms of schema (XSD) then it's not appropriate. However if you're transporting objects where persistence is short-term and you're not worried about conforming to some schema then it's definitely of use.
e.g.
Person person = new Person("Brian Agnew");
XStream xStream = new XStream();
System.out.println(xStream.toXML(person));
and conversion from XML to the Person object is similarly trivial.
(note XStream is thread-safe)
There is something called XML RPC. This examples pretty much shows what you're looking for:
http://docstore.mik.ua/orelly/xml/jxml/ch11_02.htm
It would be simpler to use existing XMPP clients and servers and not write your own at all.
If this is in fact homework, then I would suggest writing the client and server as you have suggested, using all java, but use a String as the message. You can then easily add parsing of the string to/from XML when all other parts are working.
I would suggest to also have a look at Betwixt and Digester. For Digester there are some tutorials which can be found in the Digister-wiki. Betwixt provides some pretty good tutorials right on its website.
Additionally to these two tools there is a list of alternatives that can be found in the Reference section of http://wiki.apache.org/commons/Digester/WhyUseDigester
You're on the right page trying to break the task into smaller pieces.