I am developing a java application and i am using XML to store the settings and other data in the application.I have read about Java preferences manager API but i felt storing in XML is more convenient in my application.I started usng JAXB first but then i dint find any tutorials to modify the XML once it is created.My application involves storing the Email account details of the users.As the user adds his accounts dynamically , i need to add them to XML as well.So i dint find JAXB convenient(or rather i dint find any tutorials to update or modify the XML ).The only other option i found was DOM parser here http://docs.oracle.com/javase/tutorial/jaxp/dom/readingXML.html .But i feel it is too complicated for such a simple application.speed , memory etc doesnt matter to me.Are there any other alternatives to do this?
This can be a good (java-oriented) solution. Besides being Berkeley DB enables the development of custom data management solutions, without the overhead traditionally associated with such custom projects. yet based on XML files. It's has a very complete documentation for integration with Java lang.
With JAXB you can:
Unmarshal the XML into Java objects.
Manipulate the Java objects to modify the XML.
Marshal the objects producing a new XML document which you can use to replace the original XML.
Or:
Parse the XML into a DOM
Unmarshal the XML using a JAXB Binder
Modify the object
Use the Binder to apply the changes back to the DOM.
Write out the DOM replacing the original XML
I like to point you to XMLBeam (Disclosure: I'm affiliated with that project). This library is ideal to store configuration values in XML and while the application grows maintain the XML format without changing your Java API.
This is how IO operations look like:
Projection projection = projector.io().file(file).read(Projection.class);
projector.io().file(file).write(projection);
You get one liner IO handling and static typed access to the XML content. A pattern for configuration data would be to include a default value template in your JAR, read this if no config file was found on disk and write it back to disk on config changes. But of course you can also create documents from scratch (described in tutorials).
Related
I have XML Configuration file and I need to load/save it so What are best ways to Parse XML Files for Load/Save configurations other than Serialization as I don't like the option of Serialization
For a Java application I serialized a pretty complex application state using XStream. For the most part this works extremely good. If the object is really simple this should not be a problem. Another simple alternative I often use to transfer data from Object <-> XML is JAXB with annotations on the Objects, or if the XML structure is the master with an XML Schema from which the classes can be generated.
For C# you could use IXmlSerializable, or DataContract. Multiple answers can be found on SO using both these classes. See this to see how to use DataContract.
I'm using Apache Commons Digester (with annotations) in order to load an XML file into a Java class.
Everything works correctly.
Now, I need to update the XML file. I have to change (in Java) the value of a property, and then to write out the new XML file.
How could I do? As far as I can see, Digester API is not designed for this purpose.
Edit: reading the answers, I understand that I did not give enough informations. My XML file is a configuration file for a program A, so I really need its content when I launch program A. Then, I have another GUI program B that is able to modify this configuration file, it just takes some input from the user and modifies the relative fields on the XML file.
As you have found, Digester is a read-only tool - it provides a mapping from XML to Java classes, but not the reverse. If you need to read the XML into Java classes and then write it back to XML again, I would suggest either:
Keep using Digester for the reading, and use a low-level XML writer
class (such as XMLStreamWriter)
to write the data back. This is suitable if your data is not that
complex, and/or the output XML is a different structure to the input
XML.
Replace Digester with a full Java to XML mapping library (JAXB, JiBX
etc.), which will both read and write the XML for you. This is
suitable if your data is more complex, and/or the output XML is the
same structure as the input XML.
Don't know enough about what you are doing to really recommend one of those approaches over the other.
If you don't actually need the data in Java classes at all and are just trying to transform it, then XSLT as #sharonbn says is also a good solution.
XML modification (usually called XML transformation) is best handled in XSLT standard. Apache Xalan is (one of) the Java libraries that implement this standard
I have read the spring documentation for reading/writing the XML documents..(http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/oxm.html)
The options provided using different typs of marshalling(Oxm, Jaxb, jibx, castor, xstream) all seems to be using the conversion of a document based on some mapping file or a java bean or identifying by comparing each individual element using xstream.
I have a requirement, where i will get big xml files and i need to read some of the elements only in it and i dont want to keep the complete xml into the memory.
What are the options that i have in spring?
if any of the options that the spring documentation provides and useful for my requirement, please provide some examples.
BTW, i am using latest spring 4... FYI.
For large XML file you would need a SAX parser which is an event based parser.
Here an example
http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/
Spring Object/XML Mapping is to convert XML to and from an object which is a different use case.
I am currently working on a Webservice which will take several different XML files as input. Now I have to somehow bind these different XML files to the Java model of the Webservice. The problem is, that this Webservice needs to be as flexible as possible because the format of the input XML is viable to change at any time.
Is there a way to use the XSD-schema files, which correspond to the input XML, to directly bind the input XML to the existing Webservice model?
To may be make things a little more clear: EclipseLink MOXy provides the option to use several binding files to bind several XML formats to one java model. I am basically trying to do the same thing, but without having to write a binding file for each possible XML input. Instead I would like to use the XSD-Schema associated with the XML input as a binding file.
Also I cannot compile a new model or classes from a new XSD. The Model needs to be as constant as possible.
Tips or general pointers in the right direction would be greatly appreciated!
I am going to develop a database import/export feature in a Java EE application.
I plan to use XML-binding solution to convert between Java object and XML file which is import/export file.
Import feature: unmarshal the XML file to Java object in memory representation, then use JDBC to update the database.
Export feature: inverse the import process. retrieve the database to Java object and marshal the object to XML.
I think it can work fine, but it's not flexible enough. Because the XSD of XML is pre-defined, it's impossible to change XML schema and Java object definition at runtime. Say it's dynamic binding. Even I want the feature supports other file formats (You could forget it, if the format is too far at this stage).
What is your advice about the feature? Thanks!
I don't know whether this is correct answer, but if in any case it is helpful:
I have used Spring, Hibernate, JAXB where you can annotate your database entity class and its element with jaxb annotation and you are not required to write any xml schema files. In spring you can use jaxb Marshaller.
I think it should be possible in pure jaxb also, so u can look into jaxb annotation.
I think it can work fine, but it's not
flexible enough. Because the XSD of
XML is pre-defined, it's impossible to
change XML schema and Java object
definition at runtime
If you think it isn't flexible enough, go with a data interchange format which relieves you from all these fixed schema definitions (I know even JSON has a schema specification but you get the point). Is using JSON acceptable?
I would go as far to argue that if "importing to database" and "exporting from database" is the only requirement, you need not even create Java objects for this. Simply pass in a JSON string which contains the schema which would then be processed by a JSON processor which interfaces directly with your DAO layer. Similarly with the data read from the database. The downside is that "date" support in JSON is spotty at best.
Come to think of it, it need not be JSON. You can take a look at other data serialization formats like Apache Avro. But then again, if XML is your requirement which can't be changed, you can get around the "flexibility limitation" by not using a schema at all.
After all, XML is like violence. If it doesn't solve your problem, you're not using enough of it. :-)