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

Java code for dropping elements and attributes

  • 1.  Java code for dropping elements and attributes

    Posted Mon November 23, 2009 06:59 PM

    I have a xml document and in that document, I could have a mixture of elements and attributes.

    Is there Java code available out there to drop empty elements and attributes? I don’t want to have to go through each elements and/or attributes to find out if their value is null or empty then dropping it.

    There must be a better way.


    #webMethods
    #Flow-and-Java-services
    #Integration-Server-and-ESB


  • 2.  RE: Java code for dropping elements and attributes

    Posted Fri December 04, 2009 02:32 AM

    Really roughly, off the top of my head and not compiled (let alone tested):

     
    public void dropEmptyValues (IData aDoc)
    {
    IDataCursor vCsr = aDoc.getCursor();
    boolean vOk = vCsr.first();
    while (vOk)
    {
    Object vValue = vCsr.getValue();
    if (vValue == null)
    vOk = vCsr.delete(); // moves to next value if there is one
    else
    if (vValue instanceof String && ((String)vValue).equals(""))
    vOk = vCsr.delete();
    else
    {
    // recurse through child docs
    // beware of reference loops (not checked here)
    if (vValue instanceof IData)
    dropEmptyValues((IData)vValue);
    else
    if (vValue instanceof IData[])
    {
    IData vDocs[] = (IData[])vValue;
    for (int i = 0; i < vDocs.length; i++)
    dropEmptyValues(vDocs[i]);
    }
    vOk = vCsr.next();
    }
    }
    vCsr.destroy();
    }

    HTH!


    #webMethods
    #Flow-and-Java-services
    #Integration-Server-and-ESB