Author |
Message
|
DevNYC1 |
Posted: Fri Feb 01, 2013 6:56 pm Post subject: Putting XML content on the output Q in Java Compute Node |
|
|
Apprentice
Joined: 22 Aug 2012 Posts: 32
|
I understand how to create XML element by element and place the produced XML on the outbound Q. However, I need to do something different. In my Java Compute Node I am calling a utility that returns already generated XML as a String and I need to write this XML to the Q (MQOutput node)
I thought I can just do this (assume that xml variable contains the XML structure):
Code: |
MbElement outParser = outRoot.createElementAsLastChild(MbXMLNSC.PARSER_NAME);
outRoot.createElementAsFirstChild(xml);
|
But that does not work - nothing is written to the Q. I could not find any articles on this. Any help would be appreciated. |
|
Back to top |
|
 |
DevNYC1 |
Posted: Fri Feb 01, 2013 7:14 pm Post subject: |
|
|
Apprentice
Joined: 22 Aug 2012 Posts: 32
|
Found the solution (posting in case someone else finds this useful):
Code: |
byte []msgBytes = xml.getBytes();
outRoot.createElementAsLastChildFromBitstream
(msgBytes,MbXMLNSC.PARSER_NAME,"","","",0,0,0); |
|
|
Back to top |
|
 |
fjb_saper |
Posted: Sat Feb 02, 2013 4:54 am Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
You sure about your code?
I would have thought you'd parse attaching to outParser and not outRoot...  _________________ MQ & Broker admin |
|
Back to top |
|
 |
DevNYC1 |
Posted: Sat Feb 02, 2013 6:32 am Post subject: |
|
|
Apprentice
Joined: 22 Aug 2012 Posts: 32
|
|
Back to top |
|
 |
kimbert |
Posted: Sat Feb 02, 2013 1:44 pm Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
1. This code is not safe (although it is very common):
Code: |
byte []msgBytes = xml.getBytes();
outRoot.createElementAsLastChildFromBitstream
(msgBytes,MbXMLNSC.PARSER_NAME,"","","",0,0,0); |
You should almost never call the single-parameter version of java.lang.String.getBytes(). You cannot guarantee the character encoding of the bytes that will be returned. It is much safer to specify an encoding using the two-parameter version.
2. I'm not convinced that you need to use XMLNSC at all. If you have been given a valid XML string then why not convert it to a BLOB ( in the encoding required by the consumer ) and then output that BLOB using the BLOB domain?
If you cannot trust the service that returned the string, and you want to validate the returned XML then you should continue to use XMLNSC. |
|
Back to top |
|
 |
DevNYC1 |
Posted: Sat Feb 02, 2013 2:16 pm Post subject: |
|
|
Apprentice
Joined: 22 Aug 2012 Posts: 32
|
kimbert wrote: |
You should almost never call the single-parameter version of java.lang.String.getBytes(). You cannot guarantee the character encoding of the bytes that will be returned. It is much safer to specify an encoding using the two-parameter version |
Thanks so much for this advice. The output Q's application expects the standard XML document. What is the appropriate encoding that I should specify in the getBytes method for the standard XML file? If that is relevant for the charset, the QA and Prod environments and MQs are on the AIX Unix and we develop on Windows XP Pro.
Quote: |
I'm not convinced that you need to use XMLNSC at all. If you have been given a valid XML string then why not convert it to a BLOB ( in the encoding required by the consumer ) and then output that BLOB using the BLOB domain? |
the consumer expects a valid XML document that can be validated against the XSD. I am inclined to use the XMLNSC if it works. |
|
Back to top |
|
 |
kimbert |
Posted: Sun Feb 03, 2013 12:58 pm Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
Quote: |
The output Q's application expects the standard XML document. What is the appropriate encoding that I should specify in the getBytes method for the standard XML file? |
This is something that you should either know, or should be able to look up in a specification document. I cannot possibly tell you what encoding you should use.
Does the XML that you are emitting begin with an XML declaration? If so, does it have an 'encoding' attribute? Because it if does, it would be a good idea to emit the XML in the encoding that the document claims to be using.
Quote: |
the consumer expects a valid XML document that can be validated against the XSD. I am inclined to use the XMLNSC if it works. |
Fair enough. If the source cannot be trusted then using XMLNSC is a good way to ensure that you emit well-formed XML that conforms to the XSD. |
|
Back to top |
|
 |
rekarm01 |
Posted: Sun Feb 03, 2013 3:45 pm Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 1415
|
kimbert wrote: |
You should almost never call the single-parameter version of java.lang.String.getBytes(). ... It is much safer to specify an encoding using the two-parameter version. |
Good advice, but slightly off: You should almost never call the zero-parameter version of java.lang.String.getBytes(). ... It is much safer to specify a charset using the one-parameter version.
DevNYC1 wrote: |
What is the appropriate encoding that I should specify in the getBytes method for the standard XML file? |
That's to be decided between the sending and receiving applications, based upon whatever charsets they both support. In the absence of any other requirements, "UTF-8" works well for most applications. (Charset names are case-insensitive).
More importantly, the createElementAsLastChildFromBitstream() ccsid parameter must indicate the specified charset. For example, set ccsid=1208 for the "UTF-8" charset, set ccsid=819 for the "ISO-8859-1" charset, etc. Setting ccsid=0 defaults to the qmgr's ccsid, which may or may not be suitable; if the ccsid and charset don't match, the output message may be partially or completely garbled.
DevNYC1 wrote: |
the consumer expects a valid XML document that can be validated against the XSD. |
If the message flow is validating the output message against a schema, then either set the createElementAsLastChildFromBitstream() messageSet and messageType parameters to identify the schema and message element to validate against, or make sure to set these values elsewhere. |
|
Back to top |
|
 |
|