We've Moved! Please visit our new and improved forum over at our new portal: https://portal.plumvoice.com/hc/en-us/community/topics

Problems dereferencing XML object with javascript

Questions and answers about IVR programming for Plum DEV

Moderators: admin, support

Post Reply
rstevehole
Posts: 5
Joined: Fri Nov 29, 2013 4:41 pm

Problems dereferencing XML object with javascript

Post by rstevehole »

This is an addendum to an earlier post.

I am attempting to dereference the following XML object:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<IVRAuthenticationContext>
  <class>com.spiekerpoint.spider.api.IVRAuthenticationContext</class>
  <clientAdministratorId/>
  <name>Sam Electrician</name>
  <workerId>3</workerId>
</IVRAuthenticationContext>

using the following Javascript methods:

Code: Select all

        function getElementText(document, tag) {
             element = document.getElementsByTagName(tag).item(0);
             if (element) {
                 contentNodes = element.childNodes
                 textNode = contentNodes.firstChild
                 if (textNode) { 
                     return(textNode.nodeValue)
                 }
             }
         }

Referencing the function above with the following:

Code: Select all

    <assign name="workerId" expr="getElementText(context,'workerId')" />
always returns null. Numerous tests with serveral variants of the function indicates the there are no text nodes in the DOC under the "<workerId>" element tag. Without some mechanism for debugging the XML parsing, I can't really say why the text nodes don't appear. Any help would be much appreciated.

support
Posts: 3632
Joined: Mon Jun 02, 2003 3:47 pm
Location: Boston, MA
Contact:

Re: Problems dereferencing XML object with javascript

Post by support »

Hi,

We found that a slight modification to the getElementText method allowed for the node value to be properly pulled. We tested this with this script:

Code: Select all

<?php
header("Content-type: text/xml");
echo "<?xml version=\"1.0\"?>";
?>

<vxml version="2.0">
  <var name="workerId"/>
  <script>
    function getElementText(document, tag) {
          element = document.getElementsByTagName(tag).item(0);
          if (element) {
              textNode = element.firstChild
/*
              contentNodes = element.childNodes
              textNode = contentNodes.firstChild
*/
              if (textNode) {
                  return(textNode.nodeValue)
              }
          }
      }
  </script>
  <form>
    <block>
      <data name="context" src="example_xml.xml"/>
      <assign name="workerId" expr="getElementText(context,'workerId')" />
      <prompt> Worker ID: <value expr="workerId"/> </prompt>
    </block>
  </form>
</vxml>
The example_xml.xml file was the XML you provided.


Hope this helps.

Regards,
Plum Support

rstevehole
Posts: 5
Joined: Fri Nov 29, 2013 4:41 pm

Re: Problems dereferencing XML object with javascript

Post by rstevehole »

... which is a simpler form of the script that I had -- which is good. Note that I had tried using "element.textValue" which also should have worked, but it came back with null as well. FYI.

Thanks for your time.

Post Reply