cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marc Portier <>
Subject Re: JXForms, flow and XML data store
Date Thu, 10 Jul 2003 12:36:12 GMT

Upayavira wrote:
> I'd like to start building a site that uses jxforms and flow to allow the user to edit

> content (addresses) stored in a single XML file. There'll be one user responsible 
> for up to 200 addresses, with the addresses visible on a public site.
> The addresses are all stored in a single XML file.
> I can see how, using jxforms/flow, I can really easily build the front end. But how 
> do I get my data out of my XML file, and then get the changed data back into it? 
> I could do it in Java, but that'd require XML parsers, serializers, etc, etc, which'd

> be a pain to write for such a small and simple site.
> Can I use jxpath in some way to access the XML file resource?

yes, jxpath allows you to do that (designed for it in fact)

if you have the xpath expressions into your xml-document you can 
easily find a Pointer (JXpath concept) to it, and use that to 
set/get values...

assuming your XML somewhat looks like
  <address id="0982389">
     <name>Some Name</name>
     <street>OneStreet 12</street>
     <city zip="2987">TheTown</city>

  <address id="0982390">

so just assuming you have your address.xml parsed into a DOM 
tree, you can

//warning uncompiled code/cut-pasted from other example on HD

String addressID;
Document addressDOM;

addressDOM = ...;// parsing the src/restoring it form memory...
addressID  = ...;// probably somewhere in the request
                  //or in the flow

JXPathContext docCtxt = JXPathContext.newContext(addressDOM);

Pointer listPtr = docCtxt.getPointer("addresslist");
JXPAthComtext listCtxt = docCtxt.getRelativeContext(listPtr);

String addrXpath = "address[@id='" + addressID + "']";
Pointer addrPtr = listCtxt.getPointer(addrXpath);
JXPathContext addrCtxt = listCtxt.getRelativeContext(addrPtr);

addrCtxt.setValue("name"     , newNameValue);
addrCtxt.setValue("street"   , newStreetValue);
addrCtxt.setValue("city"     , newCityValue);
addrCtxt.setValue("city/@zip", newZipValue);

creation of new nodes is normally done in jxpath with factories, 
but I guess in this case using the underlaying DOM is easier.

Node list = (Node)listCtxt.getContextBean();

//make sure this new one has a recognisable id, so you can
// forge a direct addrPtr to it for binding in the values.

(NOTE: if your addresses don't have a real lookup-id in the xml, 
but you only have their sequence/index number in the XML file, 
then the factory method might be more useful after all)

(NOTE2: you might want to consider holding a 'fresh' DOM 
document-fragment (kindof template) in memory to clone/import 
into your document)

finally: haven't touched deletion of nodes yet, but again: the 
DOM is still under there, so you could use that for sure

I know this is javacode, but maybe someone else can explain how 
to easily address this from inside the js.


Side note:
I recently mentioned some ideas on some generic binding of a 
woody-form-model to a javabean or xml backend....  I got some 
code working on my HD showing for it (and using jxpath, so doing 
this is where I got my initial jxpath understanding)

I'm wrapping up with an example and some docos as we speak
hoping to be able to post into bugzilla before the weekend

you might find some applicable usage of jxpath in there...
if you can't wait to see that arrive: just use the list to come 
back on above suggestions

> Regards, Upayavira

Marc Portier                  
Outerthought - Open Source, Java & XML Competence Support Center
Read my weblog at                        

View raw message