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
相关文档:
Java NIO API详解
在JDK
1.4以前,Java的IO操作集中在java.io这个包中,是基于流的阻塞(blocking)API。对于大多数应用来说,这样的API使用很方
便,然而,一些对性能要求较高的应用,尤其是服务端应用,往往需要一个更为有效的方式来处理IO。从JDK 1.4起,NIO
API作为一个基于缓冲区,并能提供非阻塞(non-blo ......
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 {
......
一.MsXml创建XML文档示例
// XmlCreationDemo.cpp
#include <stdlib.h>
#include <stdio.h>
// 引入MSXML解析器
#import <msxml4.dll>
using namespace MSXML2;
class InitializeCom
{
public:
InitializeCom() { CoInitialize(NULL); // Initializes the COM library }
~Initializ ......
1。数据库
在创建的时候,指定其编码为UTF-8.
(1)oracle:
SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
---------------------------------------------
SIMPLIFIED CHINESE_CHINA.UTF8
SQL>
jdbc url无须指定编码。
& ......
private static boolean isValidDate(String strValue ) {//20091001字符串
int d = Integer.parseInt(strValue.substring(6, 8));
int m = Integer.parseInt(strValue.substring(4, 6));
int y = Integer.parseInt(strValue.subst ......