Alex-

Here's the mapping file:

<mapping>
  <class name="NodeClass">
    <map-to xml="node-class" />
    <field name="xml" type="org.w3c.dom.Node" direct="true"
        handler="DOMHandler">
        <bind-xml name="description" node="element" />
    </field>
  </class>
</mapping>

And here is the DOMHandler class, with exception handling removed (please excuse the poor variable names):

public class DOMHandler extends GeneralizedFieldHandler {
  public Object convertUponGet( Object arg0 ) {
    // take in object, return string
    Node xml = (Node)arg0;
    Transformer t = TransformerFactory.newInstance().newTransformer();
    Source input = new DOMSource( xml );
    StringWriter sw = new StringWriter();
    Result output = new StreamResult( sw );
    t.transform( input, output );
    return( sw.getBuffer().toString() );
  }

  public Object convertUponSet( Object arg0 ) {
    // take in string, return object
    DocumentBuilder db =
      DocumentBuilderFactory.newInstance().newDocumentBuilder();
    StringReader input = new StringReader( (String)arg0 );
    Node xml = db.parse( new InputSource( input ) );
    return( xml );
  }

  public Class getFieldType() {
    return( Node.class );
  }
}

That should give you everything you need to get the functionality I achieved. Let me know if you have any problems.

Stephen

Alex Milowski wrote:


-----Original Message-----
From: Stephen Bash [mailto:[EMAIL PROTECTED]
Sent: Thu 10/6/2005 6:13 AM
To: [email protected]
Subject: Re: [castor-user] JDOM/DOM Node as Field?
Alex-

I did some quick tests this morning, and using a CustomFieldHandler (http://castor.codehaus.org/xml-fieldhandlers.html) I was able to "marshal" and "unmarshal" a DOM node, but as has been discussed on the list before, Castor performs entity replacement (lots of &gt; &lt;) so it doesn't look exactly how one would expect. I haven't been following the arbitrary XML discussion very closely, but that may be the larger challenge in this case.


Actually, this might help.  In reality, the server isn't going
to do much with this XML.  Just store it in the database as a blob
and then needs to reconstitute it on the way out as proper XML.

Can you send me this example?  I'd like to try some experiments.  :)

Thanks!

-------------------------------------------------
If you wish to unsubscribe from this list, please send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------





------------------------------------------------------------------------

-------------------------------------------------
If you wish to unsubscribe from this list, please send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------

-------------------------------------------------
If you wish to unsubscribe from this list, please send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------

Reply via email to