"complex" XML from web service

Discussion in 'CRM / Helpdesk / App Integration' started by asmith3006, Apr 9, 2014.

Thread Status:
Not open for further replies.
  1. asmith3006

    Joined:
    Mar 5, 2014
    Messages:
    94
    Likes Received:
    5
    Hi,

    Just getting my head around VAD and would appreciate some help.

    I'm trying to return "complex" XML (as the manual calls it) where the XML returned has multiple elements in it. Is it possible to access the data of the child-elements once it's in the return variable? The manual doesn't really say one way or the other
    So this suggests that the full returned data is stored, but how do I access it? For example if I return
    Code:
    <response><type>1</type><enabled>false</enabled></response>
    and then based on the value of <enabled> I want to give them either menu A or menu B how would I use this in the following "conditional" block?

    Thanks.

    Andrew.
     
  2. VAD_Support

    VAD_Support Active Member

    Joined:
    Aug 6, 2009
    Messages:
    690
    Likes Received:
    0
    Hi Andrew,

    If your web service returns:
    Code:
    <response><type>1</type><enabled>false</enabled></response>
    Then, the "WebServicesInteraction.Result" variable will contain the text:
    Code:
    <type>1</type><enabled>false</enabled>
    Note that the root element has been removed.

    So, after that, you can do whatever you need with that string. For example, you can create a javascript function that receives that string as a parameter, and returns if enabled is true or false. Then you can call that function using an External Code Execution component. And finally use the result of the External Code Execution component into a Conditional component to take one branch or the other.

    Hope the information helps.

    Kind regards
     
  3. renaro.santos

    Joined:
    Aug 29, 2014
    Messages:
    4
    Likes Received:
    0
    I am having the same problem as Andrew described.
    Also, i am getting the xml unformatted, where '<' is replaced by '&lt ;' and '>' is replaced by '&gt ;' How can I fix it?
    The second question is, how can I pass this returned xml as a parameter to a javascript function?

    Best Regards, Renaro.
     
  4. VAD_Support

    VAD_Support Active Member

    Joined:
    Aug 6, 2009
    Messages:
    690
    Likes Received:
    0
    Hi Renaro,

    In order to unescape the XML you can write a javascript function, like this:
    http://dracoblue.net/dev/encodedecode-special-xml-characters-in-javascript/

    To do that, you have to use an External Code Execution component. You configure it to call a JavaScript function, select your JavaScript file containing all the functions you need, and then add the parameters to pass to the funciton.

    Kind regards.
     
  5. renaro.santos

    Joined:
    Aug 29, 2014
    Messages:
    4
    Likes Received:
    0
    Hi. I appreciate the fast answer.
    I am using Web Interaction instead of Web Services Interaction.
    Questions:
    1 - Is there any difference between them?
    2 - I can't manage to get the result using 'Web Services Interaction', I don't know how to get it.
    I tried to print it into a File but it doesn't work, it works fine if I assigne it to a variable, but then I can't see the return. I know the result is a XML, how can I see it?


    Best Regards, Renaro.
     
  6. VAD_Support

    VAD_Support Active Member

    Joined:
    Aug 6, 2009
    Messages:
    690
    Likes Received:
    0
    Hi Renaro,

    The Web Interaction component will get the entire XML returned by the web server.

    The returned XML will be into the "Content" property of your web interaction component. You can check it with the VAD debugger for instance.

    Also, take a look at the following article about invoking a web service with the VAD:
    http://www.3cx.com/blog/docs/accessing-web-service-vad/

    Kind regards.
     
  7. slippyr4

    Joined:
    Aug 18, 2014
    Messages:
    14
    Likes Received:
    0
    Feature request:-

    PLEASE PLEASE make the Web Service component parse a JSON result from a web service and make the resultant object available for use in a javascript expression.

    That, in one (probably fairly easy) change would be a revolution in flexibility.
     
  8. VAD_Support

    VAD_Support Active Member

    Joined:
    Aug 6, 2009
    Messages:
    690
    Likes Received:
    0
    Thanks for your feedback, we'll consider it for future features.

    Meanwhile you can just parse the text from javascript. There are many ways to achieve it, please take a look at this:
    http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript

    Kind regards.
     
  9. renaro.santos

    Joined:
    Aug 29, 2014
    Messages:
    4
    Likes Received:
    0
    Thanks for helping.
    I've already read this link http://www.3cx.com/blog/docs/accessing-web-service-vad/
    Didn't help.
    All I need to do is get values that are returned in a XML from a Web Server.
    What I've tried:

    Web Interaction Component: I am able to get the XML file, it comes unformatted, so I followed your instructions to unescape the xml using an External Code Execution with this link:
    http://dracoblue.net/dev/encodedecode-special-xml-characters-in-javascript/
    I am able to parse '&gt ;' to '>' , '&quot;' to ' " ', but for some really unexplained reason 'lt ;' IS NOT converted to '<'.
    Also, even if I manage to fix it, how can I read the XML using external code? Because DOMParser doesn't seems to work with 3CX External Code http://stackoverflow.com/questions/649614/xml-parsing-of-a-variable-string-in-javascript

    Web Service Interaction: I've followed the manual and I've tried many things, I just can't print the 'result' in a text file to see if it is working.
    Here some prints.

    "Images not found"

    And once again, even if I get the XML result from Web Services Interaction, how can I read it?

    I am sorry if these are stupid questions, but I truly searched the internet and couldn't find anything explaining how to get it.
     
  10. VAD_Support

    VAD_Support Active Member

    Joined:
    Aug 6, 2009
    Messages:
    690
    Likes Received:
    0
    Hi Renaro,

    Have you tried debugging the app and checking the variable values in the Debug window?

    You're saying that 'lt ;' IS NOT converted to '<'. Does it have the starting "&" character?

    How does the response look like? Can you share a sample response with us? That way I could suggest you how to parse it...

    Kind regards.
     
  11. renaro.santos

    Joined:
    Aug 29, 2014
    Messages:
    4
    Likes Received:
    0
    Here is the XML returned when I try use Web Interaction:
    https://dl.dropboxusercontent.com/u/68899142/return.txt

    Using Web Service Interaction and looking into the Log file, I discovered that I even get the Content, I just can't read or print it.
    See the log here:
    https://dl.dropboxusercontent.com/u/68899142/log.txt

    (URLhttp://localhost:5000/ivr/(S(xbcwwj0typie04bsbesciatz))/PID_158/Main_MF_fileManagementComponent1_DoIt.aspx?appendFinalCrLf=1&content=%3CCodigoRetorno%20xmlns%3D%22http%3A%2F%2Fconductor.com.br%2F%22%3E1%3C%2FCodigoRetorno%3E%0D%0A%3CDescricaoRetorno%20xmlns%3D%22http%3A%2F%2Fconductor.com.br%2F%22%3ECliente%20n%C3%A3o%20localizado%3C%2FDescricaoRetorno%3E%0D%0A%3CId_Conta%20xmlns%3D%22http%3A%2F%2Fconductor.com.br%2F%22%3E0%3C%2FId_Conta%3)

    I tried use the Debug mode, as described in the manual, but I got another error :(
    "Image not found"

    I think I am almost there, I just need to know HOW TO READ the 'Content' or 'Result' (tag values) returned from the Web Server.

    This is what the Web Server returns:
    https://dl.dropboxusercontent.com/u/68899142/result_ok.txt

    What I need, exactly, is to get the value inside Id_conta:
    example: <Id_conta>6123</Id_conta>
     
  12. VAD_Support

    VAD_Support Active Member

    Joined:
    Aug 6, 2009
    Messages:
    690
    Likes Received:
    0
    Hi Renaro,

    Ok, so in first place, in order to fix the issue with the debugger, some questions:
    - Are you deploying locally (VAD installed in the 3CX server) or remotely (VAD installed somewhere else)?
    - If you're deploying locally, try changing the PbxServer property for the project to 127.0.0.1
    - If you're deploying remotelly, try invoking the URL http://PBXSERVER:5000/ivr/VAD_GetDebugInfoFileNames.aspx and check the result.


    Next, regarding the issue with the File Management component, the log shows an internal server error (HTTP status code 500) when it tries to invoke the page which does the work. What happens if you manually invoke that URL? The URL should be:

    http://localhost:5000/ivr/PID_158/Main_MF_fileManagementComponent1_DoIt.aspx?appendFinalCrLf=1&content=%3CCodigoRetorno%20xmlns%3D%22http%3A%2F%2Fconductor.com.br%2F%22%3E1%3C%2FCodigoRetorno%3E%0D%0A%3CDescricaoRetorno%20xmlns%3D%22http%3A%2F%2Fconductor.com.br%2F%22%3ECliente%20n%C3%A3o%20localizado%3C%2FDescricaoRetorno%3E%0D%0A%3CId_Conta%20xmlns%3D%22http%3A%2F%2Fconductor.com.br%2F%22%3E0%3C%2FId_Conta%3E


    Maybe the issue could be related to some code injection protections that the web server enforces. Because in this case you're trying to post XML, that could be risky and not allowed by the web server. In order to avoid this you may need to disable request validation as explained here:
    http://stackoverflow.com/questions/2699308/send-server-urlencoded-xml-in-querystring-causing-error

    Disabling the request validation should be done in the web.config file located at "C:\ProgramData\3CX\Data\Http\Interface\ivr". Then you need to restart the web server.


    Finally, in order to get the value inside the element "Id_conta", you may use a javascript function that parses the result, for example, something like this:
    function getIdConta(webResponse)
    {
    var startElement = "<Id_conta>";
    var valueStartPosition = webResponse.indexOf(startElement);
    if (valueStartPosition == -1) return "";

    var valueEndPosition = webResponse.indexOf("</Id_conta>", valueStartPosition + startElement.length);
    if (valueEndPosition == -1) return "";

    return webResponse.substring(valueStartPosition + startElement.length, valueEndPosition);
    }


    Hope the information helps.

    Kind regards.
     
Thread Status:
Not open for further replies.