I’ve always felt that XML processing in Java is overly complex. Surely something that so many business applications need to do on a regular basis should be simpler and easier by now.
Recently I needed to get access to the raw XML being retrieved in this case from an Amazon Web Service for two reasons. Our application needed to store the raw XML into a database for caching and audit purposes. The other reason was so that we could look at the raw XML to research some corner case issues when the data we received wasn’t quite what we were expecting.
Have done a:
Document doc = db.parse(requestUrl);
You would have thought that Document would have a method along the lines of
RawXML = doc.getRawMessage();
It’s at that point that you really start to question why something so obvious and so simple is missing from standard Java libraries. I did some Googling around and found lots of people asking similar questions. The solution isn’t difficult it’s just unbelievably clumsy.
Basically what I do now is to read my XML source into a string. Once it’s in a String I can do whatever I need with it. For some reason DocumentBuilder.parse doesn’t have a version that accepts a String. You therefore have to use your String to create an InputStream that you can then pass to Parse.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Fetch the XML into a string
URL myURL = new URL(requestUrl);
InputStream myInputStream = myURL.openStream();
Scanner myScanner = new Scanner(myInputStream, "UTF-8");
String myRawXMLRequest = myScanner.next();
// Output the Raw XML Request to prove we have access to itSystem.out.println(myRawXMLRequest);// Now instead of passing the RequestURL directly to the parser we// wrap the Raw XML String in a new Input Stream and pass that into the Parser. // Old code was: // Document doc = db.parse(requestUrl); // New code:Document doc = db.parse(new InputSource(new StringReader(myRawXMLRequest)));
I recently received updated VPN software from a client. I’d been using their previous GBAuth.jar quite happily for a number of years on my Mac but after installing the new version it just failed to work at all. Double clicking on the GBAuth.Jar file to run it and the Mac would think about it for a moment and then do nothing. Not a single error message, just nothing. I keep my Mac up to date with all the latest patches so I knew I had the latest version of Java installed – or so I thought. What I’d forgotten is that Apple only maintain Java version 6 if you want Java 7 or above you have to download it from Oracle.
Before I went to all the trouble of downloading and installing Java 7 from Oracle I wanted to check the version of Java used to build the old and new versions of the Jar file. There are Jar file viewers but the easiest way for non-Java developers is simply to rename the jar file so it has a .zip extension, e.g. rename GBAuth.jar to GBAuth.zip. You can then double click on it and have OSX expand the zip file. Within the root of this zip file you’ll find the Java Jar manifest.
You can right click on the MANIFEST.MF file and open it with a text editor.
As you can see the new Jar file was actually created by Java 1.7.0-b18 so I couldn’t really expect it to work on my Java 6 installation on my Mac. Just to prove this was the issue I looked at the old Jar file….
So this proved that the previous version of the Jar had been compiled with an earlier Java version.
I then downloaded and installed Java 7 for the Mac from the Oracle site and the Jar file worked perfectly. In all of this I was only really surprised that double clicking a Jar file built under a previous version of Java just caused it to failed to run silently without any error message or assistance. It’s not a great consumer experience.
Sun seem to be pushing out update 13 for Java 1.6 this week. It’s annoying that by default the update is installing the Yahoo browser tool bar. You need to watch for the tick box and remove the tick for the Yahoo toolbar if you don’t want it. I can see this catching out many end users, which will no doubt cause lots of calls to help desks when their browser isn’t quite the same any more.
I couldn’t see a way of removing the Yahoo toolbar from within the browser – I may well have missed this in which case please let me know. To remove the Yahoo Toolbar you have to go into Start > Control Panel > Add / Remove programs and then remove the Yahoo Toolbar.
In the last few months we’ve seen Apple install additional software with iTunes updates and now Sun additional software with Java updates. I don’t think I’m turning into a grumpy old man yet, but I do think installing additional software on someone’s computer without their explicit authorisation is bad manners and shows a lack of respect for people.