使用Java中的Web服务检索XML数据

时间:2016-09-19 09:37:51

标签: java xml web-services xpath

我需要一些关于必须从XML文件中检索和检查有效元素的Web服务的帮助。网络服务方法应如下所示 - 翻译("花","英语","俄语"),客户应该能够允许您输入想要的单词,原始语言和目标语言。如果这些文件存在于XML文件中,则它的翻译将显示在客户端,否则将显示错误消息。

我已经创建了一个标准的Web服务方法 - 添加(int a,int b)并使用普通的java应用程序创建了客户端部分,添加了一个与wsdl链接相连的swing GUI,该方法运行良好。如何让这个程序工作最好?请指教。

服务如下:

@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>

XML源类:

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;

    }    
}

客户GUI sample of the program

2 个答案:

答案 0 :(得分:0)

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

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);

答案 1 :(得分:0)

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));
    }
}