有一处错误谁帮忙改下吧
程序代码:
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="org.w3c.dom.*"%>
<%@ page import="javax.xml.parsers.*"%>
<%@ page import="javax.xml.transform.*" %>
<%@ page import="javax.xml.transform.stream.*"%>
<%@ page import="javax.xml.transform.dom.*"%>
<%@ page import="javax.xml.transform.OutputKeys.*"%>
<HTML>
<HEAD><TITLE>删除已受理订单</TITLE></HEAD>
<BODY>
<%
int i=0;
//建立一个解析器工厂,以获得一个具体的解析器对象:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//使用newDocumentBuilder()方法可以获得一个DocumentBuilder对象
DocumentBuilder db = dbf.newDocumentBuilder();
//打开现有的xml文档
Document doc = db.parse(getServletConfig().getServletContext().getRealPath ("book0.xml"));
try{
Element root = doc.getDocumentElement();//根节点
for(i=0;i<root.getChildNodes();i++)
root.removeChild(root.getElemntsByTagName("book").item(i));
String fileName = "book0.xml";
String outFile = getServletConfig().getServletContext().getRealPath(fileName);
//定义转换器
Transformer tf = javax.xml.transform.TransformerFactory.newInstance().newTransformer();
//防止汉字乱码
tf.setOutputProperty(OutputKeys.ENCODING,"gb2312");
//设定XSLT引擎在输出XML文档时,是否自动添加额外的空格
tf.setOutputProperty(OutputKeys.INDENT,"yes");
//将XML文档树doc转换输出到文件
tf.transform(new DOMSource(doc),new StreamResult(outFile));
%>
已受理订单已经删除<br>
<a href=<%=fileName%>>点击这里打开产生的XML文档<%=fileName%> </a>
<%
}
catch (Exception e)
{
System.out.println (e.getMessage());
}
%>
</BODY>
</HTML>
for(i=0;i<root.getChildNodes();i++)<%@ page language="java" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="org.w3c.dom.*"%>
<%@ page import="javax.xml.parsers.*"%>
<%@ page import="javax.xml.transform.*" %>
<%@ page import="javax.xml.transform.stream.*"%>
<%@ page import="javax.xml.transform.dom.*"%>
<%@ page import="javax.xml.transform.OutputKeys.*"%>
<HTML>
<HEAD><TITLE>删除已受理订单</TITLE></HEAD>
<BODY>
<%
int i=0;
//建立一个解析器工厂,以获得一个具体的解析器对象:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//使用newDocumentBuilder()方法可以获得一个DocumentBuilder对象
DocumentBuilder db = dbf.newDocumentBuilder();
//打开现有的xml文档
Document doc = db.parse(getServletConfig().getServletContext().getRealPath ("book0.xml"));
try{
Element root = doc.getDocumentElement();//根节点
for(i=0;i<root.getChildNodes();i++)
root.removeChild(root.getElemntsByTagName("book").item(i));
String fileName = "book0.xml";
String outFile = getServletConfig().getServletContext().getRealPath(fileName);
//定义转换器
Transformer tf = javax.xml.transform.TransformerFactory.newInstance().newTransformer();
//防止汉字乱码
tf.setOutputProperty(OutputKeys.ENCODING,"gb2312");
//设定XSLT引擎在输出XML文档时,是否自动添加额外的空格
tf.setOutputProperty(OutputKeys.INDENT,"yes");
//将XML文档树doc转换输出到文件
tf.transform(new DOMSource(doc),new StreamResult(outFile));
%>
已受理订单已经删除<br>
<a href=<%=fileName%>>点击这里打开产生的XML文档<%=fileName%> </a>
<%
}
catch (Exception e)
{
System.out.println (e.getMessage());
}
%>
</BODY>
</HTML>
root.removeChild(root.getElemntsByTagName("book").item(i));
这句错误了
程序代码:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 24 in the jsp file: /del_book.jsp
The operator < is undefined for the argument type(s) int, NodeList
21: Document doc = db.parse(getServletConfig().getServletContext().getRealPath ("book0.xml"));
22: try{
23: Element root = doc.getDocumentElement();//根节点
24: for(i=0;i<root.getChildNodes();i++)
25: root.removeChild(root.getElemntsByTagName("book").item(i));
26: String fileName = "book0.xml";
27: String outFile = getServletConfig().getServletContext().getRealPath(fileName);
An error occurred at line: 25 in the jsp file: /del_book.jsp
The method getElemntsByTagName(String) is undefined for the type Element
22: try{
23: Element root = doc.getDocumentElement();//根节点
24: for(i=0;i<root.getChildNodes();i++)
25: root.removeChild(root.getElemntsByTagName("book").item(i));
26: String fileName = "book0.xml";
27: String outFile = getServletConfig().getServletContext().getRealPath(fileName);
28: //定义转换器
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:312)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:299)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:589)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
An error occurred at line: 24 in the jsp file: /del_book.jsp
The operator < is undefined for the argument type(s) int, NodeList
21: Document doc = db.parse(getServletConfig().getServletContext().getRealPath ("book0.xml"));
22: try{
23: Element root = doc.getDocumentElement();//根节点
24: for(i=0;i<root.getChildNodes();i++)
25: root.removeChild(root.getElemntsByTagName("book").item(i));
26: String fileName = "book0.xml";
27: String outFile = getServletConfig().getServletContext().getRealPath(fileName);
An error occurred at line: 25 in the jsp file: /del_book.jsp
The method getElemntsByTagName(String) is undefined for the type Element
22: try{
23: Element root = doc.getDocumentElement();//根节点
24: for(i=0;i<root.getChildNodes();i++)
25: root.removeChild(root.getElemntsByTagName("book").item(i));
26: String fileName = "book0.xml";
27: String outFile = getServletConfig().getServletContext().getRealPath(fileName);
28: //定义转换器
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:312)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:299)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:589)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
搜索更多相关主题的帖子:
html java
----------------解决方案--------------------------------------------------------
删除节点最好使用从后向前的方式删除,
for(i=root.getChildNodes()-1;i>=0;i--)
root.removeChild(root.getElemntsByTagName("book").item(i));
----------------解决方案--------------------------------------------------------
回复 2楼 rareliu
结果一样,都报错 ----------------解决方案--------------------------------------------------------
回复 2楼 rareliu
问题解决了,还是谢谢你那两句改成这样就可以了、getLength方法的NodeList居然要用book的(也就是要删的子节点);之前一直以为是要用他们的父节点也就是books
int i=0;
Element root = doc.getDocumentElement();
NodeList nodes = doc.getElementsByTagName("book");
for(i=nodes.getLength()-1;i>=0 ;i--){%>
<%=i%>
<%
root.removeChild(root.getElementsByTagName("book").item(i));
}
----------------解决方案--------------------------------------------------------