Java - Apache commons Digester - write xml - java

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

Related

how to directly work with `xsl` from Java?

Using Java to write xml strikes me as perhaps not the best match. Is this just because it's new and unfamiliar, perhaps?
Rather than generating JAXB source code from xml might it not be more flexible to work with xml files directly? What I mean is, cannot the middle-man be cut out?
If JAXB can generate source code for the class files to manipulate xml might there a tool which uses JAXB without creating concrete boiler-plate?
If there's simply a mis-match making this impossible please elaborate. Or, if it's simply out of reach, what are the obstacles?

CRUD on XML using Java

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).

Bind several XML files with corresponding XSD schema to one Java model

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!

How to modify XML file according to a XML Schema (.xsd file)

I have a xml file of network traffic exported by Wireshark. I need to develop a command line tool to parse the exported xml file, and create a new XML file according to given XML Schema (.xsd) or in other words modify a xml file according to given XML Schema.
Any ideas, how could I do that? most preferably in Java.
There are a few technologies that you could use here. Often XML-to-XML translation is defined using some sort of intermediate language and then actual translation is wired together in a general purpose programming language, like Java.
Take a look at these:
XSLT, Java and XSLT
Schematron
You can also do it in code, though I would adivise against this as it gets messy. However, for completeness, have a look at:
SAX

When and why would you use Apache commons-digester?

Out of all the libraries for inputing and outputting xml with java, in which circumstances is commons-digester the tool of choice?
From the digester wiki
Why use Digester?
Digester is a layer on top of the SAX
xml parser API to make it easier to
process xml input. In particular,
digester makes it easy to create and
initialise a tree of objects based on
an xml input file.
The most common use for Digester is to
process xml-format configuration
files, building a tree of objects
based on that information.
Note that digester can create and
initialise true objects, ie things
that relate to the business goals of
the application and have real
behaviours. Many other tools have a
different goal: to build a model of
the data in the input XML document,
like a W3C DOM does but a little more
friendly.
and
And unlike tools that generate
classes, you can write your
application's classes first, then
later decide to use Digester to build
them from an xml input file. The
result is that your classes are real
classes with real behaviours, that
happen to be initialised from an xml
file, rather than simple "structs"
that just hold data.
As an example of what it's NOT used for:
If, however, you are looking for a direct representation of the input xml document, as
data rather than true objects, then digester is not for you; DOM, jDOM or other more
direct binding tools will be more appropriate.
So, digester will map XML directly into java objects. In some cases that's more useful than having to read through the tree and pull out options.
My first take would be "never"... but perhaps it has its place. I agree with eljenso that it has been surpassed by competition.
So for good efficient and simple object binding/mapping, JAXB is much better, or XStream. Much more convenient and even faster.
EDIT 2019: also, Jackson XML, similar to JAXB in approach but using Jackson annotations
If you want to create and intialize "true" objects from XML, use a decent bean container, like the one provided by Spring.
Also, reading in the XML and processing it yourself using XPath, or using Java/XML binding tools like Castor, are good and maybe more standard alternatives.
I have worked with the Digester when using Struts, but it seems that it has been surpassed by other tools and frameworks for the possible uses it has.

Categories