xmlparserdomformat.jsp

来自「jsp动态网站开发与实例(第3版) 源码」· JSP 代码 · 共 100 行

JSP
100
字号
<%@page contentType="text/html"%>
<%@page pageEncoding="ms950"%>
<%@page import="javax.xml.parsers.*" %>
<%@page import="org.w3c.dom.*" %>
<html><head><title>格式化 XML</title></head>
<body>
<%
DocumentBuilderFactory theDocumentBuilderFactory=         
DocumentBuilderFactory.newInstance() ; 
DocumentBuilder theDocumentBuilder =         
theDocumentBuilderFactory.newDocumentBuilder() ; 
Document theDocument = theDocumentBuilder.parse        
(request.getRealPath("/"+"ch12//xmlFile//customers.xml")) ; 
Element theParentElement = theDocument.getDocumentElement() ;       
NodeList theNodeList =theParentElement.getElementsByTagName("customer");
out.println("<table border=1 width='100%'> ");
out.println("<th>编号</th><th>姓名</th>");
out.println("<th>电话</th><th>邮件</th>");
for(int i=0;i<theNodeList.getLength();i++)
{    
Element theChildElement = (Element)theNodeList.item(i) ;     
NodeList theChildNodeList  = null ;     
String outString ="" ;    
out.println("<tr><td>");     
theChildNodeList=theChildElement.getElementsByTagName("customerid");    
outString = (theChildNodeList.item(0)).getFirstChild().getNodeValue() ;    
out.println(outString +"<br>")  ;        
out.println("</td><td>");      
//    
theChildNodeList=theChildElement.getElementsByTagName("customername");    
outString = (theChildNodeList.item(0)).getFirstChild().getNodeValue() ;    
out.println(outString +"<br>")  ;           
out.println("</td><td>");       
//    
theChildNodeList=theChildElement.getElementsByTagName("tel");    
outString = (theChildNodeList.item(0)).getFirstChild().getNodeValue() ;    
out.println(outString +"<br>")  ;      
out.println("</td><td>");         
//   
theChildNodeList=theChildElement.getElementsByTagName("email");    
outString = (theChildNodeList.item  (0)).getFirstChild().getNodeValue() ;    
out.println(outString +"<br>")  ;                 
out.println("</td></tr>");     
}    
%>
</tr>
</table>
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?