Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用web服务检索XML数据_Java_Xml_Web Services_Xpath - Fatal编程技术网

在Java中使用web服务检索XML数据

在Java中使用web服务检索XML数据,java,xml,web-services,xpath,Java,Xml,Web Services,Xpath,关于必须从XML文件检索和检查有效元素的Web服务,我需要一些帮助。web服务方法应该是这样的-翻译(“flower”、“english”、“russian”),客户端应该允许您输入所需的单词、原始语言和目标语言。如果这些存在于XML文件中,则其翻译将显示在客户端,如果不存在,则将显示错误消息 我已经创建了一个标准的web服务方法-add(intA,intB),还使用普通java应用程序创建了客户机部分,添加了一个swing GUI,与wsdl链接连接,该方法运行良好。对我来说,怎样才能使这个计

关于必须从XML文件检索和检查有效元素的Web服务,我需要一些帮助。web服务方法应该是这样的-翻译(“flower”、“english”、“russian”),客户端应该允许您输入所需的单词、原始语言和目标语言。如果这些存在于XML文件中,则其翻译将显示在客户端,如果不存在,则将显示错误消息

我已经创建了一个标准的web服务方法-add(intA,intB),还使用普通java应用程序创建了客户机部分,添加了一个swing GUI,与wsdl链接连接,该方法运行良好。对我来说,怎样才能使这个计划起到最好的作用?请给我一些建议

该服务如下所示:

@WebMethod(operationName = "translate")
    public String translate(@WebParam(name = "word") String word, 
            @WebParam(name = "originalLanguage") String originalLanguage, 
            @WebParam(name = "targetLanguage") String targetLanguage) 
            throws Exception {

        XMLSource dataLSource = new XMLSource();
        return dataLSource.getTranslation(word);

    }
考虑到XML文件应该如下所示:translation.XML

<?xml version="1.0" encoding="UTF-8"?>
<translation>
    <word>
        <english>car</english>
        <russian>avtomobil</russian>    
    </word>
    <word>
        <english>flower</english>
        <russian>tsvetok</russian>    
    </word>
    <word>
        <english>butterfly</english>
        <russian>babochka</russian>   
    </word>
</translation>

客户端

您可以将此作为起点,并根据需要进行修改。这意味着您需要使用InputSource对象加载文件,然后使用XPath从XML文件中提取数据。网上有很多关于如何做到这一点的例子和教程。下面的代码片段来自

stringxml=“goodhi”;
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xpathFactory.newXPath();
InputSource source=新的InputSource(新的StringReader(
xml);
String status=xpath.evaluate(“/resp/status”,source);
System.out.println(“satus=“+状态”);

您可以将此作为起点,并根据需要进行修改。这意味着您需要使用InputSource对象加载文件,然后使用XPath从XML文件中提取数据。网上有很多关于如何做到这一点的例子和教程。下面的代码片段来自

stringxml=“goodhi”;
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xpathFactory.newXPath();
InputSource source=新的InputSource(新的StringReader(
xml);
String status=xpath.evaluate(“/resp/status”,source);
System.out.println(“satus=“+状态”);

您尝试了什么?什么部件现在不工作?请给我们看一些代码。您可以使用DOM解析器从XML文件中提取信息,然后检查传递给web服务的值。例如,查看这篇关于如何做到这一点的帖子:。或者,您可以使用SAX解析器。但是,更根本的是,为什么不将转换矩阵存储在关系数据库中呢?为什么它需要是XML?解析XML并将其作为WS-call的一部分进行搜索对我来说效率很低。如何最好地检查为给定xml文件传递的值?您尝试了什么?什么部件现在不工作?请给我们看一些代码。您可以使用DOM解析器从XML文件中提取信息,然后检查传递给web服务的值。例如,查看这篇关于如何做到这一点的帖子:。或者,您可以使用SAX解析器。但是,更根本的是,为什么不将转换矩阵存储在关系数据库中呢?为什么它需要是XML?解析XML并将其作为WS-call的一部分进行搜索对我来说效率很低。如何最好地检查为给定xml文件传递的值?因此,您的建议是,在这种情况下使用XPath比使用DOM更好。我会尝试一下,然后很快回复。我并不是说它比DOM好,但这是一种非常简单的方法,可以实现您所描述的。DOM的问题是,每次调用代码时,它都会读取和解析整个XML文件,而SAX则逐行解析文件,所需内存要少得多。不确定XPath代码在重新使用内存时的具体行为,但您应该检查这是否是一个考虑因素。这个编程任务还有其他要求吗?要求如下:您需要创建两种不同语言之间的翻译服务。在服务中,单词的DB应该放在一个具有随机结构的XML文件中。当客户端请求翻译单个单词时,服务将验证XML文件中是否存在该单词,并返回已翻译单词的值。否则,服务将返回错误消息。客户端使用描述的服务调用带有3个字符串参数的translate方法,如前所述。我尝试创建一个使用XPath解析器的XML源类,并在服务中调用它,但一直向我抛出InvocationTargetException。我如何解决另一个问题呢?所以您的建议是,在这种情况下使用XPath比使用DOM要好。我会尝试一下,然后很快回复。我并不是说它比DOM好,但这是一种非常简单的方法,可以实现您所描述的。DOM的问题是,每次调用代码时,它都会读取和解析整个XML文件,而SAX则逐行解析文件,所需内存要少得多。不确定XPath代码在重新使用内存时的具体行为,但您应该检查这是否是一个考虑因素。这个编程任务还有其他要求吗?要求如下:您需要创建两种不同语言之间的翻译服务。在服务中,单词的DB应该放在一个具有随机结构的XML文件中。当客户端请求翻译单个单词时,服务将验证XML文件中是否存在该单词,并返回已翻译单词的值。否则,服务将返回错误消息。客户端使用描述的服务调用带有3个字符串参数的translate方法,如前所述。我尝试创建一个使用XPath解析器的XML源类,并在服务中调用它,但一直向我抛出InvocationTargetException。我如何解决这个问题?您不应该从代码中返回类似
Error
的任何内容,因为该值也可以是对so完全有效的转换值
public class XMLSource {

    private Document readData() throws Exception{

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        File xmlFile = new File("translation.xml");
        return db.parse(new FileInputStream(xmlFile));

    }

    public String getTranslation(String original) throws Exception{

        Document doc = readData();
        XPathFactory xpf = XPathFactory.newInstance();
        XPath xp = xpf.newXPath();
        XPathExpression xpe = xp.compile("/translation/word/russian[../english"
                + "/text()='"+original+"'] ");
        String translated = (String) xpe.evaluate(doc, XPathConstants.STRING);
        return translated;

    }    
}
String xml = "<resp><status>good</status><msg>hi</msg></resp>";

XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();

InputSource source = new InputSource(new StringReader(
xml));
String status = xpath.evaluate("/resp/status", source);

System.out.println("satus=" + status);
public class XMLSource {
    public String getTranslation(String original) throws Exception{
        Document doc;
         try {
            doc = readData();
            XPathFactory xpf = XPathFactory.newInstance();
            XPath xp = xpf.newXPath();
            XPathExpression xpe = xp.compile("/translation/word/russian[../english"
                    + "/text()='"+original+"'] ");
            String translated = (String) xpe.evaluate(doc, XPathConstants.STRING);
            if ("".equals(translated)) return " word not found"; 

            return translated;
         } catch (Exception ex) {
             Logger.getLogger(XMLSource.class.getName()).log(Level.SEVERE, null, ex);
             return "Error";
         }
     }   

    private Document readData() throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        File xmlFile = new File("translation.xml");/*enter the absolute path to the XML file.for example"C:\\Users\\User\\Documents\\NetBeansProjects\\WebServiceTranslate\\src\\java\\service\\translation.xml"*/ 
        return (Document) db.parse(new FileInputStream(xmlFile));
    }
}