写XML文件(DOM无需额外包)

 
/**
	*  写入XML
	*/
	public static boolean saveXml(String spath) {
			
		DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		try{
			db = dbf.newDocumentBuilder();
		}catch(Exception pce)
		{
			System.exit(1);
		}
		Document doc = db.newDocument();
		
			try {
				TransformerFactory tFactory = TransformerFactory.newInstance();
				Transformer transformer = tFactory.newTransformer();
				DOMSource source = new DOMSource(doc);
				
				
				Element root = doc.createElement("ProductList");
				doc.appendChild(root);
				Element productitem = doc.createElement("ProductItem");
				root.appendChild(productitem);
				Element productname = doc.createElement("ProductName");
				productname.appendChild(doc.createTextNode("产品1"));
				productitem.appendChild(productname);
				
				
				StreamResult result = new StreamResult(new File("C:\\product.xml"));
				transformer.transform(source, result);
				}
				catch(Exception e){
				}
				return true;
		}

你可能感兴趣的:(c,xml,exception,String,File,产品)