本篇代码与之前的xml读取正好为反向的操作,前文中,将所有方法直接写在main()方法中,也可以写在外面,而在main()方法中调用。本文就以此方式来编写代码。
从本文代码中能看出来,由于不能直接使用递归和循环操作,使得输入文本类容的版块比较冗长繁琐,期待以后学习新知识来改善这一环节。
【代码如下】
package sina1_xml_dom;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class DomTest {
public DocumentBuilder getDocumentBuilder(){
// 创建一个DocumentBuilderFactory对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//创建一个DocumentBuilder对象
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationExcep
tion e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return db;
}
public void createXML(){
DocumentBuilder db = getDocumentBuilder();
Document document = db.newDocument();
//创建文档
document.setXmlStandalone(true);
//将standalone设为“yes”
Element Dota2 = document.createElement_x("Dota2");
//创建根节点
Element Hero = document.createElement_x("Hero");
//创建子节点
Element live = document.createElement_x("生命");
//创建
Element magic = document.createElement_x("魔法");
Element strength = document.createElement_x("力量");
Element agility = document.createElement_x("敏捷");
Element wit = document.createElement_x("智力");
Element initialAttack = document.createElement_x("初始攻击");
Element initialArmor = document.createElement_x("初始护甲");
Element attackRange = document.createElement_x("攻击范围");
Element view = document.createElement_x("视野");
Element movementSpeed = document.createElement_x("移动速度");
Element trajectorySpeed = document.createElement_x("弹道速度");
Element conjuryTime = document.createElement_x("施法前后摇");
Element attackTime = document.createElement_x("攻击前后摇");
live.setTextContent("454");
//由于setNodeValve()对于Element类型的返回值永远是null,故添加文本内容要用该方法
magic.setTextContent("273");
strength.setTextContent("16(+1.7)");
agility.setTextContent("16(+1.6)");
wit.setTextContent("18(+2.9)");
initialAttack.setTextContent("38-44");
initialArmor.setTextContent("1.24");
attackRange.setTextContent("600");
view.setTextContent("1800/800");
movementSpeed.setTextContent("280");
trajectorySpeed.setTextContent("900");
conjuryTime.setTextContent("0.3/2.4");
attackTime.setTextContent("0.55/0");
Hero.appendChild(live);
//添加
Hero.appendChild(magic);
Hero.appendChild(strength);
Hero.appendChild(agility);
Hero.appendChild(wit);
Hero.appendChild(initialAttack);
Hero.appendChild(initialArmor);
Hero.appendChild(attackRange);
Hero.appendChild(view);
Hero.appendChild(movementSpeed);
Hero.appendChild(trajectorySpeed);
Hero.appendChild(conjuryTime);
Hero.appendChild(attackTime);
Hero.setAttribute("name", "水晶室女");
Dota2.appendChild(Hero);
//向根节点中添加子节点
document.appendChild(Dota2);
//将根节点(已包含Hero)添加到DOM树中
//创建xml文件
TransformerFactory tff = TransformerFactory.newInstance();
//创建工厂类型的变量不能用new,要用newInstance()
try {
Transformer tf = tff.newTransformer();
//创建Transformer对象
tf.setOutputProperty(OutputKeys.INDENT, "yes");
//yes代表换行操作
tf.transform(new DOMSource(document), new StreamResult(new File("C:\\test\\Heros2.xml")));
} catch (TransformerConfiguration
Exception e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
DomTest test = new DomTest();
test.createXML();
long end = System.currentTimeMillis();
System.out.println(end-start);
}
}
【运行结果如下】
82
打开 → C:\\test\\Heros2.xml
<生命>454</生命>
<魔法>273</魔法>
<力量>16(+1.7)</力量>
<敏捷>16(+1.6)</敏捷>
<智力>18(+2.9)</智力>
<初始攻击>38-44</初始攻击>
<初始护甲>1.24</初始护甲>
<攻击范围>600</攻击范围>
<视野>1800/800</视野>
<移动速度>280</移动速度>
<弹道速度>900</弹道速度>
<施法前后摇>0.3/2.4</施法前后摇>
<攻击前后摇>0.55/0</攻击前后摇>