title: How to structure Spring code for JAXB marhalling and proccessing of XML
body: <p><strong>The Question</strong></p>
  
  <p>I'm new to Java and spring and I'd like to know how to structure code which marshals different objects from XML and then processes them. I'm converting some code JAVA code from before my time to use spring. I know the way I've approached this this probably, wrong but If someone could offer a few pointers on how to restructure things "the Spring way" that would help alot. I've read a lot of the Spring docs, but I'm finding it hard to apply what is in there to my code.</p>
  
  <p><strong>The Situation</strong></p>
  
  <p>I'm not going to post the whole code tree as even and simple example is a lot of code (which is the problem). So I'll just describe the method.</p>
  
  <p>I've got XML and schemas for two classes CLASSA &amp; CLASSB. I've generated JAVA wrappers using xjc. I've got a JAVA class which is a wrapper for the JAXB marshaller. The wrapper needs to be given the class name and the package name of the class to be marshalled on construction.</p>
  
  <pre><code>public JaxbWrapper(String ClassName, String packageName) throws ClassNotFoundException, InstantiationException,
          IllegalAccessException, JAXBException
  {
  
      this.packageName = packageName;
  
              // set the context is the expensive operation
      setJAXBContext(Class.forName(fullJaxbClassName(ClassName)).newInstance());
  
              // get the schma from preset schema dir
      schemaFileName = findSchema(ClassName); 
      myMarsheller = jc.createMarshaller(); 
      myMarsheller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  }
  </code></pre>
  
  <p>I've then got one bean for each instance of the JaxBWrapper for both ClassA and ClassB. They are almost identical so only the one for class A is shown.</p>
  
  <pre><code>&lt;bean id="jaxbParser_CLASSA" class="org.test.JAXB.JaxbWrapper"
      lazy-init="true" scope="prototype"&gt;
      &lt;constructor-arg index="0" type="java.lang.String"
          value="CLASSA" /&gt;
      &lt;constructor-arg index="1" type="java.lang.String"
          ref="testPackage" /&gt;
      &lt;property name="encoding" ref="xmlEncoding" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Unhelpfully the XML files I have to process are delivered <em>one at a time</em> in file with a fixed name and a rolling counter (and I cannot change that). So I'm unable to tell what object is in the file from the filename. I've have a tiny utility function which check which the object type is in the file I'm trying to process. </p>
  
  <p>I then have the following function:</p>
  
  <pre><code>public static Object loadXML(String xmlFile, String fileName) throws Exception
  {
      InputStream iStream = XmlUtils.openFile(xmlFile);
      String xmlObjectType = XmlUtils.getObjectTypeFromXML(xmlFile);
              // get the right JAXB processor
      JaxbWrapper jaxB = myContext.getBean("jaxbParser_" + xmlObjectType , JaxbWrapper.class);
      Object  obj = jaxB.objectFromXML(iStream, xmlObjectType , fileName);
      return obj;
  }
  </code></pre>
  
  <p>So I'm taking the object name and getting the right bean from the context. This means I could use things like Spring's object pool to hold lots of JaxbWrappers for different objects. However the way I've implemented this feels wrong to me. I don't like the myContext.getBean("jaxbParser_" + xmlObjectType method of getting the JaxbWrapper bean. </p>
  
  <p><em>So my question is: Is there a better way of structuring this code? A few simple pointers would be very much appreciated.</em></p>
  
  <p><strong>Additional Complexity</strong></p>
  
  <p>This is where things, at the moment, become really unmanageable. Once the marshaling stage has completed I've got a post processing stage where there are several different post processor <em>for each</em> class type. I've got Spring beans for each of these and I'm getting them from the application context using:</p>
  
  <pre><code>myContext.getBean(xmlObjectType + "_" + processorType + "_" + xmlObjectType);
  </code></pre>
  
  <p>where:</p>
  
  <ul>
  <li>xmlObjectType is a string CLASSA or CLASSB which is set by reading the object type from the file (as above).</li>
  <li>processorType is s string which is set from the comand line. </li>
  </ul>
  
  <p>To set the proceesorType I'm doing something like the following when the application starts.</p>
  
  <pre><code>    if (line.hasOption("file"))
      {
          processorType = "FileProcessor";
      }
      if (line.hasOption("print"))
      {
          processorType = "PrintProcessor";
      }
  </code></pre>
  
  <p>Again, I don't think this is the right way to do things, but It's the best I've got at the moment :-(.</p>
  
  <p><strong>I guess the more general question is how do JAXB and Spring work together? In the real world I have lots very large and complex CLASSA and CLASSBs. I've got java classes for these generated by xjc. As I'm using xjc I have to use jaxb (I guess). The question is how to do that in the Spring world.</strong></p>
  
  <p><strong>Or can I get rid of JAXB and use a spring component. I'd need to use something else to generate all the classes other than xjc. But I cannot find anything that would perform that task</strong> </p>