XML中的nodeValue属性未返回节点的值

huangapple go评论69阅读模式
英文:

nodeValue attribute in XML not returning the node's value

问题

我正在尝试在VXML中处理从API接收的XML数据。请求是使用<data>元素进行的。我能够从XML对象中检索到nodeNames,但nodeValues为空。

用于处理XML数据的函数如下:

<script>
  <![CDATA[
    function xmlToJson(root) {
      var nodes = root.childNodes;
      var res = {};

      for (var i = 0; i < nodes.length; i++) {
        res[nodes.item(i).nodeName] = nodes.item(i).nodeValue;
      }
      return res;
    };
  ]]>
</script>

VXML代码:

<block>
  <data name="xmlData" src="/getJSON" />
  <var name="root" expr="xmlData.documentElement" />
  <var name="cleanData" expr="xmlToJson(root)" />

  <log label="HrushikeshPracticeIvr|Index.jsp" expr="cdrkey">
    XML to JSON::: <value expr="cleanData" />
  </log>

  <exit />
</block>

从VXI浏览器的输出:

Jan 2 12:16:09.30|47201006917376|3|8001|com.aumtechinc.vxi|[label:HrushikeshPracticeIvr|Index.jsp]
[expr:null] XML to JSON::: { (birthDate, ), (firstName, ), (id, ), (lastName, ), (version, ) }

我参考了以下来源来实现上述代码:https://www.w3.org/TR/voicexml21/#sec-data-dom

英文:

I am trying to process XML data received from an API in VXML. The request is made using <data> element. I am able to retrieve nodeNames from the XML object, but nodeValues are empty.

Function used to process xml data:

&lt;script&gt;
	&lt;![CDATA[
		function xmlToJson(root) {
			
			var nodes = root.childNodes;
			var res= {};

			for(var i=0; i&lt;nodes.length; i++) {
				res[nodes.item(i).nodeName] = nodes.item(i).nodeValue;
			}
			return res;
			
		};
	]]&gt;		

	&lt;/script&gt;

VXML code:

&lt;block&gt;
			&lt;data name=&quot;xmlData&quot; src=&quot;/getJSON&quot; /&gt;
			&lt;var name=&quot;root&quot; expr=&quot;xmlData.documentElement&quot;/&gt;
			&lt;var name = &quot;cleanData&quot; expr = &quot;xmlToJson(root)&quot; /&gt;
			
			&lt;log label=&quot;HrushikeshPracticeIvr|Index.jsp&quot; expr=&quot;cdrkey&quot;&gt; XML to JSON::: &lt;value expr = &quot;cleanData&quot; /&gt; &lt;/log&gt;
			
			&lt;exit /&gt;
		&lt;/block&gt;

Output from VXI browser:

> Jan 2
> 12:16:09.30|47201006917376|3|8001|com.aumtechinc.vxi|[label:HrushikeshPracticeIvr|Index.jsp]
> [expr:null] XML to JSON::: { (birthDate, ), (firstName, ), (id, ),
> (lastName, ), (version, ) }

I have referred following source for above code implementation:
https://www.w3.org/TR/voicexml21/#sec-data-dom

答案1

得分: 1

请检查返回的XML内容。我使用过的大多数VoiceXML浏览器会在值周围存在空白字符(例如空格、回车、换行符)时创建额外的子节点。

英文:

Double check the contents of the returned XML. Most of the VoiceXML browsers I've used will create additional child nodes if there is white space around the value (e.g. spaces, carriage returns, line feeds).

huangapple
  • 本文由 发表于 2020年1月3日 15:20:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定