That's really helpful. That got me most of the way there. I have to take into account that "child" may not be the only kind of child a parent can have. Given schema constraints, I have to make sure children appear in the right order. I've found a solution that still needs testing for all cases.
The overall goal is to take some "source" XmlObject and apply it to a "target" XmlObject. The "target" XmlObject is the live XmlObject. We have custom events, event listeners, etc., working with the "target" XmlObject. That's why I can't just call target.set(source); we lose all the listeners. So, you invoke an "edit" on the live "target" and get a dialog. The dialog makes a copy of the "target" and returns the modified copy based on user input. So your "target" is still the live XML and the "source" is the updated version of "target." If everything goes correctly, your target is now equivalent to your source. I have a series of methods that compare source and target. One handles attributes. One handles child text. One handles removing child elements from the target that no longer exist in the source. One handles updating child elements that exist in both target and source. Finally, I have the method that adds child elements from source that don't exist in target. This is the one that's giving me trouble. I iterate through all of the source's child elements in order. Order's important; otherwise we could break schema constraints. Assuming source is correct, I try to place the new child element in the right place under target. // This is the qualified name of the previous child element from source we processed. QName previous = ... XmlCursor targetCursor = target.newCursor(); // Attempt to go to the last child we processed. This should pass unless the target has no child elements and this is the first child element processed. if (targetCursor.toChild(previous)) { // If we don't have a sibling, w try to go to the end of the parent (containing) element. if (!targetCursor.toNextSibling()) { targetCursor.toParent(); targetCursor.toEndToken(); targetCursor.toPrevToken(); } // If we made it to the last child we processed, we want to insert the new child after it. else { targetCursor.toEndToken(); } } sourceCursor.copyXml(targetCursor); previous = sourceName; targetCursor.dispose(); // Iterate to the next source child. Does this make any sense whatsoever? Am I making this too difficult? At the end of the day, I want to operate on "target" so it is equivalent to "source" without losing any of the custom event-related information in "target." On Wed, Oct 23, 2013 at 3:35 PM, <jerry...@oracle.com> wrote: > You need to go to the END of the parent cursor or to the nextToken of the > END of the lastChild, then do the insert. > > XmlObject test1 = XmlObject.Factory.parse("<** > parent><child>first</child></**parent>"); > XmlCursor testcurs = test1.newCursor(); > if (testcurs.isStartdoc()){ > testcurs.toNextToken(); > } > boolean res = testcurs.toLastChild(); > XmlCursor.TokenType tt = testcurs.toEndToken(); //this goes to END > tag of the last child element > tt = testcurs.toNextToken(); //we are now at the END tag of parent > testcurs.**insertElementWithText("child",**"second"); > > or this > XmlObject test1 = XmlObject.Factory.parse("<** > parent><child>first</child></**parent>"); > XmlCursor testcurs = test1.newCursor(); > if (testcurs.isStartdoc()){ > testcurs.toNextToken(); //now at parent element > } > XmlCursor.TokenType tt = testcurs.toEndToken(); //END tag of > parent element > testcurs.**insertElementWithText("child",**"second"); > > > > On 10/23/2013 11:51 AM, Michael Bishop wrote: > >> Hello all, >> >> I'm a bit stuck in trying to achieve this with XmlObjects and XmlCursor. >> Imagine the following scenario: >> >> <parent> >> <child>Some text</child> >> </parent> >> >> I want to insert a new element AFTER the child element. >> >> <parent> >> <child>Some text</child> >> <!-- New element here! --> >> </parent> >> >> I don't necessarily know anything about the child element. It might have >> child text like the example above. It might not. It might have child >> elements, it might not. It might have siblings, it might not. Here's where >> I'm stuck: >> >> // XmlObject representing parent. >> XmlObject parent = ...; >> >> XmlCursor parentCursor = parent.newCursor(); >> >> // Now I'm at the START token for the child element. >> parentCursor.toChild("child"); >> >> From here, how do I reliably get the cursor past </child> or <child/>? >> Basically I'm at the start of <child> and want to be right past the end of >> that element. >> >> Thanks, >> >> Michael >> > > > ------------------------------**------------------------------**--------- > To unsubscribe, e-mail: > user-unsubscribe@xmlbeans.**apache.org<user-unsubscr...@xmlbeans.apache.org> > For additional commands, e-mail: user-h...@xmlbeans.apache.org > >