当前位置: 技术问答>java相关
在JAVA中解析XML的问题,分必送
来源: 互联网 发布时间:2015-07-23
本文导语: JDK1.4以上版本。我写了一个lgm.java如下:编译时候出现如下错误: 其中e:\try\web-inf\classes\fxml.xml是正确的,请各位放心。 问题是Document XMLDoc = MyParser.getDocument();出现错误,为什么? e:lgm.java:70: cannot resolve symbol symbo...
JDK1.4以上版本。我写了一个lgm.java如下:编译时候出现如下错误:
其中e:\try\web-inf\classes\fxml.xml是正确的,请各位放心。
问题是Document XMLDoc = MyParser.getDocument();出现错误,为什么?
e:lgm.java:70: cannot resolve symbol
symbol : method getDocument ()
location: class javax.xml.parsers.DocumentBuilder
Document XMLDoc = MyParser.getDocument();
^
1 error
package bean;
import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
public class lgm{
String backxml="";
ResultSet rset=null;
Connection conn =null;
Statement stmt =null;
String localname="",sql="";
String attribute="";
//建立xml文档
public void writexml(){
String Filename="e:\try\web-inf\classes\fxml.xml";
BufferedWriter fos;
System.out.println(backxml);
try {
fos = new BufferedWriter(new FileWriter((new File(Filename))));
fos.write(backxml);
fos.flush();
} catch(IOException e) {
System.out.println("写文档时发生io错误");
}
}
//数据库连接
public void openConnection() throws ClassNotFoundException,SQLException {
this.conn=DriverManager.getConnection( "jdbc:odbc:airwork;","sa","");
}
public void executequery(String query) throws SQLException {
this.stmt=conn.createStatement();
this.rset=stmt.executeQuery(query);
}
public void executeupdate(String query) throws SQLException {
this.stmt=conn.createStatement();
stmt.executeUpdate(query);
if (stmt!=null) stmt.close();
}
public void close() throws SQLException {
if (conn!=null) conn.close();
if (rset!=null) rset.close();
if (stmt!=null) stmt.close();
}
//开始XML解析
public void ParseDoc() throws ClassNotFoundException,SQLException
{
DocumentBuilder MyParser;
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
try
{
writexml();
openConnection();
MyParser=factory.newDocumentBuilder();
MyParser.parse("e:\try\web-inf\classes\fxml.xml");
Document XMLDoc = MyParser.getDocument();
close();
}
catch (Exception e)
{
System.out.println("解析出错Error IOException!!!");
}
}
}
其中e:\try\web-inf\classes\fxml.xml是正确的,请各位放心。
问题是Document XMLDoc = MyParser.getDocument();出现错误,为什么?
e:lgm.java:70: cannot resolve symbol
symbol : method getDocument ()
location: class javax.xml.parsers.DocumentBuilder
Document XMLDoc = MyParser.getDocument();
^
1 error
package bean;
import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
public class lgm{
String backxml="";
ResultSet rset=null;
Connection conn =null;
Statement stmt =null;
String localname="",sql="";
String attribute="";
//建立xml文档
public void writexml(){
String Filename="e:\try\web-inf\classes\fxml.xml";
BufferedWriter fos;
System.out.println(backxml);
try {
fos = new BufferedWriter(new FileWriter((new File(Filename))));
fos.write(backxml);
fos.flush();
} catch(IOException e) {
System.out.println("写文档时发生io错误");
}
}
//数据库连接
public void openConnection() throws ClassNotFoundException,SQLException {
this.conn=DriverManager.getConnection( "jdbc:odbc:airwork;","sa","");
}
public void executequery(String query) throws SQLException {
this.stmt=conn.createStatement();
this.rset=stmt.executeQuery(query);
}
public void executeupdate(String query) throws SQLException {
this.stmt=conn.createStatement();
stmt.executeUpdate(query);
if (stmt!=null) stmt.close();
}
public void close() throws SQLException {
if (conn!=null) conn.close();
if (rset!=null) rset.close();
if (stmt!=null) stmt.close();
}
//开始XML解析
public void ParseDoc() throws ClassNotFoundException,SQLException
{
DocumentBuilder MyParser;
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
try
{
writexml();
openConnection();
MyParser=factory.newDocumentBuilder();
MyParser.parse("e:\try\web-inf\classes\fxml.xml");
Document XMLDoc = MyParser.getDocument();
close();
}
catch (Exception e)
{
System.out.println("解析出错Error IOException!!!");
}
}
}
|
Document XMLDoc = MyParser.parse("e:\try\web-inf\classes\fxml.xml");应该这样写
|
String uri = "file:" + "e:/try/web-inf/classes/fxml/xml" ;
MyParser.parser(uri);
MyParser.parser(uri);