Safely delete and modify elements within Apache XmlBeans object?
We are using Apache XmlBeans, with code generated from a schema. My
assigned task is to read in an XML instance that conforms to the schema,
modify it in various application-specific ways, then revalidate. For
example, change the value of an element, delete an element from a list,
etc. I am using Java method calls like removeMyElement(i) to remove the
element at position (i). I am not using Cursors or any low-level DOM
access.
I am clearly doing the changes wrong because my code causes the XmlBeans
core to throw XmlValueDisconnectedException on certain operations. The
explanation for this exception seems to be that the XmlObject has become
disconnected from its underlying store.
I checked the XmlBeans FAQ and the sample code for guidelines. I found
many samples at http://xmlbeans.apache.org/samples/ that show how to
create an object from scratch, how to read in XML, how to validate XML,
etc. Unfortunately I didn't find a sample that does a messy change to the
content. The FAQ at http://wiki.apache.org/xmlbeans/XmlBeansFaq doesn't
have a good question on this either.
I have discovered (by some googling) posts stating that after calling a
method to remove an element in a list, I must immediately call the
getElementsArray() method again. That's reasonable, a good start.
We thought it would be convenient to gather a list of objects (references
to XmlBeans objects) that need to be modified, then iterate over the list
changing them. But use of a cached object reference is where I run into
the ditch.
Does every change to an XmlObject backed by the XmlBeans XmlStore cause
all existing references to become invalid? I don't yet have the right
mental model for what's going on when I call the java methods.
Please point me or send advice, thanks in advance.
No comments:
Post a Comment