tag:blogger.com,1999:blog-8852543472291091913.post532475625939992062..comments2023-06-09T01:54:26.505-06:00Comments on Software Everything: In Response to the Easiest Java XML Binding. What About XStream?Anonymoushttp://www.blogger.com/profile/15025531615679576013noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-8852543472291091913.post-84988603727514500912008-12-08T17:16:00.000-06:002008-12-08T17:16:00.000-06:00I wrote a post about using JAXB to do XML parsing,...I wrote a post about using JAXB to do XML parsing, inspired by this article and zaeffi's comment:<BR/><A HREF="http://www.basilv.com/psd/blog/2008/simple-xml-parsing-using-jaxb" REL="nofollow">Simple XML Parsing using JAXB</A>Unknownhttps://www.blogger.com/profile/07430401813145860745noreply@blogger.comtag:blogger.com,1999:blog-8852543472291091913.post-70217253918445274052008-11-23T07:58:00.000-06:002008-11-23T07:58:00.000-06:00This was a really helpful post. I had the same ide...This was a really helpful post. I had the same idea that parsing XML should be just a matter of throwing some annotations on my java objects, like Hibernate persistence, but the 'official' Java XML APIs don't seem to mention this case anywhere in their documentation. <BR/><BR/>@zaeffi Thanks for the tip on how to do this with JAXB. I'm probably going to try that.Unknownhttps://www.blogger.com/profile/07430401813145860745noreply@blogger.comtag:blogger.com,1999:blog-8852543472291091913.post-47459418893892137822008-07-23T10:00:00.000-06:002008-07-23T10:00:00.000-06:00I had only ever used JAXB with XSD, and thought th...I had only ever used JAXB with XSD, and thought that it was required for any martialing; guess I should have looked into it more. In any case if you use your JAXB method you have to write some helpers, and if you use XStream you don't have to.Anonymoushttps://www.blogger.com/profile/15025531615679576013noreply@blogger.comtag:blogger.com,1999:blog-8852543472291091913.post-75518478204654684292008-07-23T09:45:00.000-06:002008-07-23T09:45:00.000-06:00Why does Jaxb not fit your needs? As I understand ...Why does Jaxb not fit your needs? As I understand it, XML-Schema is just really needed if you want to create your java classes, if you want to create them yourself, just add the Annotations, just like in your own Binder/XStream.<BR/><BR/>With JAXB Annotations your classes would looke like this:<BR/><BR/><I><BR/>@XmlRootElement(name = "rss")<BR/>public class Rss2 {<BR/> private List<Channel> channel;<BR/> private String version;<BR/><BR/> @XmlAttribute<BR/> public String getVersion() {<BR/> return version;<BR/> }<BR/><BR/> // other getter/setter/logic<BR/><BR/><BR/>@XmlType(name = "channel")<BR/>public class Channel {<BR/> private String title;<BR/> private String link;<BR/> private String description;<BR/> private String language;<BR/> private String pubDate;<BR/> private String lastBuildDate;<BR/> private String docs;<BR/> private String generator;<BR/> private String managingEditor;<BR/> private String webMaster;<BR/> private int ttl;<BR/> private List<Item> item;<BR/><BR/> // getter/setter/logic<BR/><BR/>@XmlType(name = "item")<BR/>public class Item {<BR/> private String title;<BR/> private String link;<BR/> private String description;<BR/> private String pubDate;<BR/> private String guid;<BR/><BR/> // getter/setter/logic<BR/></I><BR/><BR/>Just the transformation is not that nice to look at, but with two generic helper methods even this can be solved (roughly):<BR/><BR/><I><BR/>public static <E extends Object> E fromXML(File xmlFile, Class<E> rootElement) throws JAXBException, IOException {<BR/> JAXBContext context = JAXBContext.newInstance(rootElement);<BR/> Unmarshaller um = context.createUnmarshaller();<BR/> um.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());<BR/> FileInputStream fin = new FileInputStream(xmlFile);<BR/> return (E) um.unmarshal(fin);<BR/>}<BR/><BR/>public static void toXML(Object root, File xmlFile) throws JAXBException, IOException {<BR/> JAXBContext context = JAXBContext.newInstance(root.getClass());<BR/> Marshaller marshaller = context.createMarshaller();<BR/> marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);<BR/> FileOutputStream fos = new FileOutputStream(xmlFile);<BR/> marshaller.marshal(root, fos);<BR/> fos.close();<BR/>}<BR/></I><BR/><BR/>with these you can do the transformations with 1 line:<BR/><BR/><I><BR/>Rss2 rss2 = Helper.load(new File("Rss2Test.xml"), Rss2.class);<BR/>Helper.save(rss2, new File("Rss2Test.xml"));<BR/></I><BR/><BR/>Or am I missing something here?zaeffihttps://www.blogger.com/profile/06165108938304365215noreply@blogger.comtag:blogger.com,1999:blog-8852543472291091913.post-38230995191782818432008-07-09T15:49:00.000-06:002008-07-09T15:49:00.000-06:00At last! An XML framework which allows for maximum...At last! An XML framework which allows for maximum laziness! I read once that this is a virtue amongst programmers :)<BR/><BR/>But seriously, use of annotations is brilliant. And, IMHO exactly what meta-data is all about. .NET uses similar techniques in XML serialization, but I've been having trouble tracking down anything noteworthy in Java.unknownhttps://www.blogger.com/profile/17564355225503341606noreply@blogger.comtag:blogger.com,1999:blog-8852543472291091913.post-21572102330313724242008-07-08T10:07:00.000-06:002008-07-08T10:07:00.000-06:00The other option that springs to mind is Commons D...The other option that springs to mind is Commons Digester. It's read-only, so you can't write your objects back out as XML, but I've found it to be very flexible once you get your head around what it's doing. I had to read in a 4-level deep XML file into objects and it ran to about 14 lines of code.<BR/><BR/>http://commons.apache.org/digester/<BR/><BR/>BarnyAnonymoushttps://www.blogger.com/profile/10078106968069394694noreply@blogger.comtag:blogger.com,1999:blog-8852543472291091913.post-73932180176232136492008-07-08T09:06:00.000-06:002008-07-08T09:06:00.000-06:00That is correct, I was not sticking to the true de...That is correct, I was not sticking to the true definition of "binding" as in the sense of JAXB and its use of XSD. What I was really after was serialization.Anonymoushttps://www.blogger.com/profile/15025531615679576013noreply@blogger.comtag:blogger.com,1999:blog-8852543472291091913.post-7896540748126215252008-07-08T07:29:00.000-06:002008-07-08T07:29:00.000-06:00I was considering to mention XStream in your previ...I was considering to mention XStream in your previous post, but I decided not to because of the fact that XStream considers itself to be a serialization tool:<BR/><BR/>"Q:Is XStream a data binding tool?<BR/>A: No. It is a serialization tool." (see xstream FAQ)<BR/><BR/>The main difference is, from my understanding, that in XStream the object is the source and not an XML schema. <BR/><BR/>Your requirements start with an XML document, and it *might* be that you will have troubles to use XStream. If not, I can highly recomment XStream, really easy to use, good performance and well documentedChrigelhttps://www.blogger.com/profile/05227615949751525227noreply@blogger.com