Jyotsna,
JXPath is designed such that nonexistent parts of the
graph cannot be set directly. If you know that arr[0]
might not exist yet, you must check for that condition
explicitly and install a factory to create that part
of the graph. My test code looks like this:
OrganisationType t = new OrganisationType();
JXPathContext ctx = JXPathContext.newContext(t);
ctx.setLenient(true);
Pointer ptr = ctx.getPointer("arr[1]");
if (ptr == null || ptr instanceof NodePointer &&
!((NodePointer) ptr).isActual()) {
ctx.setFactory(new AbstractFactory() {
public boolean createObject(JXPathContext context,
Pointer pointer, Object parent, String name, int
index) {
if (parent instanceof OrganisationType &&
"arr".equals(name)) {
ArrayList l = (ArrayList) ((OrganisationType)
parent).arr;
l.addAll(Arrays.asList(new Object[index -
l.size() + 1]));
return true;
}
return super.createObject(context, pointer,
parent, name, index);
}
});
ptr = ctx.createPath("arr[1]");
}
ptr.setValue("bar");
HTH,
Matt
--- Jyotsna <[EMAIL PROTECTED]> wrote:
> Hi
>
> I have a java bean with the following attributes and
> their getters and
> setters.
>
>
************************************************************
>
> public class OrganisationType implements
> Serializable {
>
> Long id;
> String code;
> String description;
> int version;
> Collection arr =new ArrayList();
> HashMap hsh =new HashMap();
>
> Getter and setters follow.......................
>
>
************************************************************
>
> I am using the following code to populate the array
> list
>
> OrganisationType orgna = new OrganisationType();
> orgna.setCode("12121");
> JXPathContext contexts =
> JXPathContext.newContext(orgna);
> contexts.setValue("arr[1]","12");
>
>
> ****************And the Exception I get is
> ***************************
> org.apache.commons.jxpath.JXPathException: Exception
> trying to set value
> with xpath arr[1]; Index: 0, Size: 0
> at
>
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.setValue(JXPathC
> ontextReferenceImpl.java:421)
> at
>
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.setValue(JXPathC
> ontextReferenceImpl.java:412)
> at TestJxPath.main(TestJxPath.java:36)
>
____________________________________________________________________________________
We won't tell. Get more on shows you hate to love
(and love to hate): Yahoo! TV's Guilty Pleasures list.
http://tv.yahoo.com/collections/265
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]