异常如下:
javax.xml.transform.TransformerException: java.io.FileNotFoundException: file:\e:\Tomcat%205.5\shoppingcarts.xml (文件名、目录名或卷标语法不正确。)
代码如下:
public class shoppingcart_operator
{
private DocumentBuilderFactory factory;
private DocumentBuilder builder;
private Document doc;
private String xml_filename;
public shoppingcart_operator()
{
this.xml_filename = "f:/newweb/xml/shoppingcarts.xml";
this.Create_xml_file();
}
public shoppingcart_operator(String str_ServerPath)
{
this.xml_filename = str_ServerPath + "shoppingcarts.xml";
this.Create_xml_file();
}
//生成xml文件
public void Create_xml_file()
{
File shoppingcart_xml = new File(xml_filename);
if(! shoppingcart_xml.exists())
{
try
{
FileOutputStream fos = new FileOutputStream(xml_filename);
Writer out = new OutputStreamWriter(fos, "UTF-8");
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><shopping_carts></shopping_carts>");
out.close();
fos.close();
//this.Write_xml_main();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
//添加数据
public boolean writeShopingCart(shoppingcart spc)
{
boolean success = true;
try
{
factory = DocumentBuilderFactory.newInstance();
builder=factory.newDocumentBuilder();
//doc=builder.parse(new File(xml_filename));
doc=builder.parse("shoppingcarts.xml");
doc.normalize();
Text textseg;
Element shoppingcart = doc.createElement("shopping_cart");
//shoppingcart - 用户编号
Element shoppingcart_user_id = doc.createElement("user_id");
textseg=doc.createTextNode(spc.getUser_id());
shoppingcart_user_id.appendChild(textseg);
shoppingcart.appendChild(shoppingcart_user_id);
//shoppingcart - 产品编号
Element shoppingcart_product_id = doc.createElement("product_id");
textseg=doc.createTextNode(spc.getProduct_id());
shoppingcart_product_id.appendChild(textseg);
shoppingcart.appendChild(shoppingcart_product_id);
//shoppingcart - 产品名称
Element shoppingcart_product_name = doc.createElement("product_name");
textseg=doc.createTextNode(spc.getProduct_name());
shoppingcart_product_name.appendChild(textseg);
shoppingcart.appendChild(shoppingcart_product_name);
//shoppingcart - 产品数量
Element shoppingcart_count=doc.createElement("count");
textseg=doc.createTextNode(spc.getCount());
shoppingcart_count.appendChild(textseg);
shoppingcart.appendChild(shoppingcart_count);
//shoppingcart - 产品单价
Element shoppingcart_price=doc.createElement("price");
textseg=doc.createTextNode(spc.getPrice());
shoppingcart_price.appendChild(textseg);
shoppingcart.appendChild(shoppingcart_price);
doc.getDocumentElement().appendChild(shoppingcart);//取得根节点然后加入子元素
TransformerFactory tFactory =TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);