A cotinuacion una clase que lee XML, podemos modificarla para que nos sirva con nuestro codigo:
XML:
"<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
"<S:Body>"+
"<ns2:obtenerPromocionesResponse xmlns:ns2=\"http://facade.doll.aplicaciones.clarochile.cl\">" +
"<ns2:obtenerPromocionesParamOut>" +
" <codigo>1</codigo>" +
"<promociones>" +
" <PromotionIVRTO>" +
" <codigoIVR>10010</codigoIVR>" +
" <opcion>0</opcion>" +
" </PromotionIVRTO>" +
" <PromotionIVRTO>" +
" <codigoIVR>10020</codigoIVR>" +
" <opcion>1</opcion>" +
" </PromotionIVRTO>" +
" <PromotionIVRTO>" +
" <codigoIVR>10030</codigoIVR>" +
" <opcion>2</opcion>" +
" </PromotionIVRTO>" +
" </promociones>" +
" </ns2:obtenerPromocionesParamOut>" +
" </ns2:obtenerPromocionesResponse>" +
" </S:Body>" +
"</S:Envelope>"
import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;
public class ParseaXml {
//constructor
public ParseaXml(){}
//metodo parseador
public void leeXML(String tx){
Document doc = null;
InputSource is = new InputSource(new StringReader(tx));
SAXBuilder builder = new SAXBuilder(false);
boolean flag = false;
try{
doc = builder.build(is);
}catch(JDOMException jdome){
System.out.println(jdome);
}catch(IOException ioe){
System.out.println(ioe);
}catch(Exception e){
System.out.println(e);
}
/**
* Se obtiene el elemento raiz
*/
Element raiz = doc.getRootElement();
List<Element> list = raiz.getChildren();
List<Element> listPromociones = null;
while(list.size()!=0){
list = list.get(0).getChildren();
Iterator<Element> itElem = list.iterator();
for(; itElem.hasNext();){
Element e = itElem.next();
System.out.println(e.getName() + " - " + e.getText());
if(e.getName().equals("promociones")){
listPromociones = e.getChildren();
flag = true;
}
}
}
if (flag){
Iterator<Element> itElem = listPromociones.iterator();
for(; itElem.hasNext();){
Element e = itElem.next();
List<Element> prom = e.getChildren();
System.out.println(prom.get(0).getName() + " - " + prom.get(0).getText());
System.out.println(prom.get(1).getName() + " - " + prom.get(1).getText());
}
}
}
}
No hay comentarios:
Publicar un comentario