右键点击目录,然后选择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");
}
}