inserting a JDOM-Object

Hello out there,
I have some questions about, how to inserting JDOM-Objects from servlets into the database. First I created a JDOM-Object:

import org.jdom.*;
Document mydoc = new Document ( new Element ("Anwender") );
	Element root   = mydoc.getRootElement();
	Element erstesElement = new Element ("vorname").setText(vorname);
	root.addContent( erstesElement );
	Element zweitesElement = new Element ("nachname").setText(nachname);
	root.addContent( zweitesElement );
	Element drittesElement = new Element ("wohnort").setText(wohnort);
	root.addContent( drittesElement );

Then I buildet a connection to a database ( this works, no need to dump here ) and a accessor-Object.

TXMLObjectAccessor xmlObjectAccessor = connection.newXMLObjectAccessor(
            TAccessLocation.newInstance( "ino:etc" ),
            TJDOMObjectModel.getInstance() );

But when I try to fill this accessor with my JDOM-Object…

xmlObjectAccessor.insert( mydoc );


it doesn’t work.
Do I have to build a TXML-Object first and commit it to the accessor?
How to do this?

Gruss Christian

It doesn’t work you mean it can’t even compile?
TXMLObjectAccessor.insert(TXMLObject)

Yes you should use a TXMLObject. Read it from a File, URL, Stream, Reader, etc. In the following example I get a TXMLObject instance and then read it from a stream(piped). Using this way I know for sure what model is used.


PipedInputStream pin=new PipedInputStream();
PipedOutputStream pout=new PipedOutputStream(pin);
//Write your document to pout using a thread....
TXMLObject xmlOb = TXMLObject.newInstance(TJDOMObjectModel.getInstance());
xmlOb.readFrom(pin);
//Remember to close streams

The wrong thing about the API4J is that all the documents always come from a stream or similar. So doesn’t matter wich technique you use to build your document, you should also care about TXMLObjectModel. It doesn’t reuse your data.

Can some one explain big differences using different XMLObjectModels and consecuences on XMLObjects.

Yes this works now, thank you.

I made this: create a JDOM-Object:

Document mydoc = new Document ( new Element ("Anwender") );
	Element root   = mydoc.getRootElement();
	Element erstesElement = new Element ("vorname").setText(vorname);
	root.addContent( erstesElement );
	Element zweitesElement = new Element ("nachname").setText(nachname);
	root.addContent( zweitesElement );
	Element drittesElement = new Element ("wohnort").setText(wohnort);
	root.addContent( drittesElement );

then created a TXMLObject and fill it with the JDOM.

TXMLObject tobj = TXMLObject.newInstance ( mydoc);

… and give this object the accessor:

xmlObjectAccessor.insert( tobj );

But this is a littles bit “pedestrian”! I would prefer a possibility where I can create a TXMLObject and fill it with elements.

Gruss Christian

I haven’t found a way to do so. As I say that’s one of the bad things in the API4J in Tamino. There is no way to access the underlaying Stream,DOM,JDOM behind the XMLObject.
XMLObject is only for the transaction not for data manupulating.

I just discover that you can ‘reuse’ your DOM or JDOM. There’s a way to create a TXMLObject. I don’t know if you can still edit the JDOM or DOM, in fact I think no.
Use Method TXMLObject.newInstance(Object object)
As described in JavaDoc
Factory method, creates a TXMLObject instance from the given object instance. The type of the input object determines the object model for the TXMLObject instance returned. For example, if the input object is an instance of org.w3c.dom.Element or org.w3c.dom.Document, the DOM object model is taken. If the input element is an instance of org.jdom.Element or org.jdom.Document, the JDOM object model is taken. The XML content of the input object is taken as the XML content of the TXMLObject instance.

Parameters:
    object - an object representing the XML document. 
Returns:
    TXMLObject that wraps the given object.