Archive for the ‘Examples’ Category

Oracle XML DB – Inserting XML

Friday, March 20th, 2015


So following on from my previous post about creating XMLType tables in Oracle (either CLOB or schema based), here’s a few quick notes on getting data into the tables.

Inserting Data into XMLType Table

-- Create XMLType Table

-- create an Oracle directory to hold the XML (readable by Oracle)
CREATE OR REPLACE DIRECTORY XML as '/home/foo/bar/xml'

-- insert the record (reading from filesystem)
-- filename.xml would be in directory you created above
INSERT INTO XML_TABLE(SELECT XMLTYPE(bfilename('XML', 'filename.xml'), nls_charset_id('UTF8')) FROM dual);

Of course you could easily write some PL/SQL to iterate all files in the directory.

You can also insert XML directly via SQL:


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

Oracle XML DB – XMLType table creation

Wednesday, March 18th, 2015


So I’ve been doing some Oracle XML work again. I’d lost my notes from last time (nice touch) so here are my notes from this time:

XMLType Table Creation

CREATE TABLE xml_table OF XMLType 

This will create “an xml table” into which you can insert XML. It will check that the XML is well formed before insertion. It will not validate that all documents inserted are the same or that elements contain valid data.

For that we need to create a table based upon an XML Schema/XSD

-- create an Oracle directory where the schema can be stored
create or replace directory XSD as '/home/foo/bar/oracle/xsd'

-- copy your Schema into the directory
-- then
  DBMS_XMLSCHEMA.registerSchema(SCHEMAURL => '', SCHEMADOC => bfilename('XSD','my_schema.xsd'));

-- Note: the SCHEMAURL can be basically anything. Oracle uses it as a unique identifier
-- Create the table
CREATE TABLE matter OF xml_table XMLSCHEMA "" ELEMENT "nameOfTheRootXmlElement";

There are many overloaded versions of DBMS_XMLSCHEMA.registerSchema so you can shortcut some of the steps above. For example:

    TRUE, -- generate required Oracle types. Default = true
    TRUE, -- generate javabeans. Default = false
    FALSE, -- generate errors when registering schema. True = do not
    TRUE -- generate table

Full docs for the DBMS_XMLSCHEMA package are here

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

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 | 7 Comments »

Unit Testing Validation in Annotation Based Validating Spring Beans

Friday, April 22nd, 2011


I added validation, via annotations, to a Spring “Model” bean. I needed someway to Unit Test this validation, without running the container and without initialising the Spring context.

The bean (simplified)


import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

public final class ProductModel {
    @Pattern(regexp="[^\n^\t^\r]+", message="Long Name must not contain New Lines, Carriage Returns or Tabs")
    private String longName;

    private String shortName;

    // rest snipped for brevity

The Unit Test


import java.util.Set;

import javax.validation.ConstraintViolation;

import junit.framework.Assert;

import org.hibernate.validator.HibernateValidator;
import org.junit.Before;
import org.junit.Test;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

public class Temp {
    private LocalValidatorFactoryBean localValidatorFactory;
    public void setup() {
        localValidatorFactory = new LocalValidatorFactoryBean();
    public void testLongNameWithInvalidCharCausesValidationError() {
        final ProductModel productModel = new ProductModel();
        productModel.setLongName("A long name with\t a Tab character");
    	Set<ConstraintViolation<ProductModel>> constraintViolations = localValidatorFactory.validate(productModel);
    	Assert.assertTrue("Expected validation error not found", constraintViolations.size() == 1);


It actually took me a few hours to work the above test out (simple as it is). If I hadn’t stumbled upon these Spring Unit Tests, I might never have got it.

Tags: , , , ,
Posted in Spring | 2 Comments »

Spring MVC URI Template Example

Monday, April 18th, 2011


So, I’m working on an existing Spring 3.x web project. This project heavily favours the use of annotations over XML configuration. As I was trawling through the source code I noticed “some wierd” syntax: “/{xxxx}” (which of course I had to look up!).

Most of the following is based upon the excellent Spring Documentation. I’ve lifted and rewritten the parts I require to serve as a condensed “aide-mémoire”.


A URI Template is a URI-like string, containing one or more variable names. When you substitute values for these variables, the template becomes a URI.

Tags: , ,
Posted in Spring | No Comments »