XMLClass.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
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.XMLOutputter;
public class XMLClass
{
XMLClass()
{
}
public String[] getIPbyArray(String strPath)
{
//strPath = "a.xml";
FileInputStream fi = null;
try
{
fi = new FileInputStream(strPath);
}
catch (IOException e)
{
// do nothing
}
SAXBuilder saxBuiler = new SAXBuilder();
Document doc = null;
try
{
doc = saxBuiler.build(fi);
}
catch (JDOMException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Element root = doc.getRootElement();
List leaves = root.getChildren();
//String[] arrayIP = {"10.40.109.1" ,"10.40.109.1","10.40.109.1","10.40.109.1","10.40.109.1"};
String[] arrayIP =new String[5];
int numIP = leaves.size();
for(int i= 0; i< numIP; i++)
{
Element child = (Element)leaves.get(i);
arrayIP[i] = child.getAttributeValue("id");
System.out.println(arrayIP[i] );
}
return arrayIP;
}
public ArrayList getIPbyArrayList(String strPath)
{
//strPath = "a.xml";
FileInputStream fi = null;
try
{
fi = new FileInputStream(strPath);
}
catch (IOException e)
{
// do nothing
}
SAXBuilder saxBuiler = new SAXBuilder();
Document doc = null;
try
{
doc = saxBuiler.build(fi);
}
catch (JDOMException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Element root = doc.getRootElement();
List leaves = root.getChildren();
//String[] arrayIP = {"10.40.109.1" ,"10.40.109.1","10.40.109.1","10.40.109.1","10.40.109.1"};
ArrayList ipList =new ArrayList();
int numIP = leaves.size();
for(int i= 0; i< numIP; i++)
{
Element child = (Element)leaves.get(i);
ipList.add(child.getAttributeValue("id"));
System.out.println(child.getAttributeValue("id"));
}
return ipList;
}
public void writeXml(String[] strArray , String strPath)
{
XMLOutputter xmlOut = new XMLOutputter();
FileOutputStream fileOutputStream = null;
Element root = new Element("list");
// 根节点添加到文档中;
Document doc = new Document(root);
// 此处 for 循环可替换成 遍历 数据库表的结果集操作;
int size = strArray.length;
for (int i = 0; i < size; i++)
{
// 创建节点 user;
Element elements = new Element("user");
// 给 user 节点添加属性 id;
elements.setAttribute("id", ""+strArray[i]);
// 给 user 节点添加子节点并赋值;
// elements.addContent(new Element("IP"+i).setText("xuehui"));
// elements.addContent(new Element("age").setText("28"));
// elements.addContent(new Element("sex").setText("Male"));
// 给父节点list添加user子节点;
root.addContent(elements);
}
try
{
fileOutputStream = new FileOutputStream(strPath);
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//OutputStream fileOutputStream;
try
{
xmlOut.output(doc, fileOutputStream);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void writeXml(ArrayList ipList , String strPath)
{
XMLOutputter xmlOut = new XMLOutputter();
FileOutputStream fileOutputStream = null;
Element root = new Element("list");
// 根节点添加到文档中;
Document doc = new Document(root);
// 此处 for 循环可替换成 遍历 数据库表的结果集操作;
int size = ipList.size();
for (int i = 0; i < size; i++)
{
// 创建节点 user;
Element elements = new Element("user");
// 给 user 节点添加属性 id;
elements.setAttribute("id", ""+ipList.get(i));
// 给 user 节点添加子节点并赋值;
// elements.addContent(new Element("IP"+i).setText("xuehui"));
// elements.addContent(new Element("age").setText("28"));
// elements.addContent(new Element("sex").setText("Male"));
// 给父节点list添加user子节点;
root.addContent(elements);
}
try
{
fileOutputStream = new FileOutputStream(strPath);
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//OutputStream fileOutputStream;
try
{
xmlOut.output(doc, fileOutputStream);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args)
{
String[] arrayIP = {"10.40.109.1" ,"10.40.109.1","10.40.109.1","10.40.109.1","10.40.109.1"};
try
{
XMLClass xmlClass = new XMLClass();
System.out.println("生成 mxl 文件...");
xmlClass.writeXml(arrayIP,"user.xml");
xmlClass.getIPbyArrayList("user.xml");
System.out.println("生成 mxl 文件end...");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
/*************************显示界面MainFrame.java***********************************************
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class MainFrame extends JFrame
{
MainFrame()
{
JFrame frame = new JFrame("测试界面");
Container container = frame.getContentPane();
JLabel lable = new JLabel("IP地址");
lable.setHorizontalAlignment(SwingConstants.CENTER);
final JComboBox comboxIPforWrite = new JComboBox();
// JComboBox comboxIPforWrite = new JComboBox();
JComboBox comboxIPforShow= new JComboBox();
comboxIPforWrite.setEditable(true);
JButton button = new JButton();
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
int numItem = comboxIPforWrite.getItemCount();
ArrayList listIP = new ArrayList();
for(int i =0 ;i<numItem; i++)
{
listIP.add((String) comboxIPforWrite.getItemAt(i));
//listIP
XMLClass xmlClass = new XMLClass();
xmlClass.writeXml(listIP, "user.xml");
}
}
});
//for()
/*
* 窗体放置的控件内容
*/
JPanel panelMain = new JPanel();
panelMain.setLayout(new BorderLayout());
JPanel panelIP = new JPanel();
panelIP.setLayout(new GridBagLayout());
panelIP.add(lable, new GridBagConstraints(0,0,1,1,100,100,GridBagConstraints.CENTER,GridBagConstraints.BOTH , new Insets(0, 0, 0 ,0),0, 0 ));
panelIP.add(comboxIPforWrite, new GridBagConstraints(1,0,1,1,100,100,GridBagConstraints.CENTER,GridBagConstraints.BOTH , new Insets(0, 0, 0 ,0),0, 0 ));
panelMain.add(panelIP ,BorderLayout.NORTH);
//panelMain.setSize(new Dimension(61,39));
comboxIPforShow = beforeShow(comboxIPforShow);
/*
* 窗体放置的控件内容
*/
container.add(panelMain);
frame.setVisible(true);
frame.setSize(618,382);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private JComboBox beforeShow(JComboBox comboxIPforWrite)
{
int numItem = comboxIPforWrite.getItemCount();
ArrayList listIP = new ArrayList();
XMLClass xmlClass = new XMLClass();
listIP = xmlClass.getIPbyArrayList("user.xml");
for(int i =0 ;i<numItem; i++)
{
//listIP.add((String) comboxIPforWrite.getItemAt(i));
//listIP
comboxIPforWrite.addItem(listIP.get(i));
}
return comboxIPforWrite;
}
public static void main (String[] args)
{
MainFrame mainFrame = new MainFrame();
}
}