问题描述
- 无法建立xml文件 dos命令下运行出错
- 我按照书上的代码敲进eclipse,没报错,代码没问题,这个代码的功能是在同目录下建立一个与该java文件同名的xml文件,xml文件里输入了相关数据,但是运行后没反应,本地没生成该xml文件,浴室dos命令运行该java文件,javac编译通过,可是java时却“
F:eclipseWorkSpacexmlupdatesrcxmlupdate>java cratexml
错误: 找不到或无法加载主类 cratexmlF:eclipseWorkSpacexmlupdatesrcxmlupdate>“
报告无法加载主类,这是什么原因,网上说的classpath等配置之类的都没问题。以下是代码:
package xmlupdate;
import java.io.File;
import java.io.FileOutputStream;import javax.xml.parsers.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;public class cratexml {
public static void main(String [] args){
org.w3c.dom.Document doc;
org.w3c.dom.Element booklist;
org.w3c.dom.Element book;try{ DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=dbf.newDocumentBuilder(); doc=builder.newDocument(); if(doc!=null){ booklist=doc.createElement(""图书列表""); book=doc.createElement(""图书""); booklist.appendChild(book); org.w3c.dom.Element isbn = doc.createElement(""isbn""); org.w3c.dom.Element name = doc.createElement(""name""); org.w3c.dom.Element author = doc.createElement(""author""); org.w3c.dom.Element price = doc.createElement(""price""); org.w3c.dom.Element num = doc.createElement(""num""); book.appendChild(name); book.appendChild(author); book.appendChild(price); book.appendChild(num); doc.appendChild(booklist); TransformerFactory transFactory=TransformerFactory.newInstance(); Transformer transformer=transFactory.newTransformer(); DOMSource domSource=new DOMSource(); File file=new File(""book.xml""); FileOutputStream out=new FileOutputStream(file); StreamResult xmlResult=new StreamResult(out); transformer.transform(domSource xmlResult); } }catch (Exception e){ e.printStackTrace(); }}
}
解决方案
时间: 2024-11-18 14:20:20