/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Sercultur.lists;
import Sercultur.classes.Distrito;
import XmlUtils.XmlUtil;
import java.io.StringReader;
import java.util.LinkedHashSet;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
*
* @author LAP01
*/
public class DistritoList {
public static LinkedHashSet<Distrito> getAll(){
LinkedHashSet<Distrito> list = new LinkedHashSet<Distrito>();
Document doc = getServiceDoc();
NodeList rows = doc.getElementsByTagName("Row");
for(int i = 0; i < rows.getLength(); i++ ){
Element e = (Element)rows.item(i);
Element numeros = (Element) e.getElementsByTagName("Numero").item(0);
Element distritos = (Element) e.getElementsByTagName("Distrito").item(0);
list.add(
new Distrito( Integer.parseInt(numeros.getTextContent()),
distritos.getTextContent())
);
}
return list;
}
private static Document getServiceDoc(){
SerculturServices.Sws service = new SerculturServices.Sws();
QName portQName = new QName("http://ws.sercultur.pt/", "SwsSoap12");
String req = "<Distritos xmlns=\"http://ws.sercultur.pt/\"></Distritos>";
try { // Call Web Service Operation
Dispatch<Source> sourceDispatch = null;
sourceDispatch = service.createDispatch(portQName, Source.class, Service.Mode.PAYLOAD);
Source result = sourceDispatch.invoke(new StreamSource(new StringReader(req)));
return XmlUtil.getDocFromSource(result);
} catch (Exception ex) {
System.out.println("Error:"+ex.getMessage());
}
return null;
}
}