0

我需要从textarea传递XML字符串。JSF问题是它XML被编码了,我无法将它传递给验证。我的验证方法如下所示:

public boolean validate(String data) {
    Source xmlFile = new StreamSource(data);
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {
        URL kenexaSchema = getClass().getClassLoader().getResource("KenexaXMLConfiguration.xsd");
        Schema schema = schemaFactory.newSchema(kenexaSchema);
        Validator validator = schema.newValidator();
        validator.validate(xmlFile);
        LOGGER.debug(xmlFile.getSystemId() + " is valid");
        return true;
    } catch (SAXException ex) {
        LOGGER.error(xmlFile.getSystemId() + " is not valid.", ex);
        LOGGER.error("Reason: " + ex.getLocalizedMessage());
        return false;
    } catch (IOException ex) {
        LOGGER.error("IO Exception occurred while validating proivder data.", ex);
        LOGGER.error("Reason: " + ex.getLocalizedMessage());
        return false;
    }
}

输入参数dataXML String. schemaFactory调用方法时newSchema出现错误:

java.net.MalformedURLException: no protocol: %3C?xml%20version=%221.0%22%20encoding=%22UTF-8%22?%3E%0D%0A%3Cxmlresults%3E%0D%0A%20%20%20%20%3Cdata%3E%0D%0A%20%20%20%20%20%20%20%20%3Cid%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Crespondentdata%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctestid%3E22000%3C/testid%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Crespondentid%3Ermtest13%3C/respondentid%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctestsessionid%3E5981%3C/testsessionid%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctestinstanceid%3E136485%3C/testinstanceid%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cexample%3E0%3C/example%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cstatus_code%3E0%3C/status_code%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cstatus_desc%3ECOMPLETED%20SUCCESSFULLY%3C/status_desc%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cstart_time%3E14/08/06%2016:22:32%3C/start_time%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cend_time%3E14/08/06%2016:23:42%3C/end_time%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C/respondentdata%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cresultdata%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cscale%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cscalename%3ESocial%3C/scalename%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Craw_score%3E28.00%3C/raw_score%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ct-score%3E44%3C/t-score%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Csten_scrore%3E4%3C/sten_scrore%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctime_taken%3E65686%3C/time_taken%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Citems_attempted%3E80%3C/items_attempted%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cfinal_score%3E26%3C/final_score%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C/scale%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cscale%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cscalename%3ELeader%3C/scalename%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Craw_score%3E23.00%3C/raw_score%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ct-score%3E27%3C/t-score%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Csten_scrore%3E1%3C/sten_scrore%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctime_taken%3E658654%3C/time_taken%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Citems_attempted%3E80%3C/items_attempted%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Cfinal_score%3E26%3C/final_score%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C/scale%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C/resultdata%3E%0D%0A%20%20%20%20%20%20%20%20%3C/id%3E%0D%0A%20%20%20%20%3C/data%3E%0D%0A%3C/xmlresults%3E

是否有某种方法可以传递未编码的XML来自JSFtextArea 的内容?我正在使用JSF 2.0,应用程序部署在 Tomcat 6 上。

4

1 回答 1

1

很抱歉误报。问题不是 JSF textarea,而是这一行:

URL kenexaSchema = getClass().getClassLoader().getResource("KenexaXMLConfiguration.xsd");

我找到了以不同方式获取 XSD 的方法,现在它正在工作。

于 2012-08-14T15:13:53.693 回答