How to use TXMLObjectIterator()??

Dear all,

I want to do is to remove the whole instrument element for each jazzMusician:
Original Document:




1
Piano






2
Drum



Modified Document:









HERE is my code:
if(response.hasFirstXMLObject()){
TXMLObject xmlObject = TXMLObject.newInstance( TDOMObjectModel.getInstance());
TQuery findroot = TQuery.newInstance(“/jazzMusician”);
TResponse rootresponse = xmlObjectAccessor.query(findroot);
int removelistSize = 0;
while(rootresponse.getXMLObjectIterator().hasNext()){
xmlObject = rootresponse.getFirstXMLObject();
Element oldroot = (Element) xmlObject.getElement();
NodeList removenode = oldroot.getElementsByTagName(“instrument”);
removelistSize = removenode.getLength();
for(int r=0; r<removelistSize; r++){
oldroot.removeChild(removenode.item(r));
}
rootresponse.getXMLObjectIterator().next();
try{
response = xmlObjectAccessor.update(xmlObject);
System.out.println(“Delete SUCCEED”);
}catch (TUpdateException ex){
System.out.println("Delete FAIL: " + ex.getMessage());
}
}
}else
System.out.println(“NO!!”);

I don’t know what’s wrong in my code, but it return nullpointer exception. If you have found the bugs of my code, please let me know, or if you have any better solutions for this remove action,
please tell me too. Thanks everyone!!


Snake

Is it possible that you could use XQuery update?

Something along the lines of “update delete input()/jazzMusician/instrument”.

Hi Mark,


If I want to delete “instrument” by its specific attribute value or sub-element value, how to set the XQuery?? Thanks you~~~


Snake

Then you would need to read the documentation entitled “Performing Update Operations” under the XQuery documentation.

Thx Mark,

But how to solve if using DOM method?

Snake

I think that your DOM processing looks OK.

However, I would expect you to have process EACH resultant node (document) one at a time. I.e. have you tried iterating over each result element, performing the change and then updating just that element?

Could it be possible that the following line produce null pointer exception?

xmlObject = rootresponse.getFirstXMLObject();
//It is inside the while loop

And I tried put the following line outside the while loop, then no exception occured:

try{
response = xmlObjectAccessor.update(xmlObject);
System.out.println(“Delete SUCCEED”);
}catch (TUpdateException ex){
System.out.println("Delete FAIL: " + ex.getMessage());
}

However, the result is not I expected.
SO, what is the problem?

I was given this answer by Bill Leeney and it looks good to me:

Problem is, a NodeList is a LIVE collection. So let’s say there are three items in it to start with; items 0,1 and 2.

The code deletes the (0) item leaving only two items which are now indexed by 0 and 1. The code gets away with deleting item (1) next, leaving only item(0). Now the code tries to delete item (2) - it doesn’t exist.

If you try removing the children in reverse order, 2,1,0. This should always work.

Thx!!!
It is OK now~~~~~