Discussions

EJB design: How to pass xml documents from one EJB to another EJB?

  1. Hi All,

    I am trying to pass XML Document (org.w3c.dom.Document) from one EJB to another EJB. While trying to pass XML Document from one EJB to another EJB, I got NotSerializableException? Later I found that org.w3c.dom.Document is not serializable. Then I thought I can convert the xml to string and pass it. But I couldn't find any API's for directly parsing the xml string (I am using JAXP and also I found that almost all the java based parsers does not support parsing of string which contains valid and welformed xml content). Since it is not possible to use IO classess or read from file system in EJB, can anybody suggest me any alternatet ways to do this.

    (I had gone through the "XML generation from EJB" pattern, found in this site, but there I couldn't find any discussion on how to pass the xml documents between EJB's as the following two cases are rulled out i.e. whatever we are exchanging between EJB should be serializable and as of now well known java based parsers (jaxp, xerces etc.) doesn't support the parsing of xml string.)

    Thanks in advance.

    Regards,

    Jayakumar
  2. Hi,

    >But I couldn't find any API's for directly parsing the xml >string (I am using JAXP and also I found that almost all >the java based parsers does not support parsing of string >which contains valid and welformed xml content).

    I may sugest you to use 'jdom' for xml parsing.

    For more information about 'jdom' read in:
    http://www.jdom.org/

    Good luck.

    Vaidas
  3. Hi Vaidas,

    I checked the JDOM JavaDoc, but I couldn't able to find API which parses the string that contains valid xml content. Is that I am making the mistake? Can you please tell me which class I have to use?

    Thanks in advance.

    Regards,

    jayakumar

  4. In this short sample I will try to show, how it is possible
    to create document using jdom SAXBuilder. In case 1 data is
    read from xml file; in case 2 data is read from string.
    Hope it will help you. I have tried that - it works.

    // Sample
     {
     ...
           
     SAXBuilder bldr = new SAXBuilder("org.apache.xerces.parsers.SAXParser");

     org.jdom.Document mDocument;
     // case 1: create document from xml file
     mDocument = bldr.build(new File(inFileName));
     // Create xml outputer
     XMLOutputter out = new XMLOutputter(" ", true);
     String xmlStr = out.outputString(mDocument);
     // Print xml string (for debug)
     System.out.println(xmlStr);
     // case 2: create document form string
     mDocument = bldr.build(new StringReader(xmlStr));
     // Print document (for debug)
     out.output(mDocument, System.out);
     
     ...
     }

    Regards,
    Vaidas
  5. Hi Jay,

    I will describe my approach in one of my project. Lets take ejb1 has to pass XML to EJB2. Construct a String object which is just the content as in there in XML file. If u print the String it will be same as XML. Then I open a Stream and pass this String Object thru the stream. In EJB2 I used this String object & constructed the DOM object and used it.

    Let me get ur i/ps.

    bye,

    Shiva
  6. Hi shivanand hiremath,

    Thanks for your kind reply. I want to ask you one thing, how you used StringReader in EJB (as we can't use IO packages inside EJB).

    Also I know how to convert the xml document to string, but I want to know how to convert that string to create a xml document (without using IO streams).

    Waiting for your reply.....

    Regards,

    Jayakumar
  7. Parsing the XML document back and forth to a string is going to kill your performance. (And I mean _really_ kill it.)

    The problem is not that org.w3c.dom.Document doesn't implement Serializable (you are correct though, it doesn't.)

    The problem is that your implementation of this interface doesn't implement Serializable. If you are using JAXP from Sun then I know it doesn't. I had this exact same problem, after getting the client to agree that the return type from my methods would be the exact interface you mention.

    However, after a minor heart attack, I realized that we weren't going to use JAXP in production, we were going to use Xerces and IBM XML4J. One quick wander through the Xerces documentation and....

    ...*phew* The Xerces classes DO implement the Serializable interface. Problem solved!

    Use these and you'll be absolutely fine. We do on the project I am using, and it works wonderfully (and is approximately 10 times quicker than parsing things in and out of Strings.

    Hope that helps

    Chz

    Tony
  8. Hi Tony,

    Thanks a lot Tony. I followed the model rather than converting to string etc.. Thanks again.

    Regards,

    Jayakumar
  9. Only other thing to note is that you are not allowed to do I/O in an EJB, that's not the same thing as not using any of the classes from java.io.

    StringArrayInputStream is OK, since it's not doing I/O.

    Chz

    Tony