Archive for the ‘How to’s’ Category

Oracle XML DB – extracting relational data using XMLTables

Wednesday, April 22nd, 2015

Background

So say you’ve got some XML in an XMLType table. You want to get the data into a “normal” relational table (maybe for faster reporting etc)

The XML looks like this:

<!-- this is one row in the XML table (xml_table) -->
<mxrecords>
   <mxrecord>
       <sysurn></sysurn>
       <description></description>
   </mxrecord>
   <mxrecord>
       <sysurn></sysurn>
       <description></description>
   </mxrecord>
   <mxrecord>
       <sysurn></sysurn>
       <description></description>
   </mxrecord>
</mxrecords>

<!-- this is another row in the XML table -->
<mxrecords>
   <mxrecord>
       <sysurn></sysurn>
       <description></description>
   </mxrecord>
   <mxrecord>
       <sysurn></sysurn>
       <description></description>
   </mxrecord>
   <mxrecord>
       <sysurn></sysurn>
       <description></description>
   </mxrecord>
   <mxrecord>
       <sysurn></sysurn>
       <description></description>
   </mxrecord>
</mxrecords>

The relational table looks like this:

create table reporting(
    sysurn varchar2(50),
    description varchar2(4000)
);

Lets rip the XML into the relational table:

insert into reporting
select mxrecord.* from xml_table xt,
    XMLTABLE(
        '/mxrecords/mxrecord'
        passing xt.OBJECT_VALUE
        columns mxrecord XMLTYPE PATH '/mxrecord') mxrecords,
    XMLTABLE(
        '/mxrecord'
        passing mxrecords.mxrecord
        columns sysurn varchar2(50) PATH '/mxrecord/sysurn',
                description varchar2(4000) PATH '/mxrecord/description') mxrecord

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

Oracle XML DB – Inserting XML

Friday, March 20th, 2015

Background

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 TABLE XML_TABLE OF XMLType 

-- 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:

INSERT INTO XML_TABLE VALUES(XMLType('<mxrecords>
        <mxrecord>
            <sysurn>sysurn1</sysurn>
            <eventid>eventid1</eventid>
        </mxrecord>
        <mxrecord>
            <sysurn>sysurn2</sysurn>
            <eventid>eventid2</eventid>
        </mxrecord>
    </mxrecords>'
));

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

Oracle XML DB – XMLType table creation

Wednesday, March 18th, 2015

Background

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
BEGIN
  DBMS_XMLSCHEMA.registerSchema(SCHEMAURL => 'http://www.foobar.com/xsd/my_schema.xsd', SCHEMADOC => bfilename('XSD','my_schema.xsd'));
END;

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

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

BEGIN
  DBMS_XMLSCHEMA.registerSchema(
    http://www.foobar.com/xsd/my_schema.xsd',
    bfilename('XSD','my_schema.xsd'),
    TRUE, -- generate required Oracle types. Default = true
    TRUE, -- generate javabeans. Default = false
    FALSE, -- generate errors when registering schema. True = do not
    TRUE -- generate table
);
END;

Full docs for the DBMS_XMLSCHEMA package are here
here

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

Read a File as String with Java

Thursday, June 16th, 2011

Introduction

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…

Example

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

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

Unit Test Private Java Methods using Reflection

Thursday, May 5th, 2011

Introduction

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!

Example

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;

    @Before
    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);
        m.setAccessible(true);
        parameters = new Object[1];
    }

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

Update

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 »