java 操作XML文件(片段)
//create a new Document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.newDocument();
//add root Node
Element noteuser = d.createElement("note-users");
d.appendChild(noteuser);
//add exists Node
Document readXML = db.parse(new File("c:\\note-user.xml"));
NodeList nl = readXML.getElementsByTagName("user");
for(int i = 0; i < nl.getLength(); i++)
{
Element n = d.createElement("user");
n.setAttribute("name", nl.item(i).getAttributes().getNamedItem("name").getNodeValue());
if(id !=null && id.equals(nl.item(i).getAttributes().getNamedItem("name").getNodeValue()))
{
//duplicate id (redirect to error page)
response.getWriter().print("<font color = red>duplicate id !</font>");
response.getWriter().print(" ");
response.getWriter().print("<a href = \"reg.jsp\">return to login page</a>");
return;
}
n.setAttribute("password", nl.item(i).getAttributes().getNamedItem("password").getNodeValue());
noteuser.appendChild(n);
}
//add regist Node
Element n = d.createElement("user");
n.setAttribute("name", id);
n.setAttribute("password", pwd);
noteuser.appendChild(n);
//save data to file
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(d);
//file's path
StreamResult result = new StreamResult(new File("c:\\note-user.xml"));
transformer.transform(source, resu
相关文档:
package test
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
public class ReadSelectedLine{
// 读取文件指定行。
static void readAppointedLineNumber(File sourceFile, int lineNumber)
throws IOException {
......
在类中定义的方法在返回值前加上static修饰符就可以在main方法中调用了。如果不用static那就需要在main方法中创建对象,使用对象来调用对象的方法。
public class Test{
public static void main(String[] args){
Test t=new Test();
int b=1;
int c=2;
int[] ......
1 JAVA的反射,其实就是通过一个实例化的对象反过来去找到一个类的完整信息,比如对于如下的形式:
X x=new X();
x.getClass().getName();
这里就会输出这个类所在的完整信息,即"包名.类名";
最常用的三种实例化CLASS类对象
Class<?> c1 = null ; // 指定泛型
C ......
A flexible layout configurable with pattern string.
The goal of this class is to format
a LoggingEvent
and return the results as a String. The results depend on the conversion
pattern
.
The conversion pattern is closely related to the conversion pattern of the
printf function in C ......