ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Handle XML in Java

Post new topic  Reply to topic Goto page 1, 2  Next
 Handle XML in Java « View previous topic :: View next topic » 
Author Message
newbee001
PostPosted: Wed Aug 17, 2016 9:21 am    Post subject: Handle XML in Java Reply with quote

Novice

Joined: 04 Aug 2016
Posts: 10

Hi,

I've a requirement to insert 'xml' data from Java compute node to the DB2 table column of data type 'xml'.

Please guide me on how to write the code to insert xml data directly.

Thanks in advance.
Back to top
View user's profile Send private message
mqjeff
PostPosted: Wed Aug 17, 2016 9:28 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

Steps
  1. Read the documentation on the JavaCompute node
  2. Read the documentation on mapping from IIB types to database types
  3. determine what types are supported
  4. Read the documentation on performing database updates in a JavaCompute node

_________________
chmod -R ugo-wx /
Back to top
View user's profile Send private message
Vitor
PostPosted: Wed Aug 17, 2016 9:28 am    Post subject: Re: Handle XML in Java Reply with quote

Grand High Poobah

Joined: 11 Nov 2005
Posts: 26093
Location: Texas, USA

newbee001 wrote:
I've a requirement to insert 'xml' data from Java compute node to the DB2 table column of data type 'xml'.


Why? Why from the JCN rather than the Compute node or (if you're a JDBC user) the Database node?

newbee001 wrote:
Please guide me on how to write the code to insert xml data directly.


What code have you tried? What happened? Why wasn't that what you wanted?
_________________
Honesty is the best policy.
Insanity is the best defence.
Back to top
View user's profile Send private message
newbee001
PostPosted: Wed Aug 17, 2016 10:40 am    Post subject: Reply with quote

Novice

Joined: 04 Aug 2016
Posts: 10

Hi,

Thanks jeff for your suggestion.

yes vitor, its a jdbc user and hence need to develop in java..

below is my xml structure

Properties
MQMD
XMLNSC
<employees>
<employee1>
<name>tom</name>
...
...
</employee1>
<otheremployees>
<empdetails><name>kevin</name>... <name>Ross</name></empdetails>
</otheremployees>
</employees>

Code is written as below -

MbElement root = inMessage.getRootElement();
MbElement otheremployees = rootMsg.getLastChild().getFirstChild();
byte[] mybytes=(byte[]) otheremployees.getValue();
String msgBdy = new String(mybytes);

I'm getting null pointer exception here.. 'empdetails' xml is not converted to byte format. Any suggestions pls?

Thanks in advance
Back to top
View user's profile Send private message
Vitor
PostPosted: Wed Aug 17, 2016 11:27 am    Post subject: Reply with quote

Grand High Poobah

Joined: 11 Nov 2005
Posts: 26093
Location: Texas, USA

newbee001 wrote:
yes vitor, its a jdbc user and hence need to develop in java..


So why not use the Database node?

newbee001 wrote:
I'm getting null pointer exception here.. 'empdetails' xml is not converted to byte format. Any suggestions pls?


I'm by no measure the best person to speak about Java. Having said that, are you sure getValue works on complex XML elements and, once you get it working, how do you plan to get a byte array into an XML type column?
_________________
Honesty is the best policy.
Insanity is the best defence.
Back to top
View user's profile Send private message
newbee001
PostPosted: Wed Aug 17, 2016 11:51 am    Post subject: Reply with quote

Novice

Joined: 04 Aug 2016
Posts: 10

hi victor,

Database node will accept only ODBC datasource name. Please correct me if i'm wrong.

we have JDBC provider defined for DB connection and JCN seems the better choice..

https://developer.ibm.com/answers/questions/187138/can-i-connect-to-an-application-database-using-jdb.html

Yes, I've overcome the nullpointer exception

It seems DB2 XML is BLOB in ESQL and
BLOB in ESQL is equiv to byte[] in Java.

https://www.ibm.com/support/knowledgecenter/SSMKHH_9.0.0/com.ibm.etools.mft.doc/ak05730_.htm

My question is, If i send xml in byte[] from Java to the DB2 column having XML datatype, will it work?

Please clarify.

Thanks


Last edited by newbee001 on Wed Aug 17, 2016 12:00 pm; edited 1 time in total
Back to top
View user's profile Send private message
mqjeff
PostPosted: Wed Aug 17, 2016 12:00 pm    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

An MbElement doesn't always have a value.
Your code
Code:
byte[] mybytes=(byte[]) otheremployees.getValue();
assumes that it does.

An MbElement isn't a serializable object. You need to take an extra step to get it as a bitstream.
_________________
chmod -R ugo-wx /
Back to top
View user's profile Send private message
newbee001
PostPosted: Wed Aug 17, 2016 12:03 pm    Post subject: Reply with quote

Novice

Joined: 04 Aug 2016
Posts: 10

yes jeff, the pbm is resolved after changing the code as below -

String ccsid = inMessage.getRootElement().getFirstElementByPath("Properties/CodedCharSetId").getValueAsString();
Integer ccsidValue = new Integer(ccsid);
byte[] mybytes=(byte[]) otheremployees.toBitstream(null, null, null, 0, ccsidValue, 0);
String str = new String(mybytes);

however getting exception while sending byte[] to DB2 column having 'xml' datatype.

As posted in previous msg, can I send xml as byte[] from JCN to Db2 column having 'xml' datatype? pls clarify.
Back to top
View user's profile Send private message
Vitor
PostPosted: Wed Aug 17, 2016 12:10 pm    Post subject: Reply with quote

Grand High Poobah

Joined: 11 Nov 2005
Posts: 26093
Location: Texas, USA

newbee001 wrote:
Database node will accept only ODBC datasource name. Please correct me if i'm wrong.


No, I'm wrong, would have sworn it took a JDBC not an ODBC.

we have JDBC provider defined for DB connection and JCN seems the better choice..

newbee001 wrote:
It seems DB2 XML is BLOB in ESQL

https://www.ibm.com/support/knowledgecenter/SSMKHH_9.0.0/com.ibm.etools.mft.doc/ak05730_.htm


Yes it is, as that link indicates.

newbee001 wrote:
BLOB in ESQL is equiv to byte[] in Java.


As indicated here

newbee001 wrote:
My question is, If i send xml in byte[] from Java to the DB2 column having XML datatype, will it work?


Try it and see. Let us know how it works out for you.

I personally doubt that a byte array will match the "internal representation" mentioned here, and I'm interested to see if the byte array counts as a binary data type (as described in that same link). Certainly the IIB data center indicates that it ought to work.

I wonder about the CCSID that the byte stream has vis a vis the database.
_________________
Honesty is the best policy.
Insanity is the best defence.
Back to top
View user's profile Send private message
newbee001
PostPosted: Wed Aug 17, 2016 1:17 pm    Post subject: Reply with quote

Novice

Joined: 04 Aug 2016
Posts: 10

I'm getting below Db2 exception by passing xml in string/byte[] format from JCN -

SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=XMLSTR

however am able to run below SQL successfully from JCN -
stmt.executeUpdate("insert into emp(xmlin) values('<test><name>sss</name></test>')");

In the below code, str has the same value - <test><name>sss</name></test>.. however not sure why if this passed as a parameter to SQL query is failing in JCN.. any thoughts?

String ccsid = inMessage.getRootElement().getFirstElementByPath("Properties/CodedCharSetId").getValueAsString();
Integer ccsidValue = new Integer(ccsid);
byte[] mybytes=(byte[]) otheremployees.toBitstream(null, null, null, 0, ccsidValue, 0);
String str = new String(mybytes);
Back to top
View user's profile Send private message
fjb_saper
PostPosted: Wed Aug 17, 2016 1:43 pm    Post subject: Reply with quote

Grand High Poobah

Joined: 18 Nov 2003
Posts: 20696
Location: LI,NY

newbee001 wrote:
yes jeff, the pbm is resolved after changing the code as below -

Code:
          String ccsid = inMessage.getRootElement().getFirstElementByPath("Properties/CodedCharSetId").getValueAsString();
          Integer ccsidValue = new Integer(ccsid);
          byte[] mybytes=(byte[]) otheremployees.toBitstream(null, null, null, 0, ccsidValue, 0);
          String str =  new String(mybytes);




Your code is dangerous.
You are transforming the XML into a byte[] with anonymous (but known CCSID). (InputRoot.Properties.CodedCharSetId). I'd just have mandated UTF-8 with ccsid 1208 ...
Then you are creating a String from the byte[] using the platform's default ccsid??? and just pray to God that they are the same???
You should use the form new String (byte[] mybytes, String CharsetName)...
Code:
String str = new String (mybytes, "UTF-8");

Now can you tell me the Java name of the charset defined by the CCSID on the InputRoot???
Have fun
_________________
MQ & Broker admin
Back to top
View user's profile Send private message Send e-mail
newbee001
PostPosted: Wed Aug 17, 2016 3:34 pm    Post subject: Reply with quote

Novice

Joined: 04 Aug 2016
Posts: 10

Thanks, the problem is resolved.

Is this correct? pls advise.
byte[] mybytes=(byte[]) emp.toBitstream(null, null, null, 546, 1208, 0);
String empStr = new String(mybytes,"UTF-8");
Back to top
View user's profile Send private message
fjb_saper
PostPosted: Wed Aug 17, 2016 9:20 pm    Post subject: Reply with quote

Grand High Poobah

Joined: 18 Nov 2003
Posts: 20696
Location: LI,NY

newbee001 wrote:
Thanks, the problem is resolved.

Is this correct? pls advise.
byte[] mybytes=(byte[]) emp.toBitstream(null, null, null, 546, 1208, 0);
String empStr = new String(mybytes,"UTF-8");


Yes this looks right
_________________
MQ & Broker admin
Back to top
View user's profile Send private message Send e-mail
Vitor
PostPosted: Thu Aug 18, 2016 5:57 am    Post subject: Reply with quote

Grand High Poobah

Joined: 11 Nov 2005
Posts: 26093
Location: Texas, USA

Vitor wrote:
I wonder about the CCSID that the byte stream has vis a vis the database.


*smug*


_________________
Honesty is the best policy.
Insanity is the best defence.
Back to top
View user's profile Send private message
fjb_saper
PostPosted: Thu Aug 18, 2016 5:07 pm    Post subject: Reply with quote

Grand High Poobah

Joined: 18 Nov 2003
Posts: 20696
Location: LI,NY

He is not talking about it. He transformed the byte[] into a String. My guess is that he is sending a String downstream to the DB and the DB must know how to handle a Java String...
_________________
MQ & Broker admin
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic  Reply to topic Goto page 1, 2  Next Page 1 of 2

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Handle XML in Java
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.