mysql省市县三级联动查询_三级联动查询全国省市区(xml与
数据库)
提供有l和china.sql⽂件,实现全国省市区的三级联动效果
⼀、xml实现
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
@SuppressWarnings("serial")
public class ChinaJFrame extends JFrame {
private JPanel contentPane;
private List cityList=null;
private List provinceList=null;
private List countyList=null;
@SuppressWarnings("rawtypes")
private JComboBox provinceComboBox, cityComboBox, countyComboBox;
SAXReader reader = new SAXReader();
Document document = null;
List list=null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChinaJFrame frame = new ChinaJFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*
* @throws DocumentException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public ChinaJFrame() throws DocumentException {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
provinceComboBox = new JComboBox();
provinceComboBox.setModel(new DefaultComboBoxModel(new String[] { "省份" })); provinceComboBox.setBounds(33, 106, 108, 21);
cityComboBox = new JComboBox();
cityComboBox.setBounds(171, 106, 108, 21);
cityComboBox.setModel(new DefaultComboBoxModel(new String[] { "地级市" })); countyComboBox = new JComboBox();
countyComboBox.setBounds(302, 106, 108, 21);
countyComboBox.setModel(new DefaultComboBoxModel(new String[] { "市、县级市" })); provinceList = getProvince("province");
for (String s : provinceList) {
provinceComboBox.addItem(s);
}
provinceComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
StateChange() == ItemEvent.SELECTED){
int ProvinceIndex = SelectedIndex();
cityList = getCity(ProvinceIndex);
dom4j读取xml
for (String s : cityList) {
cityComboBox.addItem(s);
}
}
}
});
cityComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
StateChange() == ItemEvent.SELECTED){
cityComboBox=(JComboBox) e.getSource();
String name2=(String) SelectedItem();
countyList=getCounty(name2);
for(String cl:countyList){
countyComboBox.addItem(cl);
}
}
}
});
JLabel lblNewLabel = new JLabel(
"\u5168\u56FD\u57CE\u5E02\u4E09\u7EA7\u8054\u52A8"); lblNewLabel.setFont(new Font("宋体", Font.BOLD, 18)); lblNewLabel.setBounds(140, 25, 160, 48);
contentPane.add(provinceComboBox);
contentPane.add(cityComboBox);
contentPane.add(countyComboBox);
contentPane.add(lblNewLabel);
}
@SuppressWarnings("unchecked")
public List getProvince(String name) {
list = new ArrayList();
try {
document = ad(new File("l"));
} catch (DocumentException e) {
e.printStackTrace();
}
Element root = RootElement();
for(Iterator i = root.elementIterator(name); i.hasNext();) {
Element node = i.next();
List attrs = node.attributes();
if (attrs != null) {
for (Attribute attr : attrs) {
list.Value());
}
}
}
return list;
}
@SuppressWarnings("unchecked")
public List getCity(int index) {
list = new ArrayList();
try {
document = ad(new File("l"));
} catch (DocumentException e) {
e.printStackTrace();
}
Element root = RootElement();
List elements = root.elements();
List elements1 = (index-1).elements();
for (int i=0; i < elements1.size(); i++) {
List attrs = (i).attributes();
if (attrs != null) {
for (Attribute attr : attrs) {
list.Value());
}
}
}
return list;
}
@SuppressWarnings("unchecked")
public List getCounty(String name2){
list = new ArrayList();
try {
document = ad(new File("l"));
} catch (DocumentException e) {
e.printStackTrace();
}
province[@name='北京市']
List nodes = document.selectNodes("//city[@name='" + name2 + "']//county//@name"); for (Node node : nodes) {
list.Text());
}
return list;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。