`
mengqingyu
  • 浏览: 329054 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

Voice Xml规范的XML语音交互(含jdom解析)

阅读更多
       VoiceXml简称Vxml通过它可以实现,网页文字发出语音并且可以接收客户端键盘输入操作来进行交互,也可以实现电信服务那种电话自动语音服务。关于Vxml说明文档请见附件!
        用VXML实现像中国移动10086那样的自动语音服务,可以通过写出VXML规范的XML文件来实现,在这里只介绍一下怎么写出可以交互的VXML文件,和怎样用软件来测试它的效果。测试可以用这个免费软件但是它只能读英文和阿拉伯数字,无法读出中文。软件名称:prophecy-8.0.252.0-small-tts-ds.msi在google刻意搜到。下面这个文件是xml格式的jsp文件。这个文件的访问地址可以配置在prophecy软件里,作为语音程序的入口。具体操作可参照prophecy软件附带说明书。这个文件在运行的时候会自动被解析成XML文件并用软件自动读出内容,实现交互。可以写多个嵌套的文件来实现一层一层的交互。需要注意的是这里的动态数据的代码只能内嵌java代码,EL表达式是无法被解析的。例如:
		<% 
			TDictionaryMaintenance parentDic = (TDictionaryMaintenance)request.getAttribute("parentDic");
			TDictionaryMaintenance objectDic = (TDictionaryMaintenance)request.getAttribute("objectDic");
			List dicList = (List)request.getAttribute("childrenDics"); 
		%>
		<prompt>
			<% out.print("Please press 1 to find sites of the "+objectDic.getName()+"."); %>
			<c:if test="<%=dicList!=null%>">
				<%
					for(int i=0;i<dicList.size();i++)
					{
						TDictionaryMaintenance dic = (TDictionaryMaintenance)dicList.get(i);
						String str = "Please press "+(i+2);
						str += " to find "+dic.getName()+".";
						out.print(str);
					}
				%>
			</c:if>
			<% out.print("Please press 0 to return parent menu."); %>
		</prompt>

<%@page contentType="application/voicexml+xml; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<?xml version="1.0"?>
<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">
<form>
	<field name="input" type="number">
	<property name="bargein" value="false"/>
	<prompt>
		I will write content.<!--要读出的内容。-->
	</prompt>
	<!--filled为根据输入的操作来执行方法-->
	<filled>
	<!--当按1时,执行如下操作。-->
	<if cond="input == 1">
		<!--服务请求地址,如下: &amp;等同于以往web下的&符号。-->
		<goto next="<c:url value='/html/voicexml/article.ao?method=searchArticle&amp;code=zcfg'/>"/>
	<elseif cond="input == 2"/>
		<goto next="<c:url value='/html/voicexml/site.ao?method=searchSite&amp;code=zcfg'/>"/>
	<else/>
		input error,please input again.
	</if>
	</filled>
	</field>
</form> 
</vxml>


可以通过jdom对Vxml进行读写操作:
package vxml;
import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;


public class VxmlReader {
	
	private static VxmlReader instance = null;
	private static final String VXML_FLIE_NAME = "VoiceXml.xml";
	private Element vxmlElt;
	
	private VxmlReader() {
		SAXBuilder sb = new SAXBuilder();
		try {
			Document doc = sb.build(Thread.currentThread().getContextClassLoader().getResourceAsStream(VXML_FLIE_NAME));
			this.vxmlElt = doc.getRootElement();
			List<Element> list = this.vxmlElt.getChildren();
			for(Element l : list) {
				System.out.println(l.getValue());
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static VxmlReader getInstance() {
		if(null == instance) {
			instance = new VxmlReader();
		}
		return instance;
	}
	
	public static void main(String[] args) {
		VxmlReader.getInstance();
	}
}

package vxml;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;


public class VxmlWriter {

	public static void main(String[] args) {
		DateFormat dateParser = new SimpleDateFormat("yyyyMMddhhmmss");
		Element vxmlElt = new Element("vxml");
		vxmlElt.setAttribute("version", "2.1");
		Element formElt = new Element("form");
		Element blockElt = new Element("block");
		Element promptElt = new Element("prompt");
		promptElt.addContent("Things are working correctly! Congratulations.");
		blockElt.addContent(promptElt);
		formElt.addContent(blockElt);
		vxmlElt.addContent(formElt);
		Document doc = new Document(vxmlElt);
		
		Format format = Format.getCompactFormat(); 
		format.setEncoding("utf-8"); 
		format.setIndent("	");//设置缩进 
		
		XMLOutputter out = new XMLOutputter(format);
		String str = out.outputString(doc);
		System.out.println(str);
		
		try {
			out.output(doc, new FileOutputStream("c:/VoiceXml" + dateParser.format(new Date()) + ".xml"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

需要jdom.jar
分享到:
评论
2 楼 mengqingyu 2011-02-22  
andilyliao 写道
孟庆禹??

李傲? 哈哈 竟然这里碰到你了。。
1 楼 andilyliao 2011-02-22  
孟庆禹??

相关推荐

Global site tag (gtag.js) - Google Analytics