XML In Java In XML Is Ugly
By Adrian Sutton
I’ve been struggling with getting Cocoon and Exist to work together properly and allow me to insert an XML snippet into a document in the database. Apart from the fact that the apparent assumption seems to be that no one would ever have an XML snippet being fed into an XML-based system and thus all the < and > characters should be converted to entities, plus the fact that Exists seems to only want to retrieve stuff from the database and not change stuff in the database, I’ve wound up with the following:
<?xml version="1.0" ?>
<xsp:page xmlns:xsp="http://apache.org/xsp"
xmlns:xsp-request="http://apache.org/xsp/request/2.0"
xmlns:xdb="http://exist-db.org/xmldb/1.0">
<result>
<xdb:driver class="org.exist.xmldb.DatabaseImpl" />
<xdb:collection uri="xmldb:exist:///db/features" user="admin" password="password">
<xsp:logic>
String comment = <xsp-request:get-parameter name="comment" />;
String author = <xsp-request:get-parameter name="author" />;
String id = <xsp-request:get-parameter name="id" />;
String xupdateQuery = "<xupdate:modifications version='1.0' xmlns: xupdate='http://www.xmldb.org/xupdate'><xupdate:append select='/my:featureList/ my:feature[@id = " + id + "]/my:comments'><xupdate:element name='my:comment' namespace='http://www.ephox.com/featureManager'><xupdate:attribute name='author'>" + author + "</xupdate:attribute>" + comment + "</xupdate: element></xupdate:append></xupdate:modifications>";
try {
XUpdateQueryService service = (XUpdateQueryService)collection. getService("XUpdateQueryService", "1.0");
service.update(xupdateQuery);
} catch (Exception e) {
e.printStackTrace();
}
</xsp:logic>
<xdb:execute>
<xdb:xpath>
"/my:featureList/my:feature[@id = <xsp-request:get-parameter name="id" />]
</xdb:xpath>
</xdb:execute>
</xdb:collection>
<greeting>
</greeting>
</result>
</xsp:page>
Seriously ugly…. Surely there has to be a better way! I know I can move all of the Java code into a logic sheet but my real concern is with the doubly escaped XUpdate template which is just completely unreadable. If so wish I could just tell XQuery that the request parameter should be treated as an XML snippet instead of as a string.
Sigh. Perhaps this would be easier if I actually knew something about Cocoon, Exist, XQuery, XUpdate and XML databases before I started this project….