IBM webMethods Hybrid Integration

IBM webMethods Hybrid Integration

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
Expand all | Collapse all

How to use TXMLObjectIterator()??

  • 1.  How to use TXMLObjectIterator()??

    Posted Wed March 17, 2004 08:58 AM

    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


    #API-Management
    #webMethods
    #webMethods-Tamino-XML-Server-APIs


  • 2.  RE: How to use TXMLObjectIterator()??

    Posted Wed March 17, 2004 09:20 AM

    Is it possible that you could use XQuery update?

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


    #webMethods
    #API-Management
    #webMethods-Tamino-XML-Server-APIs


  • 3.  RE: How to use TXMLObjectIterator()??

    Posted Wed March 17, 2004 09:56 AM

    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


    #API-Management
    #webMethods-Tamino-XML-Server-APIs
    #webMethods


  • 4.  RE: How to use TXMLObjectIterator()??

    Posted Wed March 17, 2004 10:11 AM

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


    #API-Management
    #webMethods-Tamino-XML-Server-APIs
    #webMethods


  • 5.  RE: How to use TXMLObjectIterator()??

    Posted Wed March 17, 2004 11:59 AM

    Thx Mark,

    But how to solve if using DOM method?

    Snake


    #webMethods-Tamino-XML-Server-APIs
    #API-Management
    #webMethods


  • 6.  RE: How to use TXMLObjectIterator()??

    Posted Wed March 17, 2004 04:22 PM

    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?


    #API-Management
    #webMethods
    #webMethods-Tamino-XML-Server-APIs


  • 7.  RE: How to use TXMLObjectIterator()??

    Posted Fri March 19, 2004 09:23 AM

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

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


    #API-Management
    #webMethods
    #webMethods-Tamino-XML-Server-APIs


  • 8.  RE: How to use TXMLObjectIterator()??

    Posted Fri March 19, 2004 09:26 AM

    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?


    #API-Management
    #webMethods
    #webMethods-Tamino-XML-Server-APIs


  • 9.  RE: How to use TXMLObjectIterator()??

    Posted Fri March 19, 2004 11:42 AM

    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.


    #webMethods-Tamino-XML-Server-APIs
    #webMethods
    #API-Management


  • 10.  RE: How to use TXMLObjectIterator()??

    Posted Tue March 23, 2004 08:08 AM