当前位置: 技术问答>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
*******************************************************************
lgm.java代码如下:
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 如下:
其中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
*******************************************************************
lgm.java代码如下:
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 如下:
|
你的字符串书写格式不对,应为"file:e:/try/web-inf/classes/fxml.xml";。
String uri = "file:" + new File( name ).getAbsolutePath();
// 使用JAXP作为解析器
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document document = db.parse( uri );
String uri = "file:" + new File( name ).getAbsolutePath();
// 使用JAXP作为解析器
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document document = db.parse( uri );
|
对,我试了一下 应改为:
MyParser.parse("e:/try/web-inf/classes/fxml.xml");
MyParser.parse("e:/try/web-inf/classes/fxml.xml");
|
uri 是一种规范的资源格式 前面要加上protocol
|
file://e:/try/web-inf/classes/fxml.xml