「java写入xml」java写入xml文件
今天给各位分享java写入xml的知识,其中也会对java写入xml文件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java如何向xml文件写入内容?
我以前学dom解析的时候写了一个小例子,你参考参考
package com.lhx.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class Test {
public static void main(String[] args) {
DocumentBuilderFactory fct=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder bui=fct.newDocumentBuilder();
Document doc=bui.newDocument();
Element ps=doc.createElement("persons");
Element p1=doc.createElement("person");
Element p2=doc.createElement("person");
Attr id1=doc.createAttribute("id");
Attr id2=doc.createAttribute("id");
id1.setNodeValue("1");
id2.setNodeValue("2");
Element name1=doc.createElement("name");
Text na1=doc.createTextNode("龙大哥");
Element name2=doc.createElement("name");
Text na2=doc.createTextNode("龙大爷");
Element sex1=doc.createElement("sex");
Text se1=doc.createTextNode("帅哥");
Element sex2=doc.createElement("sex");
Text se2=doc.createTextNode("妹子");
doc.appendChild(ps);
ps.appendChild(p1);
p1.appendChild(name1);
p1.setAttributeNode(id1);
name1.appendChild(na1);
p1.appendChild(sex1);
sex1.appendChild(se1);
ps.appendChild(p2);
p2.appendChild(name2);
p2.setAttributeNode(id2);
name2.appendChild(na2);
p2.appendChild(sex2);
sex2.appendChild(se2);
try {
FileOutputStream fos=new FileOutputStream(new File("E:/longdada.xml"));
try {
((org.apache.crimson.tree.XmlDocument)doc)
.write(fos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
附图:
这个例子有文本节点的创建,属性的创建等等,基本上可以解决绝大多数XML内容了。无论你想创建什么类型的XML,可以套用里面的方法。
另外,注意:文件通过流创建的时候用到一个类,需要一个jar,这个类我已经用完整形式写出来了,你去网上下载下来,添加进工程即可。
弱国觉得可行,望采纳^_^
JAVA如何写XML文件?
import java.io.*;\x0d\x0a \x0d\x0aimport org.dom4j.*;\x0d\x0a import org.dom4j.io.OutputFormat;\x0d\x0a import org.dom4j.io.XMLWriter;\x0d\x0a \x0d\x0apublic class DOM4JTest {\x0d\x0a public static void main(String[] args) {\x0d\x0a Document doc = DocumentHelper.createDocument();\x0d\x0a doc.addProcessingInstruction("xml-stylesheet", "type='text/xsl href='students.xsl'");\x0d\x0a Element root = doc.addElement("students");\x0d\x0a \x0d\x0a Element eltStu1 = root.addElement("student").addAttribute("sn", "01");\x0d\x0a Element eltName1 = eltStu1.addElement("name");\x0d\x0a Element eltAge1 = eltStu1.addElement("age");\x0d\x0a eltName1.setText("张三");\x0d\x0a eltAge1.setText("20");\x0d\x0a \x0d\x0a Element eltStu2 = root.addElement("student").addAttribute("sn", "02");\x0d\x0a Element eltName2 = eltStu2.addElement("name");\x0d\x0a Element eltAge2 = eltStu2.addElement("age");\x0d\x0a eltName2.setText("李四");\x0d\x0a eltAge2.setText("18");\x0d\x0a \x0d\x0a try {\x0d\x0a OutputFormat format = new OutputFormat("\x0d\x0a ", true);\x0d\x0a format.setEncoding("gb2312");\x0d\x0a // 可以把System.out改为你要的流。\x0d\x0a XMLWriter xmlWriter = new XMLWriter(new PrintWriter(System.out), format);\x0d\x0a xmlWriter.write(doc);\x0d\x0a xmlWriter.close();\x0d\x0a } catch (IOException e) {\x0d\x0a e.printStackTrace();\x0d\x0a }\x0d\x0a }\x0d\x0a }
如何用Java实现对xml文件的读取和写入以及保存
直接附源码import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;import org.dom4j.*;
import org.dom4j.io.XMLWriter;
public class Dom4jSample { public static void main(String[] args) {
Dom4jSample dom4jSample = new Dom4jSample();
Document document = dom4jSample.createDocument();
try{
dom4jSample.FileWrite(document);
Document documentStr = dom4jSample.StringToXML("ChinaI Love!/China");
dom4jSample.XMLWrite(documentStr);
Element legend = dom4jSample.FindElement(document);
System.out.println(legend.getText());
}
catch(Exception e)
{
}
}
/*
* Create a XML Document
*/
public Document createDocument()
{
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");
Element author1 = root.addElement("Lynch");
author1.addAttribute("Age","25");
author1.addAttribute("Country","China");
author1.addText("I am great!");
Element author2 = root.addElement("Legend");
author2.addAttribute("Age","25");
author2.addAttribute("Country","China");
author2.addText("I am great!too!");
return document;
}
/*
* Create a XML document through String
*/
public Document StringToXML(String str) throws DocumentException
{
Document document = DocumentHelper.parseText(str);
return document;
}
public Element FindElement(Document document)
{
Element root = document.getRootElement();
Element legend = null;
for(Iterator i=root.elementIterator("legend");i.hasNext();)
{
legend = (Element)i.next();
}
return legend;
}
/*
* Write a XML file
*/
public void FileWrite(Document document) throws IOException
{
FileWriter out = new FileWriter("C:/Dom2jSample.xml");
document.write(out);
out.close();
}
/*
* Write a XML format file
*/
public void XMLWrite(Document document) throws IOException
{
XMLWriter writer = new XMLWriter(new FileWriter("C:/Dom2jSampleStr.xml"));
writer.write(document);
writer.close();
}
}
java读取指定文件夹并写入xml
你用File f=new File("D:\\TestResult\\" + folderName); boolean b=f.mkdirs(); 先创建文件夹,然后在创建文件夹下的输出文件
if(b)
FileOutputStream fos=new FileOutputStream("D:\\TestResult\\" + folderName + xmlName + ".xml"));
java中如何写xml
呵呵,你这个问题,其实写XML很简单的,氛围以下几步:1.导入DOM4J包,在此我使用dom4j来做。2.创建dom4j的实例:Document document = DocumentHelper.createDocument();3.一级一级的添加节点或者属性,这个具体参照dom4j帮助:Element rootGen = document.addElement("root");4.定义以下3个对象,然后进行操作: Writer writer = null;
OutputFormat format = null;
XMLWriter xmlwriter = null; ***********************************************************************************附上代码: public void CreateXMl(StudentBean sn){
//创建document对象
Document document = DocumentHelper.createDocument();
//定义根节点Element
Element rootGen = document.addElement("root");
//定义根节点ROOT的子节点们
Element nameGen = rootGen.addElement("Name");
nameGen.addAttribute("name", "我是中文");
Element ageGen = rootGen.addElement("Age");
Element addrGen = rootGen.addElement("Address");
Writer writer = null;
OutputFormat format = null;
XMLWriter xmlwriter = null;
//将定义好的内容写入xml文件中
try {
//使用这个writer也可以,只不过遇到中文会乱码哦
// writer = new FileWriter("d:/test.xml");
//进行格式化
format = OutputFormat.createPrettyPrint();
//设定编码
format.setEncoding("UTF-8");
xmlwriter = new XMLWriter(new FileOutputStream("d:/test.xml"), format);
xmlwriter.write(document);
xmlwriter.flush();
xmlwriter.close();
System.out.println("-----------Xmlfile successfully created-------------");
} catch (Exception e) {
e.printStackTrace();
System.out.println("-----------Exception occured during of create xmlfile -------");
}
}
关于java写入xml和java写入xml文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-25,除非注明,否则均为
原创文章,转载请注明出处。