public
class
WmsConfigure
...
{
private String fileName = "configure.xml";
//name指XML中需查找的节点名,index指如果XML里有多个以name为名的节点,需查找节点的位置,attribute指需查找的属性名
/**//** Creates a new instance of WmsConfigure */
public WmsConfigure() ...{
}
public WmsConfigure(String fileName) ...{
this.fileName = fileName;
}
public String getValue(String name,String attribute)...{
return this.getValue(name,0,attribute);
}
public boolean setValue(String name,String attribute,String value)...{
return this.setValue(name,0,attribute,value);
}
//获取fileName文件中,第index个name元素的attribute属性的值
public String getValue(String name,int index,String attribute)...{
try ...{
DocumentBuilderFactory docBF=DocumentBuilderFactory.newInstance();
DocumentBuilder docB = docBF.newDocumentBuilder();
Document doc=docB.parse(new File(fileName));
String value=((Element)doc.getElementsByTagName(name).item(index)).getAttribute(attribute);
if(value.equals(""))
return null;
return value;
} catch (ParserConfigurationException ex) ...{
ex.printStackTrace();
} catch (SAXException ex) ...{
ex.printStackTrace();
} catch (IOException ex) ...{
ex.printStackTrace();
}
JOptionPane.showMessageDialog(JWmsMainFrame.thisFrame,"获取"+name+"发生异常");
return null;
}
//设置fileName文件中,第index个name元素的attribute属性的值为value
public boolean setValue(String name,int index,String attribute,String value)...{
Document doc=null;
//将值写入Document
try ...{
DocumentBuilderFactory docBF=DocumentBuilderFactory.newInstance();
DocumentBuilder docB = docBF.newDocumentBuilder();
doc=docB.parse(new File(fileName));
((Element)doc.getElementsByTagName(name).item(index)).setAttribute(attribute,value);
}catch (ParserConfigurationException ex) ...{
ex.printStackTrace();
JOptionPane.showMessageDialog(JWmsMainFrame.thisFrame,"设置"+name+"发生异常");
}catch (SAXException ex) ...{
ex.printStackTrace();
JOptionPane.showMessageDialog(JWmsMainFrame.thisFrame,"设置"+name+"发生异常");
} catch (IOException ex) ...{
ex.printStackTrace();
JOptionPane.showMessageDialog(JWmsMainFrame.thisFrame,"设置"+name+"发生异常");
}
//将修改后的Document写回到XML文件
try ...{
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf=null;
tf = tff.newTransformer();
tf.transform(new DOMSource(doc),new StreamResult(new File(fileName)));
return true;
}catch (TransformerConfigurationException ex) ...{
ex.printStackTrace();
JOptionPane.showMessageDialog(JWmsMainFrame.thisFrame,"设置"+name+"发生异常");
}catch (TransformerException ex) ...{
ex.printStackTrace();
JOptionPane.showMessageDialog(JWmsMainFrame.thisFrame,"设置"+name+"发生异常");
}
return false;
}
}
private String fileName = "configure.xml";
//name指XML中需查找的节点名,index指如果XML里有多个以name为名的节点,需查找节点的位置,attribute指需查找的属性名
/**//** Creates a new instance of WmsConfigure */
public WmsConfigure() ...{
}
public WmsConfigure(String fileName) ...{
this.fileName = fileName;
}
public String getValue(String name,String attribute)...{
return this.getValue(name,0,attribute);
}
public boolean setValue(String name,String attribute,String value)...{
return this.setValue(name,0,attribute,value);
}
//获取fileName文件中,第index个name元素的attribute属性的值
public String getValue(String name,int index,String attribute)...{
try ...{
DocumentBuilderFactory docBF=DocumentBuilderFactory.newInstance();
DocumentBuilder docB = docBF.newDocumentBuilder();
Document doc=docB.parse(new File(fileName));
String value=((Element)doc.getElementsByTagName(name).item(index)).getAttribute(attribute);
if(value.equals(""))
return null;
return value;
} catch (ParserConfigurationException ex) ...{
ex.printStackTrace();
} catch (SAXException ex) ...{
ex.printStackTrace();
} catch (IOException ex) ...{
ex.printStackTrace();
}
JOptionPane.showMessageDialog(JWmsMainFrame.thisFrame,"获取"+name+"发生异常");
return null;
}
//设置fileName文件中,第index个name元素的attribute属性的值为value
public boolean setValue(String name,int index,String attribute,String value)...{
Document doc=null;
//将值写入Document
try ...{
DocumentBuilderFactory docBF=DocumentBuilderFactory.newInstance();
DocumentBuilder docB = docBF.newDocumentBuilder();
doc=docB.parse(new File(fileName));
((Element)doc.getElementsByTagName(name).item(index)).setAttribute(attribute,value);
}catch (ParserConfigurationException ex) ...{
ex.printStackTrace();
JOptionPane.showMessageDialog(JWmsMainFrame.thisFrame,"设置"+name+"发生异常");
}catch (SAXException ex) ...{
ex.printStackTrace();
JOptionPane.showMessageDialog(JWmsMainFrame.thisFrame,"设置"+name+"发生异常");
} catch (IOException ex) ...{
ex.printStackTrace();
JOptionPane.showMessageDialog(JWmsMainFrame.thisFrame,"设置"+name+"发生异常");
}
//将修改后的Document写回到XML文件
try ...{
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf=null;
tf = tff.newTransformer();
tf.transform(new DOMSource(doc),new StreamResult(new File(fileName)));
return true;
}catch (TransformerConfigurationException ex) ...{
ex.printStackTrace();
JOptionPane.showMessageDialog(JWmsMainFrame.thisFrame,"设置"+name+"发生异常");
}catch (TransformerException ex) ...{
ex.printStackTrace();
JOptionPane.showMessageDialog(JWmsMainFrame.thisFrame,"设置"+name+"发生异常");
}
return false;
}
}