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-unsubscr...@xmlbeans.apache.org
For additional commands, e-mail: user-h...@xmlbeans.apache.org

Reply via email to