右鍵點擊目錄,然後選擇new 然後選擇file ,輸入xxx.xml
⑵ java中根據xml模板創建xml
/**
* 復制單個文件
* @param oldPath String 原文件路徑 如:c:/fqf.txt
* @param newPath String 復制後路徑 如:f:/fqf.txt
* @return boolean
*/
public void File(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在時
InputStream inStream = new FileInputStream(oldPath); //讀入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //位元組數 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("復制單個文件操作出錯");
e.printStackTrace();
}
}
⑶ java 讀取xml文件部分內容 並創建新的xml文件
用java自帶的就可以,有問題可以問我
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
//構造
public XMLUtil(String name) throws ParserConfigurationException {
filename = name;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
document = builder.newDocument();
}
/**
* 保存到文件
*/
public void toSave() {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
StreamResult result = new StreamResult(pw);
transformer.transform(source, result);
} catch (TransformerException mye) {
mye.printStackTrace();
} catch (IOException exp) {
exp.printStackTrace();
}
}
如果對您有幫助,請記得採納為滿意答案,謝謝!祝您生活愉快!
vaela
⑷ Java 如何生成xml文件
寫文件的方法專:屬 FileWriter fw = new FileWriter("test.xml"); fw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><dongguancyy><no.8><fhID=\"101null\"></fh></no.8></dongguancyy>"); fw.flush(); fw.close();
⑸ 哪位兄台給一個用java來創建xml文件、更新xml節點的例子
java創建文件不都是一樣嗎?
至於更新文件,看下這個··
別人的
xml:
<?xml version="1.0" encoding="UTF-8"?>
<users>
<Messages>
<sendName>sendUsers</sendName>
<receiveName>snake</receiveName>
<date>2007-12-04 12:20:00</date>
<status>0</status>
<message>this is Content</message>
</Messages>
</users>
java:
package com.lianxi.DAO;
import java.io.File;
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;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class UpdateXml {
public static boolean doc2XmlFile(Document document,String filename)
{
boolean flag = true;
try
{
/** 將document中的內容寫入文件中 */
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
/** 編碼 */
//transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(filename));
transformer.transform(source, result);
}catch(Exception ex)
{
flag = false;
ex.printStackTrace();
}
return flag;
}
public static Document load(String filename)
{
Document document = null;
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
document=builder.parse(new File(filename));
document.normalize();
}
catch (Exception ex){
ex.printStackTrace();
}
return document;
}
/**
* 演示修改文件的具體某個節點的值
*/
public static void xmlUpdateDemo()
{
Document document = load("c://Message.xml");
Node root=document.getDocumentElement();
/** 如果root有子元素 */
if(root.hasChildNodes())
{
/** ftpnodes */
NodeList ftpnodes = root.getChildNodes();
/** 循環取得ftp所有節點 */
for (int i=0;i<ftpnodes.getLength();i++)
{
NodeList ftplist = ftpnodes.item(i).getChildNodes();
for (int k=0;k<ftplist.getLength();k++)
{
Node subnode = ftplist.item(k);
/** 刪除ftp-chn節點 */
// if (subnode.getNodeType()==Node.ELEMENT_NODE&&subnode.getNodeName()=="ftp-chn")
// {
// ftpnodes.item(i).removeChild(subnode);
// }
/** 修改ftp-host的值為 192.168.0.1 */
if (subnode.getNodeType()==Node.ELEMENT_NODE&&subnode.getNodeName()=="status")
{
subnode.getFirstChild().setNodeValue("1");
}
}
}
}
doc2XmlFile(document,"c://Message.xml");
}
public static void main(String args[])throws Exception
{
UpdateXml.xmlUpdateDemo();
}
}
⑹ 如何用java生成一個xml文件
//首先下載並添加dom4j.jar包(自己下載)
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**
* @author wsj
*/
public class Test1 {
public static void main(String[] args) throws Exception {
Document dom=DocumentHelper.createDocument();//創建文件
Element root=dom.addElement("XVal");//添加根元素,Xval
root.addAttribute("X", "X字元串的值");
root.setText("Xval標簽中間的值");
String xml=dom.asXML();
System.out.println(xml);
File f=new File("d:/xml.xml");
PrintWriter pw=new PrintWriter(f);
pw.write(xml);
pw.close();
JOptionPane.showMessageDialog(null, "已生成xml文件,路徑為為d:/xml.xml");
}
}