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[] getIPArray(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("IP");
System.out.println(arrayIP[i] );
}
return arrayIP;
}
public ArrayList getIPArrayList(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-1; i++)
{
// 创建节点 user;
Element elements = new Element("Record");
// 给 user 节点添加属性 id;
elements.setAttribute("IP", ""+strArray[i][0]);
elements.setAttribute("LastLoginTime", ""+strArray[i][1]);
// 给 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" ,"1101071921"},
// {"10.40.109.2","1101071922"},
// {"10.40.109.3","1101071922"},
// { "10.40.109.4","1101071923"},
// {"10.40.109.5","1101071924"}
// };
// try
// {
// XMLClass xmlClass = new XMLClass();
// System.out.println("生成 mxl 文件...");
// xmlClass.writeXml(arrayIP,"user.xml");
// xmlClass.getIPArrayList("user.xml");
// System.out.println("生成 mxl 文件end...");
// }
// catch (Exception e)
// {
// e.printStackTrace();
// }
try
{
String strPath = "user.xml";
XMLClass xmlClass = new XMLClass();
System.out.println("生成 mxl 文件...");
xmlClass.getIPArray(strPath);
xmlClass.getIPArrayList("user.xml");
System.out.println("生成 mxl 文件end...");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}