iReport 如何试用xml为数据源
有没有实现代码

解决方案 »

  1.   

    import   java.io.FileOutputStream; 
    import   java.io.IOException; 
    import   java.util.ArrayList; 
    import   java.util.List; 
    import   org.jdom.Document; 
    import   org.jdom.Element; 
    import   org.jdom.JDOMException; 
    import   org.jdom.input.SAXBuilder; 
    import   org.jdom.output.Format; 
    import   org.jdom.output.XMLOutputter; 
    public   class   StudentDAO   { 
    String   file;//xml文件的位置 
    FileOutputStream   fo; 
    Document   doc; 
    SAXBuilder   builder   =   new   SAXBuilder(); 
    Format   format;   //用于设置xml文件的格式 
    private   StudentDAO()   { 

    //构造函数中设置file和format 
    public   StudentDAO(String   webroot)   {   
    file   =   webroot   +   "/data/student.xml "; 
    format   =   Format.getPrettyFormat(); 
    format.setIndent( "   "); 
    format.setEncoding( "utf-8 "); 

      //向xml文件中添加一条记录 
    public   boolean   add(StudentInfo   si)   { 
    String   id   =   si.getId(); 
    boolean   idAlreadyExisted   =   false;   
    boolean   addResult   =   false; 
    Element   root; 
    List   allStudent; 
    try   { doc   =   builder.build(file); 
    root   =   doc.getRootElement(); 
    allStudent   =   root.getChildren(); 
    //检查要添加的信息是否存在 
    for   (int   i   =   0;   i   <   allStudent.size();   i++)   { 
    Element   element   =   (Element)   allStudent.get(i); 
    if   (element.getChild( "id ").getText().equals(id))   { 
    idAlreadyExisted   =   true; 
    break; 


    //向xml文件中添加新的信息,并返回true 
    if   (!idAlreadyExisted)   { 
    Element   student   =   new   Element( "student "); 
    student.addContent(new   Element( "id ").setText(si.getId())); 
    student.addContent(new   Element( "name ").setText(si.getName())); 
    student.addContent(new   Element( "birthday ").setText(si 
    .getBirthday())); 
    student.addContent(new   Element( "address ").setText(si 
    .getAddress())); 
    allStudent.add(student); 
    XMLOutputter   xout   =   new   XMLOutputter(format); 
    fo   =   new   FileOutputStream(file); 
    xout.output(doc,   fo); 
    return   true; 
    } }   catch   (JDOMException   e)   { 
    e.printStackTrace(); 
    }   catch   (IOException   e)   { 
    e.printStackTrace(); 
    }   finally   { 
    close(); 

    return   addResult; 

    //从xml文件中删除一条信息 
    public   boolean   del(String   id)   { 
    boolean   delResult   =   false; 
    Element   root; 
    List   allStudent; 
    try   { 
    doc   =   builder.build(file); 
    root   =   doc.getRootElement(); 
    allStudent   =   root.getChildren(); 
    for   (int   i   =   0;   i   <   allStudent.size();   i++)   { 
    Element   student   =   (Element)   allStudent.get(i); 
    //如果id存在,则删除些条信息,并返回true 
    if   (student.getChild( "id ").getText().equals(id))   { 
    allStudent.remove(i); 
    XMLOutputter   xout   =   new   XMLOutputter(format); 
    fo   =   new   FileOutputStream(file); 
    xout.output(doc,   fo); 
    return   true; 

    } }   catch   (JDOMException   e)   { 
    //   TODO   Auto-generated   catch   block 
    e.printStackTrace(); 
    }   catch   (IOException   e)   { 
    //   TODO   Auto-generated   catch   block 
    e.printStackTrace(); 
    }   finally   { 
    close(); 

    return   delResult; 

    //按名字进行查询,返回所有名字以name开头的记录 
    public   ArrayList   query(String   name)   { 
    ArrayList   list   =   new   ArrayList(); 
    Element   root; 
    List   allStudent; 
    try   { 
    doc   =   builder.build(file); 
    root   =   doc.getRootElement(); 
    allStudent   =   root.getChildren(); for   (int   i   =   0;   i   <   allStudent.size();   i++)   { 
    Element   student   =   (Element)   allStudent.get(i); 
    if   (student.getChild( "name ").getText().startsWith(name))   { 
    StudentInfo   si   =   new   StudentInfo(); 
    si.setId(student.getChild( "id ").getText()); 
    si.setName(student.getChild( "name ").getText()); 
    si.setBirthday(student.getChild( "birthday ").getText()); 
    si.setAddress(student.getChild( "address ").getText()); 
    list.add(si); 


    }   catch   (JDOMException   e)   { 
    //   TODO   Auto-generated   catch   block 
    e.printStackTrace(); 
    }   catch   (IOException   e)   { 
    //   TODO   Auto-generated   catch   block 
    e.printStackTrace(); 

    return   list; 

    void   close()   { 
    if   (fo   !=   null)   { 
    try   { 
    fo.close(); 
    }   catch   (IOException   e)   { 
    e.printStackTrace(); 



      

  2.   

    资料来源>连接资料来源
    new一个 XML FILE DATASOURCE以及.XML文件名 填入 你的XML根节点 子节点 一直写到你当前要调用的如下格式:
    /popl/mstVO
     根    子    
    我当钱要用的就是  mstVO 下的 XXXXX节点
      

  3.   

    推荐jasperreport QQ群121174498  
    8错的群