java中,怎么将一组数据写入xml文件中?
1个回答
展开全部
有很多种方法,用DOM或SAX, 还有JAXB等。下面给个DOM写的例子:
java使用DOM创建如下xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<name id="2">zs</name>
</root>
代码如下:
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.Element;
import org.w3c.dom.Text;
public class CreateXML {
public static void main(String[] args) throws Exception {
//实例化解析器
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
//创建Document对象
Document doc = builder.newDocument();
//创建XML文件所需的各种对象并序列化
Element root = doc.createElement("root");
Element name = doc.createElement("name");
Text nameText = doc.createTextNode("zs");
name.appendChild(nameText);
root.appendChild(name);
doc.appendChild(root);
name.setAttribute("id", "2");
doc2XmlFile(doc,"test.xml");
}
public static boolean doc2XmlFile(Document document, String filename) {
boolean flag = true;
try {
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;
}
}
java使用DOM创建如下xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<name id="2">zs</name>
</root>
代码如下:
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.Element;
import org.w3c.dom.Text;
public class CreateXML {
public static void main(String[] args) throws Exception {
//实例化解析器
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
//创建Document对象
Document doc = builder.newDocument();
//创建XML文件所需的各种对象并序列化
Element root = doc.createElement("root");
Element name = doc.createElement("name");
Text nameText = doc.createTextNode("zs");
name.appendChild(nameText);
root.appendChild(name);
doc.appendChild(root);
name.setAttribute("id", "2");
doc2XmlFile(doc,"test.xml");
}
public static boolean doc2XmlFile(Document document, String filename) {
boolean flag = true;
try {
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;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |