xml的读和写
import org.w3c.dom.*;import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
/**
*
* @author heshuai
*
*/
public class WriterXml {
String filename;
Document document;
public WriterXml(String name) throws ParserConfigurationException {
filename = name;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.newDocument();
}
public void toWrite(String mytitle, String mycontent) {
Element root = document.createElement("WorkShop");
document.appendChild(root);
Element title = document.createElement("Title");
title.appendChild(document.createTextNode(mytitle));
root.appendChild(title);
Element content = document.createElement("Content");
content.appendChild(document.createTextNode(mycontent));
root.appendChild(content);
}
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();
}
}
public static void main(String[] args) {
try {
WriterXml myxml = new WriterXml("f:\\9.xml");
myxml.toWrite("中文题目", "中文内容");
myxml.toSave();
System.out.print("Your writing is successful.");
} catch (ParserConfigurationException exp) {
exp.printStackTrace();
System.out.print("Your writing is failed.");
}
}
} 只有大片代码,没有一点注释和说明,典型的某些开发人员的风格,别人完全不知道你想表达什么,怪不得没有人回复。别怪我打击你。:) QTP里面 怎么调用 java 的代码啊?
是不是 发错地方了? java代码写到dll里,然后注册,qtp再调用
页:
[1]