JAVA操纵XML
来源:互联网 发布:linux下查看ip 编辑:程序博客网 时间:2024/06/05 02:26
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;
}
}
- JAVA操纵XML
- Java 如何操纵 XML
- JAVA操纵XML 实例讲解
- ASP操纵XML类
- 操纵XML文件
- 操纵XML文件
- C# 操纵xml
- c#中操纵XML文件
- c#中操纵XML文件
- C# 操纵XML示例1
- C# 操纵XML示例2
- c#操纵xml文件类
- 用CMarkup类操纵XML
- 操纵java数组
- 操纵java数组
- 操纵java数组
- 操纵java数组[珍藏]
- 操纵java数组[转]
- 如何让jtable失去选中行状态
- Dbgrid使用技巧(下)
- java操纵TXT
- 在Flash中实现写文件功能
- 排序算法 笔记 有待补充
- JAVA操纵XML
- FAT文件系统原理
- C/C++数组名与指针区别深入探索,流传还挺广的
- Linux上好玩的命令
- 利用远程数据库账号,导数据库
- 完整的天气预报web服务源码
- PHP之父:最喜欢开源的四类人
- 一个黑客与一个电脑白痴的经典对白
- java.util包详解