Can you please recommend a fast XML Builder in Java that doesn't use annotations? Thanks.
JAXB 2. You can use it without annotations, defining mappings in XML resources.
Betwixt, from apache is pretty good for simple stuff. http://commons.apache.org/betwixt/
Xstream is a better and faster framework. Its easier to learn as well.
XMLEncoder. It is a standard Java class, simple, feature-poor way of serializing objects to XML.
How about StaxMate -- very light-weight, faster than object-mapper solutions (XStream or JAXB), still reasonably easy to use for many use cases.
Of course it really depends on whether by fast you mean time to write code (if so, XStream is probably the thing), or time to run it (in which case simple Stax/SAX wrapping writers, like StaxMate or XMLBuilder are the things); XStream is not particular fast, although it's not the slowest thing around.
Related
I do transform my XML file into java objects, after some researche i find many API : JAXB, JAXB2, Casto, Xtream, Simpl, XMLBeans...
I would like to know the best one to use for this manner in the context of spring boot, micro-services. reliable, fast, simple. can you give an some exemple.
thank to all.
According to my experience with Xsteam is the best choice not only for XML but for JSON with or without annotations.
Other API is highly dependented on Annotations.
So if you have dynamic class object then go for Xstream.
I'm in a situation that I have a lot of of XMLs, that are sent to me from a server and I'm using JAXB or any API based on that architecture for building instances of objects.
The problem is, I have to per-determine the class that I want to unmarshall for at compile time. My solution that is in my mind, is to read the incoming XML object and based on some tags, I will direct the unmrashaller to make an instance of the specified class. That approach will let me have a lot of IFs statements and big state machine.
Is there a better design pattern or approach ?
Try using Apache digester 3, I think it can save you lots of "if"s and is not difficult to use at all.
Have a look at this article: http://www.javaworld.com/javaworld/jw-10-2002/jw-1025-opensourceprofile.html
I'm working on an existing system that's generating XML for a legacy system using a simple template language. This is obviously not ideal because it's difficult to see the structure of the generated XML, it suffers from escaping problems and it's easy to generate invalid XML.
For any sane XML formats I'd just Xstream or another Java XML serializing library, but this legacy system has a lot of strange rules like "this node should be excluded if the value is less then ten" and "the formatting of the date in node x depends on the value of node y". There are other strange rules as well, but this should be enough to get the idea.
As I've said, the template approach is far from idea, but it's pragmatic and works (with some effort). Is there a better way to approach generating XML for legacy systems with this amount of formatting rules? XSL has crossed my mind, but implementing any amount of logic in XSL is frankly not very tempting.
Basically you need some custom logic during serialization. I am guessing that the in-memory object structure is not directly mirrored in the XML structure? Alternatives:
Use StAX and distribute read and write methods within the objects.
Use JAXB and insert custom serialization.
Don't even think of expressing your custom logic in anything other than java, i.e. some "super" framework.
I am not sure, if this is what you are looking for, but maybe try XML Binding like JAXB...
In other words: you could generate a class library from your xsd-Schema and then build your object graph in java code, then serialize it in one call to xml.
You could use simple xml and some converters I think:
http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php
What is the best performance solution for XML generation.
My goal is to build a few simple XMLs from code. I am going to implement simple custom StringBuffer based implementation of XML Builder. From other side there are several libraries like http://code.google.com/p/java-xmlbuilder/ and http://code.google.com/p/xmltool/ which has nice DSL but I guess lack on performance.
Since my goal is build simple enough XMLBuilder with great performance I think I will build custom solution. It will featuring:
Nice Java-based DSL for XML constructs (adding tags basically)
Great StringBuffer based performance.
String data escape handling when adding XML tags.
Auto-indent
Please suggest if I am wrong on performance expectations and its probably better to use ready-made libraries.
UPDATE. Why I think the performance of standard xml builders is not very good.
Standard XML builders uses Document Builder Factory and works with classes behind the scenes. Also these classes optimized to fit all users. For example I don't need namespace support etc.
<?xml version="1.0" encoding="utf-8">
<root>
<testdata>value</testdata>
</root>
</xml>
Consider very simple XML code above. If you build with standard tools it will involve so many work just to make this simple XML. I consider that it's better to just generate it by myself using String.
UPDATE 2. Performance requirement is that code should do as many things as required to generate simple XML and not more.
UPDATE 3. Thanks everyone for great comments! Now I understand better what I need and that my initial goal was not set very correctly with word "performance". My true goal is to use simple enough solution with convenient DSL to describe the XML structure and generate the XML output.
I will use plain Java objects as DSL for XML and generate XML using XStream library which is pretty straightforward solution.
UPDATE 4. JAXB. I discussed XStream vs JAXB and found that JAXB is faster than XStream. Plus I already use JAXB in my project and I like its standard annotations. I change my mind and will go with JAXB for now because XStream was originally heavily developed at the time when JAXB was not so good as today.
I will suggest something very controversial but still ...
Make profiling and performance tests with both libraries.
If you don't have time for that, assuming something is slow would be the wrong choice in my opinion.
Because if it turns out that it actually is not slow, it would save you a lot of time to use an already built and supported library/framework.
Another thought.
You will need to test your completed high performance solution against the solutions already available anyway, to check if it is really high performance. So I would strongly suggest measuring the performance of the libraries available before starting your own.
Regarding:
Standard XML builders uses Document
Builder Factory and works with classes
behind the scenes. Also these classes
optimized to fit all users. For
example I don't need namespace support
etc.
An alternative to DOM is StAX (JSR-173). It is a Streaming API for XML that is quite fast. There are several implementations, I have found Woodstox to be quite performant.
There is powerful and flexible Groovy's NodeBuilder (http://groovy.codehaus.org/GroovyMarkup).
def root = new NodeBuilder()
.people(kind:'folks', groovy:true) {
person(x:123, name:'James', cheese:'edam') {
project(name:'groovy')
project(name:'geronimo')
}
person(x:234, name:'bob', cheese:'cheddar') {
project(name:'groovy')
project(name:'drools')
}
}
XmlUtil.serialize(root, System.out)
This results with an XML document:
<?xml version="1.0" encoding="UTF-8"?>
<people kind="folks" groovy="true">
<person x="123" name="James" cheese="edam">
<project name="groovy"/>
<project name="geronimo"/>
</person>
<person x="234" name="bob" cheese="cheddar">
<project name="groovy"/>
<project name="drools"/>
</person>
</people>
One more high-performance suggestion: use StaxMate -- it is as fast as underlying Stax-based XML writer, which is rather fast (40 - 80 megabytes per second, sustained). Just make sure you do NOT use default JDK 6 Stax implementation (Sun sjsxp) but something faster like Woodstox or Aalto.
I would strongly recommend against writing your own XML writer; it is typically risky (good chance you will forget some part of escaping) as others have mentioned, and not all that likely to be faster than existing efficient solutions (not all existing solutions are efficient; you do need to find ones that are). And in the end... unless you really want to write these things, why not work on something more interesting and meaningful?
But if you do want to do something above and beyond existing writers you could consider using a simple writer and augmenting it with additional functionality that you need. For example, if you just use Stax XMLStreamWriter as base, it is quite easy to add simple but efficient abstractions. Or if you like existing packages, see if you can suggest improvements to their authors (or even code contributions).
I've been doing quite a bit of simple XML-processing in python and grown to like the ElementTree way of doing things.
Is there something similar and as easy to use in Java? I find the DOM model a bit cumbersome and find myself writing much more code than I would like to do simple things.
Or am I asking the wrong thing?
Maybe my question is: Is there a better option than the "XMLUtils" classes I see people implementing in some places to simplify their code when dealing with DOM?
Adding a litte bit here about why I like ElementTree since the question was asked.
Simplicity (I guess anything seems simple after working with DOM though)
Feels like a natural fit in python
Requires very little code on my part.
I'm trying to come up with a simple code example to illustrate, but it's sort of hard to give a good example.
Here's an attempt though. This just adds a tag with a value and an attribute to an existing xml string.
from xml.etree.ElementTree import *
xml_string = '<top><sub a="x"></sub></top>'
parsed = fromstring(xmlstring)
se = SubElement(parsed, "tag")
se.text = "value"
se.attrib["a"] = "x"
new_xml_string = tostring(parsed)
After that, the new_xml_string is
<top><sub a="x" /><tag a="x">value</tag></top>
Not an example that really covers everything, but still. There's also the fairly simple looping over tags when you want to do stuff, easy testing for presence of tags and attributes and other things.
To be honest, all XML APIs in Java suck, you just can vary the level of suckage you push yourself into which may turn horrible/slow to manageable/decent to even suprisingly OK at times.
This all mostly stems from the fact that Java APIs try to be as W3C DOM compliant as possible, in fact Xerces (Java's current native XML solution) prides itself on being compliant to a whole bunch of XML related W3C specifications as you can see from their front page.
The actual Xerces API is very unpleasant to work with, though, and because of that multiple other Java XML libraries have popped out over the years. Currently most popular ones are
JDOM, simplifies DOM operations a lot and do I dare to say even pleasant at times, works like a charm when mixed with Jaxen - well, unless you hit this problem with namespaces.
XOM which has a wonderful presentation about what's wrong with Java's XML right now and how they propose their way of doing things as a solution. In part it is actually better than JDOM, but it's not widespread enough yet so can't really say how it behaves in the real world out there. Definitely worth a check though.
dom4j, well-rounded library, supports all kinds of important features and plays out as a down-to-earth solution for XML. dom4j is basically the "old, proven and reliable" option of the popular ones.
Last but definitely not least I just have to mention StAX just because it's different, it's actually event-driven streaming API for XML. Definitely worth a look just out of curiosity.
PS. I'm currently actually writing my own XML parser/navigator as an exercise but haven't decided on what kind of API it will have. I'm really aiming for ease of use which seems to be quite rare in Java XML APIs so far, but I'm not entirely sure what kind of API I am going to provide. Python's ElementTree seems interesting, but since I'm not entirely familiar with it, would you like to maybe give a short summary on what exactly in it you find enjoyable?
You might look into the following alternatives:
dom4j
xom
jdom
Since I never used ElementTree I don't know wich one is the closest.
If you can use Groovy inside your project, it offers a set of classes that helps a lot when processing XML.
We find XOM (http://www.xom.nu) to provide simple subclassable Element functionality.
It is true the Java XML APIs are not the greatest in terms of usability. My prefered options would be XOM, JDOM then the built in JAXP in that order. There were some rumbling about native XML in the language (Begin Product Tab Sub Links
Integrating XML into the Java Programming Language) as a new data-type but that seems to have stalled.