How can i use XQ_getXPath in java classes? - Forum - Technology Partner - Progress Community

How can i use XQ_getXPath in java classes?

 Forum

How can i use XQ_getXPath in java classes?

  • I want to traverse an XML message coming to a step and get certain values. I have attached the code I tried below.

    Input XML :

    <?xml version="1.0" encoding="UTF-8"?>

    <

    Details>

         <

    Name>Nithin</Name>

    </

    Details>

    I called : XQ_getXPath("/Details/Name/text(),0,"")

    public Object XQ_getXPath(String xpathExpression, int msgPartIndex, String namespaceString) throws XQAccessorException

    {

    XQAccessorFactory accessorFactory =

    ctxGlbl.getAccessorFactory();

    XQAccessor xpathAccessor = accessorFactory.getAccessor(

    "xpath");

    if (xpathAccessor == null)

    {

    return null;

    }

    HashMap namespaceMap =

    new HashMap();

    Object xpathResult;

    if (namespaceString != null)

    {

    StringTokenizer strTokenizer =

    new StringTokenizer(namespaceString, "=");

    String namespace_prefix=

    "", namespace_uri="";

    while(strTokenizer.hasMoreTokens())

    {

    if (strTokenizer.hasMoreTokens())

    {

    namespace_prefix = strTokenizer.nextToken();

    }

    if (strTokenizer.hasMoreTokens())

    {

    namespace_uri = strTokenizer.nextToken();

    }

    //Set NameSpace prefix and URI in the namespace hashMap.

    if ((namespace_prefix != null) && (namespace_uri != null))

    namespaceMap.put(namespace_prefix, namespace_uri);

    }

    }

    /* Create a HashMap object to hold the Accessor Target:

    XQMessage, msgPartIndex(defaults to zero) and namespaceMap

    ( if not null)

    */

    HashMap map =

    new HashMap();

    map.put(

    "XQMessage", xqMsg);

    //if (msgPartIndex != 0)

    map.put(

    "XQPartIndex", msgPartIndex);

    if (namespaceMap != null)

    map.put(

    "xmlns", namespaceMap);

    try{

    xpathResult = xpathAccessor.invoke(xpathExpression, map);

    System.

    out.println("xpathResult : "+xpathResult);

    return xpathResult;

    }

    catch (XQAccessorException ex)

    {

    return null;

    }

    }

    It gives the output ----> xpathResult : null

  • Nithin,

    I am not quite sure on the use case. XQ_getXPath is a JScript function to be used in a CBR.

    The subject though says you want to use it in Java / custom service type.

    Can you elaborate a little?

    Thomas

  • Hi Thomas,

        I wanna traverse an XML message and find a value in a tag (in the ex. 'Name') and create anothee message part with that.

       So I thought of doing it in a java service type. Is there any other way around?

  • Nithin,

    I'd suggest you to take a look on Message Extensions (available on the Sonic Workbench Help Topics).

    Basically, I believe it would be easier to achieve what you're looking for if you implement an XML Transformation Service which is able to generate new messages and/or parts if need. Here is a sample scenario:

    Here is the XML sample file simulating the entry message to the XML Transformation Service:



        John Smith
       

    1222 Test

        Atlanta
        GA
        98765

    Then, you could create an XSLT file with the following content:



    xmlns:XQMessageElem="http://www.sonicsw.com/sonicxq/com.sonicsw.xq.service.xform.TransformationElementFactory"
    extension-element-prefixes="XQMessageElem">



     
       
         
       
       
           
       
     



    That XML Transformation Service will create a message with two parts where the first part contains the whole message and the second part contains just the value of the City (which is in this sample what I'm looking for).

    I'm assuming your "test" clause would be what you're looking for and then you can create the message the way you want (i.e. single part, multiple parts, etc).

    Hope this helps,

    -Marcelo

  • Hi Nithin,

    With ESB 8.0 you can make use of  the message mapping feature to achieve the same functionality. You can do this visually in the workbench without writing a custom service.

    Which version of ESB are you using?

    Thanks

    -Mahesh

  • Thanks Marcelo. But I also want to create Topics dynamically with that name(obtained from the incoming XML) and publish the message on that topic.

    At times I dont even need to add another part but simply publish the incoming message on this topic created with the value that comes in the input XML.

  • Hi MAhesh am using Version 7.6. I want to identify that value and then create topics with that name dynamically and publish on it. Do we have that feature also in 8.0?

  • You can also set the destination of the message at runtime using Message Extensions. Something like:


    would address the message to such destination.

  • nithinonpsdn wrote:

    Hi MAhesh am using Version 7.6. I want to identify that value and then create topics with that name dynamically and publish on it. Do we have that feature also in 8.0?

    Publishing to topics based on a string (where ever this string is derived from) would take a custom service or use of JavaScript (in both 7.6 and 8.0).   Typically this would be a Java service that dynamically creates endpoints with the EndpointManager.