请教一下,怎么把符合XML格式的String类型转换成JDOM的Document??

lhj_cn 2007-07-31 12:41:29
同问题。
Document doc = (Document) DocumentHelper.parseText(xmlstring1); //DOM4J
这样怎么不行呢?
其中DocumentHelper.parseText是DOM4J里的方法,返回的是DOM4J的Document,这样得到的应该是有值的,但是我调试却有异常:
java.lang.ClassCastException: org.dom4j.tree.DefaultDocument

现在就想问问怎么把string 直接转换成 JDOM的Document???
...全文
562 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
masse 2007-07-31
  • 打赏
  • 举报
回复
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class BasicDom {
public static void main(String[] args) {
Document doc = parseXmlFile("infilename.xml", false);
}

// Parses an XML file and returns a DOM document.
// If validating is true, the contents is validated against the DTD
// specified in the file.
public static Document parseXmlFile(String filename, boolean validating) {
try {
// Create a builder factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(validating);

// Create the builder and parse the file
Document doc = factory.newDocumentBuilder().parse(new File(filename));
return doc;
} catch (SAXException e) {
// A parsing error occurred; the xml input is not valid
} catch (ParserConfigurationException e) {
} catch (IOException e) {
}
return null;
}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧