Archive for the ‘Development’ Category

Java to XML, XML to Java (Marshalling and Unmarshalling)

Wednesday, July 13th, 2011


JDK6 and JAXB2.x (which comes with JDK6) make marshalling Java to XML and unmarshalling XML to Java a snap, almost trivial.


Java to XML


import java.math.BigDecimal;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class JavaToXML {
	public static void main(String[] args) throws Exception {
		JAXBContext context = JAXBContext.newInstance(Product.class);

		Marshaller m = context.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

		Product object = new Product();
		object.setName("Widget Number One");

		m.marshal(object, System.out);

XML to Java



import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class XMLToJava {

	public static void main(String[] args) {
		try {
			JAXBContext jc = JAXBContext.newInstance(Product.class);
			Unmarshaller u = jc.createUnmarshaller();

			File f = new File("product.xml");
			Product product = (Product) u.unmarshal(f);

		} catch (JAXBException e) {

Note: the @XmlRootElement is vital here!


import java.math.BigDecimal;

import javax.xml.bind.annotation.XmlRootElement;

public class Product {
	private String code;
	private String name;
	private BigDecimal price;

	public String getCode() {
		return code;
	public void setCode(String code) {
		this.code = code;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public BigDecimal getPrice() {
		return price;
	public void setPrice(BigDecimal price) {
		this.price = price;


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <name>Widget Number One</name>

Tags: , ,
Posted in Examples | 8 Comments »

The Processing Instruction Target Matching “[xX][mM][lL]” is Not Allowed

Friday, June 24th, 2011


When parsing XML you receive the following error:

...The Processing Instruction Target Matching "[xX][mM][lL]" is Not Allowed...


The chances are you have some sort of whitespace (or control character) infront of your XML declaration:

..<?xml version="1.0" encoding="utf-8"?>

It may even be that you have more than one XML declaration in the document..!

Posted in quick tips | No Comments »

Read a File as String with Java

Thursday, June 16th, 2011


I’m always Googling for a way to do this. This seems to be the best “idiomatic” solution I’ve found. So without further ado…


public String readFile(String path) throws IOException {
    FileInputStream stream = new FileInputStream(new File(path));
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb =, 0, fc.size());
        return Charset.defaultCharset().decode(bb).toString();
    finally {

Posted in Development, How to's, quick tips | No Comments »

View Source of Oracle Trigger

Wednesday, May 11th, 2011

select trigger_body from user_triggers where trigger_name = 'XXXXX'

Tags: ,
Posted in quick tips | No Comments »

Unit Test Private Java Methods using Reflection

Thursday, May 5th, 2011


I realise that the thought of Unit Testing private methods in Java classes can be like a red rag to a bull for some people and I understand the arguments.

Therefore, I present the following as a “how to”, not a moral argument for or against!


The class under test

public class Product() {
    private String privateMethod(String id) {
    //Do something private
    return "product_" + id;

The (Reflection Based) Unit Test

import java.lang.reflect.Method;

import static org.junit.Assert.*;
import org.junit.Test;

public class ProductTest {
    private Product product; // the class under test
    private Method m;
    private static String METHOD_NAME = "privateMethod";
    private Class[] parameterTypes;
    private Object[] parameters;

    public void setUp() throws Exception {
        product = new Product();
        parameterTypes = new Class[1];
        parameterTypes[0] = java.lang.String.class;
        m = product.getClass().getDeclaredMethod(METHOD_NAME, parameterTypes);
        parameters = new Object[1];

    public void testPrivateMethod() throws Exception {
        parameters[0] = "someIdentifier";
        String result = (String) m.invoke(product, parameters); 
        //Do your assertions


I’ve since been told that if dp4j.jar is in the classpath at compile-time, it will inject the necessary reflection to make this work. I haven’t had time to try this yet so YMMV.

Tags: , ,
Posted in Development, How to's | No Comments »