首页 > 开发 > AJAX > 正文

AJAX级联下拉框的简单实现案例

2024-09-01 08:28:50
字体:
来源:转载
供稿:网友

需要的JAVA类
代码如下:
package com.ajaxlab.ajax;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import com.ajaxlab.ajax.ProductClass;

public class ClassService {
   private Document dom;
   public ClassService(){
    try{
     SAXBuilder builder=new SAXBuilder();
     this.dom=builder.build(ClassService.class.getResource("product.xml"));
    }catch(Exception e){
    e.printStackTrace();
    }
   }
   public ProductClass[] getAllClass1(){
    Collection products=new ArrayList();
    Iterator iterator=this.dom.getRootElement().getChildren().iterator();
    do{
     Element element=(Element)iterator.next();
     ProductClass product=new ProductClass(element.getAttributeValue("id"),
                                     element.getAttributeValue("className"));  
        products.add(product);
    }while(iterator.hasNext());
    return (ProductClass[])products.toArray(new ProductClass[0]);

   }

   public ProductClass[] getAllClass2ById(String class1Id){
    Collection products=new ArrayList();
    Element classElement=null;
    Iterator iterator=this.dom.getRootElement().getChildren().iterator();
    do{
     Element element=(Element)iterator.next();
     if(class1Id.equalsIgnoreCase(element.getAttributeValue("id"))){
      classElement=element;
      break;
     }
    }while(iterator.hasNext());

    if(classElement!=null){
     Iterator iter=classElement.getChildren().iterator();
     do{
      Element element=(Element)iter.next();
      ProductClass product=new ProductClass(element.getAttributeValue("id"),
                                      element.getAttributeValue("className"));
               products.add(product);
     }while(iter.hasNext());

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表