2006 IR Open Discussion Posted August 18, 2006 Share Posted August 18, 2006 By: Eric Everman - eeverman Contribution: XML Datasource 2003-04-01 14:17 Hi- I would like to contribute an xml datasource wrapper that I have been using. It wraps an xml DOM Element as the datasource and supports accessing elements and attributes via paths. It also supports default values and subtrees for subreports. How should I go about getting it to you? Thanks, Eric Everman By: Alvaro Badia Tejedor - albate RE: Contribution: XML Datasource 2003-04-08 00:39 Hello, I would like to follow with the project, but now in I am busy with other things. Nevertheless this is the code that I made: ################################################### package dori.jasper.engine.data; import dori.jasper.engine.JRDataSource; import dori.jasper.engine.JRException; import dori.jasper.engine.JRField; import java.util.*; import org.jdom.*; import jasperreport.XmlContainer; /** * <p>Tí´µlo: JasperReport</p> * <p>Descripció®ºGeneracion de Informes</p> * <p>Copyright: Copyright © 2003</p> * <p>Empresa: </p> * @author Alvaro Badia * @version 1.0 */ public class JRXMLDataSource implements JRDataSource { private Collection data = null; private Iterator iterator = null; private Element currentElement = null; private Element previousElement = null; private Element raiz_xml = null; private boolean haysiguiente = true; /** * Constructor que recibe como argumento el nodo raíºde un * fichero XML */ public JRXMLDataSource(Element xmlRootElement) { this.raiz_xml = xmlRootElement; this.data = xmlRootElement.getChildren(); this.haysiguiente=true; if (this.data != null){ this.iterator = this.data.iterator(); } } /** * Constructor que recibe como argumento la ruta del fichero XML * */ public JRXMLDataSource(String nombreFichero) { Element xmlRootElement = null; XmlContainer accXML = new XmlContainer(); accXML.abrirFicheroXml(nombreFichero); xmlRootElement= accXML.getRaiz(); new JRXMLDataSource(xmlRootElement); } /** * */ public boolean next() throws JRException { boolean hasNext = false; if (this.iterator != null) { hasNext = this.iterator.hasNext(); if (hasNext) { this.currentElement = (Element)this.iterator.next(); } } if (this.previousElement != null) { if ( (this.previousElement.getName().equals (this.currentElement.getName())) != true) { this.haysiguiente = false; } } return hasNext && this.haysiguiente; } /** * NOTA: * Los nombre de los elementos deben de ser de la siguiente Forma: * nodoPadre_nodoHijo_ ... ( y asi sucesivamente hasta la profundidad del nodo * de la cual queramos el texto). * ejemplo : * * libros.xml: * <libros> * <libro> * <autor>Gabriel Garcia Marquez</autor> * <titulo>El coronel no tiene quien le escriba</titulo> * </libro> * </libros> * Nombre del campo 'autor': * jrField.name= libro_autor */ public Object getFieldValue(JRField jrField) throws JRException{ Object value = null; if (currentElement != null){ //comprobamos los nivele que tiene el nodo que estamos llamando //esto lo hacemos buscando los '_' . Vector campos = separarNombre(jrField.getName()); // itero sobre el vector de campos para llegar al nodo y devolver el valor Element nodoFinal=null; if (campos.size() == 1){ if (currentElement.getParent().getChild(jrField.getName ()) == null) value=""; else value = currentElement.getParent().getChild (jrField.getName()).getText(); this.previousElement=currentElement; }else{ for (int i = 0; i < campos.size(); i++) { nodoFinal = currentElement.getChild( (String) campos.get(i)); } if (nodoFinal == null) value=""; else value = nodoFinal.getText(); this.previousElement=currentElement; } } return value; } /** * Mé´¯do que pasᮤole como argumento una cadena con * el carᣴer '_' devuelve un Vector con las palabras separadas por el carᣴer. * */ private Vector separarNombre(String cadena) { Vector campos = new Vector(); int inicio = 0; int fin = 0; int indice = cadena.indexOf("_"); while (indice != ( -1)) { fin = indice; campos.add(cadena.substring(inicio, fin).trim()); cadena = cadena.substring(fin + 1); indice = cadena.indexOf("_"); }; campos.add(cadena.trim()); return campos; } } ################################################### This code does not work well absolutely. But if that fills up report with the data of the XML-file. If you like conntact with me , my mail is alvaroBT@ono.com. And it will clarify to you what can. Thanks you, Alvaro Badia. p.d. I'd like to participate in the project, but I have very much work. Sorry. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now