英文:
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:
<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 code:
<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>
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论