mgm娱乐场:AJAX级联下拉框的简单实现案例

需要的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; 

需要的JAVA类

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]); 

复制代码 代码如下:

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 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()); 

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]);

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()); 
return (ProductClass[])products.toArray(new ProductClass[0]); 

else…{ 
return null; 

   }

public ProductClass[] getAllClass3ById(String class1Id,String
class2Id) …{ 
Collection products = new ArrayList(); 
Element class1Element = null; 
Element class2Element = null; 

   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());

Iterator iterator =
this.dom.getRootElement().getChildren().iterator(); 
do …{ 
Element element = (Element)iterator.next(); 
if(class1Id.equalsIgnoreCase(element.getAttributeValue(“id”))) …{ 
class1Element = 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());
    return (ProductClass[])products.toArray(new ProductClass[0]);
    }
    else{
     return null;
    }
   }

if(class1Element!=null) …{ 
Iterator iter = class1Element.getChildren().iterator(); 
do …{ 
Element element = (Element)iter.next(); 
if(class2Id.equalsIgnoreCase(element.getAttributeValue(“id”))) …{ 
class2Element = element; 
break; 

}while(iter.hasNext()); 

 
   public ProductClass[] getAllClass3ById(String class1Id,String
class2Id) {
  Collection products = new ArrayList();
  Element class1Element = null;
  Element class2Element = null;

if(class2Element!=null) …{ 
Iterator iter2 = class2Element.getChildren().iterator(); 
do …{ 
Element element = (Element)iter2.next(); 
ProductClass product = new
ProductClass(element.getAttributeValue(“id”),element.getAttributeValue(“className”)); 
products.add(product); 
}while(iter2.hasNext()); 

return (ProductClass[])products.toArray(new ProductClass[0]); 

else return null; 

  Iterator iterator =
this.dom.getRootElement().getChildren().iterator();
  do {
   Element element = (Element)iterator.next();
   if(class1Id.equalsIgnoreCase(element.getAttributeValue(“id”))) {
    class1Element = element;
    break;
   }
  }while(iterator.hasNext());

<?xml version=”1.0″ encoding=”UTF-8″?> 
<!DOCTYPE class SYSTEM “product.dtd” > 
<class> 
<class1 className=”电脑配件” id=”1″> 
<class2 className=”内存” id=”1″> 
<class3 id=”1″ className=”kingmax”></class3> 
<class3 id=”2″ className=”kingston”></class3> 
<class3 id=”3″ className=”samsung”></class3> 
<class3 id=”4″ className=”hydadi”></class3> 
<class3 id=”5″ className=”ibm”></class3> 
</class2> 
<class2 className=”硬盘” id=”2″> 
<class3 id=”6″ className=”hithait”></class3> 
<class3 id=”7″ className=”IBM”></class3> 
<class3 id=”8″ className=”samsung”></class3> 
<class3 id=”9″ className=”westdata”></class3> 
</class2> 
</class1> 

  if(class1Element!=null) {
   Iterator iter = class1Element.getChildren().iterator();
   do {
    Element element = (Element)iter.next();
    if(class2Id.equalsIgnoreCase(element.getAttributeValue(“id”))) {
     class2Element = element;
     break;
    }
   }while(iter.hasNext());

<class1 className=”食品配件” id=”2″> 
<class2 className=”汉堡包” id=”1″> 
<class3 id=”1″ className=”麦当劳”></class3> 
<class3 id=”2″ className=”肯得基”></class3> 
<class3 id=”3″ className=”罗杰丝”></class3> 
</class2> 
<class2 className=”饮料” id=”2″> 
<class3 id=”4″ className=”cocacola”></class3> 
<class3 id=”5″ className=”sprite”></class3> 
<class3 id=”6″ className=”coffee”></class3> 
<class3 id=”7″ className=”water”></class3> 
</class2> 
</class1> 
</class> 

   if(class2Element!=null) {
    Iterator iter2 = class2Element.getChildren().iterator();
    do {
     Element element = (Element)iter2.next();
     ProductClass product = new
ProductClass(element.getAttributeValue(“id”),element.getAttributeValue(“className”));
     products.add(product);
    }while(iter2.hasNext());
   }
   return (ProductClass[])products.toArray(new ProductClass[0]);
  }
  else return null;
}
}

相关文章