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ѧϰ´ÓÈëÃŵ½¾«Í¨
Ò»¡¢ JDK (Java Development Kit)
JDKÊÇÕû¸öJavaµÄºËÐÄ£¬°üÀ¨ÁËJavaÔËÐл·¾³£¨Java Runtime Envirnment£©£¬Ò»¶ÑJava¹¤¾ßºÍJava»ù´¡µÄÀà¿â(rt.jar)¡£²»ÂÛʲôJavaÓ¦Ó÷þÎñÆ÷ʵÖʶ¼ÊÇÄÚÖÃÁËij¸ö°æ±¾µÄJDK¡£Òò´ËÕÆÎÕJDKÊÇѧºÃJavaµÄµÚÒ»²½¡£×îÖ÷Á÷µÄJ ......
javaÖеÄÐÞÊηû
static ±íʾ¾²Ì¬£¬Ëü¿ÉÒÔÐÞÊÎÊôÐÔ£¬·½·¨ºÍ´úÂë¿é¡£
1£¬staticÐÞÊÎÊôÐÔ£¨Àà±äÁ¿£©£¬ÄÇôÕâ¸öÊôÐԾͿÉÒÔÓà ÀàÃû.ÊôÐÔÃû À´·ÃÎÊ£¬Ò²¾ÍÊÇʹÕâ¸öÊôÐÔ³ÉΪ±¾ÀàµÄÀà±äÁ¿£¬Îª±¾Àà¶ÔÏóËù¹²ÓС£Õâ¸öÊôÐÔ¾ÍÊÇÈ«À๫ÓС££¨¹²ÓеÄÀà±äÁ¿Óë¶ÔÏóÎ޹أ¬Ö»ºÍÀàÓйأ©¡£
Àà¼ÓÔصĹý³Ì£¬Àà±¾ÉíÒ²ÊDZ£´æÔÚÎļþÖУ¨×Ö½ÚÂëÎļþ± ......
package book.xml;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.Default ......
¶¨Òå:¶¨ÒåÁËËã·¨×壬·Ö±ð·â×°ÆðÀ´£¬ÈÃËüÃÇÖ®¼ä¿ÉÒÔ»¥ÏàÌæ»»£¬´ËģʽÈÃËã·¨µÄ±ä»¯¶ÀÁ¢ÓÚʹÓÃËã·¨µÄ¿Í»§¡£
ÊÊÓÃÐÔ
Ðí¶àÏà¹ØµÄÀà½ö½öÊÇÐÐΪÓÐÒì¡£“²ßÂÔ”ÌṩÁËÒ»ÖÖÓöà¸öÐÐΪÖеÄÒ»¸öÐÐΪÀ´ÅäÖÃÒ»¸öÀàµÄËã·¨¡£
ÐèҪʹÓÃÒ»¸öËã·¨µÄ²»Í¬±äÌå¡£ÀýÈ磬Äã¿ÉÄܻᶨÒåһЩ·´Ó³²»ÈݵĿռä/ʱ¼äȨºâµÄËã·¨¡£µ±ÕâЩ±äÌåʵ ......