📄 xmlcreatedom.jsp
字号:
<%@page contentType="text/html"%>
<%@page pageEncoding="Ms950"%>
<%@page import="javax.xml.parsers.*" %>
<%@page import="org.w3c.dom.*" %>
<%@page import="javax.xml.transform.*" %>
<%@page import="javax.xml.transform.stream.*" %>
<%@page import="javax.xml.transform.dom.*" %>
<%@page import="java.io.*" %>
<html>
<head><title>建立 XML</title></head>
<body>
<%
DocumentBuilderFactory theDocumentBuilderFactory=
DocumentBuilderFactory.newInstance() ;
DocumentBuilder theDocumentBuilder =
theDocumentBuilderFactory.newDocumentBuilder() ;
DOMImplementation theDOMImplementation =
theDocumentBuilder.getDOMImplementation() ;
Document theDocument = theDOMImplementation.
createDocument(null,"customers",null) ;
Element theElement = theDocument.getDocumentElement() ;
Element theElementcustomer = theDocument.
createElementNS(null,"customer") ;
Element theElementcustomerid =theDocument.
createElementNS(null,"customerid") ;
Element theElementcustomername =theDocument.
createElementNS(null,"customername") ;
Element theElementtel =theDocument.
createElementNS(null,"tel") ;
Element theElementemail
=theDocument.createElementNS(null,"email") ;
Node theNodeCustomerid =
theDocument.createTextNode("1001") ;
Node theNodeCustomername =
theDocument.createTextNode("管正") ;
Node theNodetel =
theDocument.createTextNode("025-84210020") ;
Node theNodeemail= theDocument.createTextNode
("gz@fj.com") ;
theElementcustomerid.appendChild(theNodeCustomerid) ;
theElementcustomername.appendChild(theNodeCustomername) ;
theElementtel.appendChild(theNodetel) ;
theElementemail.appendChild(theNodeemail) ;
theElementcustomer.appendChild(theElementcustomerid) ;
theElementcustomer.appendChild(theElementcustomername) ;
theElementcustomer.appendChild(theElementtel) ;
theElementcustomer.appendChild(theElementemail) ;
theElement.appendChild(theElementcustomer) ;
TransformerFactory tff= TransformerFactory.newInstance();
Transformer theTransformer=tff.newTransformer();
theTransformer.setOutputProperty
(OutputKeys.ENCODING,"GB2312") ;
DOMSource theDOMSource = new DOMSource(theDocument) ;
File theFile = new File(request.getRealPath
("/"+"ch12//xmlFile//newCustomers.xml")) ;
theFile.createNewFile() ;
StreamResult theStreamResult = new StreamResult(theFile) ;
theTransformer.transform(theDOMSource,theStreamResult) ;
out.println("XML文件建立完成!!") ;
%>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -