import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import **.data.Server;
public class XMLfileReader
{
public Document XMLParse(String xmlFilePath)
{
DocumentBuilderFactory domFac = DocumentBuilderFactory.newInstance();
Document doc = null;
try {
DocumentBuilder dombuilder = domFac.newDocumentBuilder();
FileInputStream in = new FileInputStream(xmlFilePath);
doc = dombuilder.parse(in);
} catch (Exception e) {
e.printStackTrace();
}
return doc;
}
public ArrayList XMLreader(Document doc)
{
ArrayList serverList = new ArrayList<Server>();
try {
NodeList nl=doc.getElementsByTagName("server");
Server server = null;
int len=nl.getLength();
for(int i=0;i<len;i++){
//System.err.println("***server"+i+"*****");
Element serverNode=(Element)nl.item(i);
Node nameNode=serverNode.getElementsByTagName("name").item(0);
Node ipNode=serverNode.getElementsByTagName("ip").item(0);
Node pathNode=serverNode.getElementsByTagName("path").item(0);
Node detailNode=serverNode.getElementsByTagName("detail").item(0);
Node isPassNode=serverNode.getElementsByTagName("isPass").item(0);
String nameValue=nameNode.getFirstChild().getNodeValue();
String ipValue=ipNode.getFirstChild().getNodeValue();
String pathValue=pathNode.getFirstChild().getNodeValue();
String detailValue=detailNode.getFirstChild().getNodeValue();
Boolean isPassValue=isPassNode.getFirstChild().getNodeValue().equals("true") ? true : false;
//xml2javaObj
server = new Server();
server.setServerName(nameValue);
server.setIp(ipValue);
server.setExportPath(pathValue);
server.setPass(isPassValue);
server.setServerDatail(detailValue);
serverList.add(server);
System.out.println("name:"+nameValue);
System.out.println("ip:"+ipValue);
}
}catch (Exception e) {
}
return serverList;
}
public static void main(String[] args){
XMLfileReader xmlReader = new XMLfileReader();
Document newDom=xmlReader.XMLParse("C:/ServerxmlTest.xml");
System.out.println("-----display------");
xmlReader.XMLreader(newDom);
// xmlReader.add(newdoc);
// System.out.println("-------add to last--------");
// xmlReader.XMLreader(newdoc);
// xmlReader.delete(newdoc, 0);
// System.out.println("-----delete the first node-------");
// xmlReader.XMLreader(newdoc);
// xmlReader.change(newdoc,0,new String("name"),new String("zhushudong2"));
// System.out.println("-----change the first node-------");
// xmlReader.XMLreader(newdoc);
// try {
// xmlReader.ObjectToXML(newdoc);
// } catch (Exception e) {}
}
/**
*为Document对象里添加内容
*@param doc Document类型对象
*/
public void add(Document doc){
try {
Element customer=doc.createElement("customer");
Element id=doc.createElement("id");
Element name=doc.createElement("name");
Element address=doc.createElement("address");
Text textId=doc.createTextNode("005");
Text textName=doc.createTextNode("zhushudong");
Text textAddress=doc.createTextNode("haerbin");
id.appendChild(textId);
name.appendChild(textName);
address.appendChild(textAddress);
customer.appendChild(id);
customer.appendChild(name);
customer.appendChild(address);
Element root=doc.getDocumentElement();
root.appendChild(customer);
} catch (Exception e) {}
}
/**
*删除结点
*@param doc Document对象
*@param i 用于指示要删除结点位置
*/
public void delete(Document doc,int i){
NodeList nl=doc.getElementsByTagName("customer");
Node nodeDel=nl.item(i);
nodeDel.getParentNode().removeChild(nodeDel);
}
/**
*改变节点的值
*@param doc 传入的对象
*@param i 用于指示要修改结点位置
*@param attr 要修改节点的标记
*@param newValue 修改后的值
*/
public void change(Document doc,int i,String attr,String newValue){
NodeList nl=doc.getElementsByTagName("customer");
Element cha=(Element)nl.item(i);
Node nodeForCha=cha.getElementsByTagName(attr).item(0);
nodeForCha.getFirstChild().setNodeValue(newValue);
}
/**
*将java对象转化成xml文件
*@param doc 要转化的java对象
*/
public void ObjectToXML(Document doc)throws TransformerConfigurationException{
try {
//利用文档节点创建一个DOM输入源
DOMSource source=new DOMSource(doc);
//以newXMLfile.xml创建一个StreamResult对象
StreamResult result=new StreamResult(new File("newXMLfile.xml"));
//创建转换器工厂对象
TransformerFactory tff=TransformerFactory.newInstance();
//创建转换器对象
Transformer tf=tff.newTransformer();
//进行转换
tf.transform(source, result);
} catch (Exception e) {}
}
}