Author |
Message
|
wyatt |
Posted: Wed Oct 18, 2006 5:14 am Post subject: navigating XML message in Java Compute Node |
|
|
Voyager
Joined: 28 Nov 2004 Posts: 76
|
Hi Folks
Anyone know how to get the entire XML document using provided methods within MB java compute node?
MbElement root = assembly.getMessage().getRootElement();
MbElement document = root.getLastChild().getFirstChild();
MbElement chapter = document.getFirstChild(); // returns the first chapter
as documented in http://www-128.ibm.com/developerworks/websphere/library/techarticles/0605_crocker/0605_crocker.html
Currently, I have a working kludge as follows:
byte[] msgBytes = inMessage.getBuffer();
String msgString = new String(msgBytes);
String xmlMessage = msgString.substring(msgString.indexOf("<")); |
|
Back to top |
|
 |
jefflowrey |
Posted: Wed Oct 18, 2006 5:21 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
Whaddya mean, "the entire XML document"?
What you get inside a JCN is the logical message tree - just like what you get inside a ComputeNode.
In either case, you can ask for the bit stream that the logical message tree represents. In ESQL, this is done using the ASBITSTREAM function, and in Java it's done basically the way you've shown.
There's no way, that I'm aware of, to get the Document object that the internal parser used to build the logical message tree from a bistream that contained XML data.
But regardless, you can navigate and manipulate the logical message tree - without needing to access the bitstream.
In the JavaCompute node, you can use XPath - at least a subset of XPath 1.0 - to navigate and manipulate the message tree. Or you can use the various methods on MbElement and MbAssembly and etc. to do this.
What are you actually trying to do with "the entire XML document"? _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
wyatt |
Posted: Wed Oct 18, 2006 5:38 am Post subject: |
|
|
Voyager
Joined: 28 Nov 2004 Posts: 76
|
..using xstream to convert the XML into a java object |
|
Back to top |
|
 |
jefflowrey |
Posted: Wed Oct 18, 2006 5:59 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
...
why?
...
Again, you can get the bitstream - which you basically are already doing and it's not a kludge - but you can't get a w3c DOM Document or etc without creating one from the bitstream.
But the logical message tree is already "a Java object", and you can navigate it as such. If you're trying to call some "reusable" Java code that accepts a particular object you may be computationally better off writing something that accesses the necessary fields of the logical tree and inserts them into that Java object than reparsing the bitstream.
If the Java object is complicated, you may be better off exposing the "reusable" Java code as a WebService and invoking it that way.
If you're just trying to create a Java object that you can then access from your code in the JavaCompute node - then you really should just navigate the logical message tree. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
wyatt |
Posted: Wed Oct 18, 2006 7:01 am Post subject: |
|
|
Voyager
Joined: 28 Nov 2004 Posts: 76
|
The java object will be passed to a different team. This team has designed a set of "services" that accept java objects as part of the service signature - the service applies business rules and persistence to DB. The java compute node contains about 10 lines of code, it converts XML to Java Objects; i.e. serializes the XML data.
I don't know what you mean by "But the logical message tree is already "a Java object", and you can navigate it as such". Is there a better way to create Java Objects in MB? |
|
Back to top |
|
 |
mlafleur |
Posted: Wed Oct 18, 2006 7:06 am Post subject: |
|
|
Acolyte
Joined: 19 Feb 2004 Posts: 73
|
You can use the toBitstream method of MbElement.
For example,
byte[] xml = msg.getRootElement().getLastChild().toBitstream(null, null, "XMLNS", 0, 0, 0); |
|
Back to top |
|
 |
jefflowrey |
Posted: Wed Oct 18, 2006 7:11 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
You're going to have some fun trying to get those services to run inside Message Broker.
Especially if their persistance layer tries to use a Type 2 JDBC driver, or if they are written using J2EE.
Your JavaCompute node is going to run in a JVM that is specific to the ExecutionGroup that your flow is deployed to.
The XML Data that your JavaCompute node receives will have already been un-marshalled (not serialized!) to MbElement Java objects. You're not serializaing and unserializing the data, you're marshalling it from XML to a Java object, and unmarshalling it from a Java object to XML data.
That's why you have to get the bitstream - because you're trying to marshal it back to XML data.
Instead of remarshalling the XML data into a bitstream and then passing it to XStream to unmarshall back into an instance of a specific Java object of a specific class, you can create an empty Java object of that specific class.
Then you can use the methods on MbElement and MbNode to navigate to the specific part of the XML data you need for each field in the Java object that you need to pass to this "service". Then you can call a setter on the Java object to apply the vlaue from the MbElement. Think of this as a "copy constructor" rather than an unmarshalling step.
But again, you may have some difficulty getting these "services" to run in the WMB execution group JVM.
And a Java "interface" does not a "service" make. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
wyatt |
Posted: Wed Oct 18, 2006 8:09 am Post subject: |
|
|
Voyager
Joined: 28 Nov 2004 Posts: 76
|
mlafleur wrote: |
You can use the toBitstream method of MbElement.
For example,
byte[] xml = msg.getRootElement().getLastChild().toBitstream(null, null, "XMLNS", 0, 0, 0); |
That works, Thx |
|
Back to top |
|
 |
jefflowrey |
Posted: Wed Oct 18, 2006 8:12 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
That assumes that you're using the XMLNS parser.
In WMB v6, you should probably use the XMLNSC parser. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
wyatt |
Posted: Wed Oct 18, 2006 8:52 am Post subject: |
|
|
Voyager
Joined: 28 Nov 2004 Posts: 76
|
jefflowrey wrote: |
That assumes that you're using the XMLNS parser.
In WMB v6, you should probably use the XMLNSC parser. |
I made the necessary adjustment
Thanks for the insight/suggestions |
|
Back to top |
|
 |
|