Java Web 服务和 XML

标签 java xml web-services jax-ws

我需要为家庭作业构建一个简单的程序,该程序将根据 Web 服务中的用户输入从 XML 属性中检索数据。为此,我假设我将开始构建一个可以解析 XML 字符串的类,并且还构建了一个简单的 java 服务,该服务除了响应一条简单的消息之外什么也不做。问题是我如何将它们组合在一起以使我的程序运行?这是一个好的开始方式吗?请指教。

此外,为了让事情变得更容易一点,XML 字符串表示形式中的数据具有英语和塞尔维亚语的关键字,这将使该 Web 服务能够相互检索:

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class Recnik {
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {

        String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE language [<!ATTLIST phrase id ID #IMPLIED>]><language id=\"sr\"><phrase key=\"house\" value=\"kuca\"/><phrase key=\"dog\" value=\"pas\"/><phrase key=\"cat\" value=\"macka\"/></language>";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        //FileInputStream fis = new FileInputStream("myBooks.xml");
        InputSource is = new InputSource(new StringReader(xmlString));
        Document doc = db.parse(is);
        Element r = doc.getDocumentElement();
        NodeList language = r.getElementsByTagName("phrase");
        System.out.println(language.item(1).getAttributes().item(0).getTextContent());


    }
}



package Prevodilac;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

@WebService(serviceName = "Prevodilac")
public class Prevodilac {


    @WebMethod(operationName = "pretraga")
    public String pretraga(int a, int b) {
        Integer res = a+b;
        return res.toString();
    }
}

最佳答案

@WebService(serviceName = "Prevodilac")
public class Prevodilac {

    Document doc;

    public Prevodilac() throws ParserConfigurationException, SAXException, IOException{
        // Fill the document just once, not for each method call
        String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE language [<!ATTLIST phrase id ID #IMPLIED>]><language id=\"sr\"><phrase key=\"house\" value=\"kuca\"/><phrase key=\"dog\" value=\"pas\"/><phrase key=\"cat\" value=\"macka\"/></language>";
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(xmlString));
        doc = db.parse(is);
    }

    @WebMethod(operationName = "pretraga")
    public String pretraga(String key) {
        Element r = doc.getDocumentElement();
        NodeList language = r.getElementsByTagName("phrase");
        String result = "Not found";
        for( int index = 0; index <  language.getLength(); index++ )  {
            Node attribute = language.item(index).getAttributes().getNamedItem("key");
            // TODO (It's homework after all): 
            // check if the attribute corresponds to key parameter
            if( attribute..... ){
                // fill result with attribute value
                result = ...;
            }
        }
        return result;
    }
}

关于Java Web 服务和 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17131560/

相关文章:

java - 当 System.out.println 出现在临界区时,多个线程显示异常值

java - 使用 Button 添加文本时 EditText 水平滚动

java - 有没有办法在 Spring RestTemplate DELETE 调用中传递 header 信息

Java 风格 XML 每行一个节点且无空格

django - 当从 iPad 接收 JSON Ajax 请求时,如何使用 Django 表单?

java - HttpComponentsMessageSender - 默认最大总连接数

Java使用数组查找重复项(包含概念)

java - 调试使用 maven-weblogic-plugin 部署的 Weblogic 应用程序

java - 在Java中可以使用枚举作为列表元素吗?

xml - 带空格的 Excel XML 表格标题