2011-10-27 4 views
0

구문 분석을위한 좋은 방법이 있는지 궁금합니다. org.w3c.dom.Document ~ org.enhydra.wireless.voicexml.dom.VoiceXMLDocument?Document를 VoiceXMLDocument로 파싱하는 방법이 있습니까? [Java - Android]

나는 JavaAndroid에 사용하고 있습니다. 내 신청서는 VoiceXML 파일을 InputStream으로 읽습니다. InputStream에서 DocumentBuilderFactory을 사용하여 문서를 얻을 수 있습니다. 하지만 이번에는 DocumentVoiceXMLDocument으로 파싱합니다. 아래는 나의 부드러운 코드입니다.

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    btnLoadFile = (Button) findViewById(R.id.btnLoadFile); 

    btnLoadFile.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      InputStream input = downloadFile(fileUrl); 
      try { 
       Document dom = getVXML(input); 
       /** 
       * I'm stuff at here. 
       * Cannot parsing Document to VoiceXMLDocument. 
       * It will make an error 
       * */ 
       VoiceXMLDocument voiceXMLDocument = (VoiceXMLDocument) dom; 
      } catch (Exception e) { 
       Log.e("Exception = ", e.getMessage()); 
      } 
     } 
    }); 
} 

/** 
* Convert InputStream to Document 
* @param inputStream (InputStream) 
* @return dom (Document) 
* */ 
public Document getVXML(InputStream inputStream) 
     throws FileNotFoundException { 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    Document dom = null; 
    try { 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     dom = db.parse(inputStream); 
    } catch (ParserConfigurationException pce) { 
     pce.printStackTrace(); 
    } catch (SAXException se) { 
     se.printStackTrace(); 
    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } 
    return dom; 
} 

답변

0

나는이 주제에 대한 약간의 시간을 보내고 있고 문서를 구문 분석하지 않고 VXML 파일에서 VoiceXMLDocument을 얻을 수있는 좋은 방법을 발견했다. 아래는 내가 어떻게이 일을 끝내는지를 보여줍니다.

첫 번째 단계 : 다음 jar 파일을 가져 오십시오. jvxml-jsapi1.0.jar - jvxml-xml.jar - jvxml.jar - log4j-1.2.13.jar. 인터넷에서이 jar 파일을 찾을 수 있다고 생각합니다.

제 2 공정의 InputStream로 파일 VXML을 다운로드하고 읽을

/** 
* Download file from Internet 
* 
* @param fileUrl 
*   - file location from Internet 
* @return InputStream - file input stream 
* */ 
public static InputStream downloadFile(String fileUrl) { 
    InputStream input = null; 
    if (checkUrl(fileUrl)) { 
     try { 
      HttpUriRequest request = new HttpGet(fileUrl.toString()); 
      DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); 
      setProxy(defaultHttpClient); 
      HttpClient httpClient = defaultHttpClient; 

      HttpResponse response = httpClient.execute(request); 

      StatusLine statusLine = response.getStatusLine(); 
      int statusCode = statusLine.getStatusCode(); 
      if (statusCode == 200) { 
       HttpEntity entity = response.getEntity(); 
       BufferedHttpEntity buf = new BufferedHttpEntity(entity); 
       input = buf.getContent(); 
       return input; 
      } else { 
       throw new IOException(
         "Download failed, HTTP response code " + statusCode 
           + " - " + statusLine.getReasonPhrase()); 
      } 
     } catch (Exception e) { 
      return input; 
     } 
    } else { 
     return input; 
    } 
} 

/** 
* Check URL 
* 
* @param fileUrl 
*   - file location from Internet 
* @return boolean - URL link is valid or invalid 
* **/ 
private static boolean checkUrl(String fileUrl) { 
    URL u; 
    try { 
     u = new URL(fileUrl);// this would check for the protocol 
     u.toURI();// does the extra checking required for validation of URI 
     return true; 
    } catch (MalformedURLException e) { 
     return false; 
    } catch (URISyntaxException e) { 
     return false; 
    } 
} 

public static void setProxy(DefaultHttpClient httpclient) { 
    final String PROXY_IP = "10.10.10.10"; 
    final int PROXY_PORT = 8080; 
    final String username = ""; 
    final String password = ""; 

    httpclient.getCredentialsProvider().setCredentials(
      new AuthScope(PROXY_IP, PROXY_PORT), 
      new UsernamePasswordCredentials(username, password)); 

    HttpHost proxy = new HttpHost(PROXY_IP, PROXY_PORT); 

    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, 
      proxy); 
} 

세 번째 단계 : VoiceXmlDocument

에 입력 스트림을 구문 분석
private static VoiceXmlDocument getVoiceXMLDocument(InputStream input) { 
    VoiceXmlDocument vdom = null; 
    try { 
     vdom = new VoiceXmlDocument(new InputSource(input)); 
    } catch (ParserConfigurationException ex) { 
     Log.e("ParserConfigurationException = ", ex.getMessage()); 
    } catch (SAXException ex) { 
     Log.e("SAXException = ", ex.getMessage()); 
    } catch (IOException ex) { 
     Log.e("IOException = ", ex.getMessage()); 
    } 
    return vdom; 
} 

최종 단계 : 당신이

을 원하는 VoiceXMLDocument에서 요소를 가져옵니다
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button download = (Button)findViewById(R.id.button1); 
    download.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      String fileUrl = "http://www.ampersand.com/vxml_tests/ex.vxml"; 
      InputStream input = downloadFile(fileUrl); 
      Log.e("input = ", input.toString()); 
      try { 
       VoiceXmlDocument voiceXmlDocument = getVoiceXMLDocument(input); 
       Log.e("prompt = ", voiceXmlDocument.getElementsByTagName("prompt").item(0).toString()); 
      } catch (Exception e) { 
       return; 
      } 
     } 

    }); 
} 

이 방법은 vxml 파일에서 프롬프트 태그의 정보를 인쇄합니다. Android Log에서 볼 수 있습니다.

행운을 빌어 요!

+0

VXML 관련 도움을받을 수 있습니까? –