看看底下这个:
import java.net.URL;
import java.io.*;
import oracle.xml.parser.v2.*;
import org.w3c.dom.*;public class OurDOMParser
{
private static DOMParser parser = null;
private static String encode = ConstLib.ENCODE;
/***
功能描述:构造器。
*/
public OurDOMParser()
{
}
/***
功能描述:将一串 XML 字符串转成 XML DOM 模式。
@param xmlString XML格式字符串
@return 转换成的XML节点
@throws ClassException
*/
public static Element parse(String xmlString) throws ClassException{
Element ele = null;
try{
parser = new DOMParser();
byte[] tempByte = xmlString.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(tempByte);
BufferedReader reader = new BufferedReader(new InputStreamReader(bais,encode));
parser.parse(reader);
Document doc = (Document)(parser.getDocument());
ele = doc.getDocumentElement();
}catch(Exception e){
e.printStackTrace();
throw new ClassException("0000001");//解析XML出错。
}
return ele;
}
/***
功能描述:将一个URL所指文件的内容转成DOM模式。
@param xml文件的url
@return 转换成的XML节点
@throws ClassException
*/
public Element parse(URL url) throws ClassException{
Element ele = null;
try{
parser = new DOMParser();
parser.parse(url);
Document doc = (Document)(parser.getDocument());
ele = doc.getDocumentElement();
}catch(Exception e){
e.printStackTrace();
throw new ClassException("0000001");//解析XML出错。
}
return ele;
}}
import java.net.URL;
import java.io.*;
import oracle.xml.parser.v2.*;
import org.w3c.dom.*;public class OurDOMParser
{
private static DOMParser parser = null;
private static String encode = ConstLib.ENCODE;
/***
功能描述:构造器。
*/
public OurDOMParser()
{
}
/***
功能描述:将一串 XML 字符串转成 XML DOM 模式。
@param xmlString XML格式字符串
@return 转换成的XML节点
@throws ClassException
*/
public static Element parse(String xmlString) throws ClassException{
Element ele = null;
try{
parser = new DOMParser();
byte[] tempByte = xmlString.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(tempByte);
BufferedReader reader = new BufferedReader(new InputStreamReader(bais,encode));
parser.parse(reader);
Document doc = (Document)(parser.getDocument());
ele = doc.getDocumentElement();
}catch(Exception e){
e.printStackTrace();
throw new ClassException("0000001");//解析XML出错。
}
return ele;
}
/***
功能描述:将一个URL所指文件的内容转成DOM模式。
@param xml文件的url
@return 转换成的XML节点
@throws ClassException
*/
public Element parse(URL url) throws ClassException{
Element ele = null;
try{
parser = new DOMParser();
parser.parse(url);
Document doc = (Document)(parser.getDocument());
ele = doc.getDocumentElement();
}catch(Exception e){
e.printStackTrace();
throw new ClassException("0000001");//解析XML出错。
}
return ele;
}}
在 http://xml.apache.org/batik/index.html 有比较详尽讲解。其中一种方法是:import java.io.IOException;import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.util.XMLResourceDescriptor;import org.w3c.dom.Document;try {
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
String uri = "http://...";
Document doc = f.createDocument(uri);
} catch (IOException ex) {
// ...
}
这个贴子我结了,还是感谢楼上的帮助。