2

我需要为我的家庭作业构建一个简单的程序,该程序将根据 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();
    }
}
4

1 回答 1

3
@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;
    }
}
于 2013-06-16T12:44:49.243 回答