Ivan Ćeličanin
Ivan Ćeličanin

Reputation: 79

Java Web Service & XML

I would need to build a simple program for my homework purposes that will retrieve data from an XML attribute based on the user input in a web service. To that end, I assumed I would start building a class that could parse my XML string and also I built a simple java service that does nothing but responds with a simple message. The problem is how do I put these together in order to get my program to work? Is this a good way to begin with? Please advise.

Also, to make thing a little more easier, the data in the string representation of XML has key words in both English and Serbian that would enable this web service to retrieve from one another:

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

Upvotes: 2

Views: 3878

Answers (1)

flup
flup

Reputation: 27104

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

Upvotes: 3

Related Questions