2013-11-23 88 views
0

我正在使用jGRASP版本2.0.0_03。 (使用jdk1.7.0_25)用Java编写的程序, 我尝试用XML和Java学习之用......这就是我试图使用Java创建XML文档:我应该怎么做才能在java中使用ELement类

private void writeFile() 
    { 
    dFact = DocumentBuilderFactory.newInstance(); 
    build = dFact.newDocumentBuilder(); 
    doc = build.newDocument(); 

    Element root = doc.createElement("outPutResult"); 
    doc.appendChild(root); 

    for(Result r:resultList) 
    { 
    Element title = doc.createElement("Title"); 
    title.appendChild(doc.createTextNode(r.getTitle)); 
    root.appendChild(title); 

    Element address = doc.createElement("Address"); 
    address.appendChild(doc.createTextNode(r.getAddress)); 
    root.appendChild(address); 
    } 


    }//End of Write function 

我得到的错误作为:

hw12.java:38: error: cannot find symbol 
    Document doc; 
    ^
    symbol: class Document 
    location: class hw12 
hw12.java:246: error: cannot find symbol 
    Element root = doc.createElement("outPutResult"); 
^
    symbol: class Element 
    location: class hw12 

我该输入什么来使用这些类?

+0

您是否配置了类路径?当java编译器找不到某些东西,数据类型,调用方法时会发生此错误。 –

+0

不,它是*不是*类路径问题 - 这将是运行时错误,而不是编译器错误。 –

回答

0

这些类位于org.w3c.dom包中。您需要在源文件的顶部输入陈述,如

import org.w3c.dom.Element; 

Et cetera。

+0

我需要在jdk中添加文件吗? 我试过了,因此现在使用xmlWriterStream感到沮丧< – Snedden27

+1

@ Snedden27不,org.w3c.dom类和接口默认是可用的,它们内置于JRE类库中。 –

相关问题