1个回答
展开全部
主要是这几个包:
javax.xml.parsers
javax.xml.transform
javax.xml.transform.dom.DOMSource
javax.xml.transform.stream.StreamResult
org.w3c.dom
import java.io.*;
import java.util.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
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.NodeList;
import tools.Vote;//自己的类
//载入
private DocumentBuilderFactory dbf=null;
private DocumentBuilder db=null;
private Document doc=null;
private List listXml=null;
public Document loadXml(File file)
{
try
{
dbf=DocumentBuilderFactory.newInstance();
db=dbf.newDocumentBuilder();
doc=db.parse(file);
}
catch(Exception e)
{
e.printStackTrace();
}
return doc;
}
// 添加节点
public void addOptionXmlData(File file,int parentPos,String option)
{
if(doc==null){
doc=loadXml(file);
}
try
{
Element root=doc.getDocumentElement();
Element voot=(Element)root.getElementsByTagName("vote").item(parentPos);
Element optionNode=doc.createElement("option");
voot.appendChild(optionNode);
optionNode.setTextContent(option);
optionNode.setAttribute("sum", "0");
}catch(Exception ex){
ex.printStackTrace();
}
try//保存到XML文件
{
TransformerFactory tf=TransformerFactory.newInstance();
Transformer t=tf.newTransformer();
Properties props=t.getOutputProperties();
props.setProperty(OutputKeys.ENCODING, "gb2312");
t.setOutputProperties(props);
DOMSource dom=new DOMSource(doc);
StreamResult sr=new StreamResult(file);
t.transform(dom, sr);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
javax.xml.parsers
javax.xml.transform
javax.xml.transform.dom.DOMSource
javax.xml.transform.stream.StreamResult
org.w3c.dom
import java.io.*;
import java.util.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
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.NodeList;
import tools.Vote;//自己的类
//载入
private DocumentBuilderFactory dbf=null;
private DocumentBuilder db=null;
private Document doc=null;
private List listXml=null;
public Document loadXml(File file)
{
try
{
dbf=DocumentBuilderFactory.newInstance();
db=dbf.newDocumentBuilder();
doc=db.parse(file);
}
catch(Exception e)
{
e.printStackTrace();
}
return doc;
}
// 添加节点
public void addOptionXmlData(File file,int parentPos,String option)
{
if(doc==null){
doc=loadXml(file);
}
try
{
Element root=doc.getDocumentElement();
Element voot=(Element)root.getElementsByTagName("vote").item(parentPos);
Element optionNode=doc.createElement("option");
voot.appendChild(optionNode);
optionNode.setTextContent(option);
optionNode.setAttribute("sum", "0");
}catch(Exception ex){
ex.printStackTrace();
}
try//保存到XML文件
{
TransformerFactory tf=TransformerFactory.newInstance();
Transformer t=tf.newTransformer();
Properties props=t.getOutputProperties();
props.setProperty(OutputKeys.ENCODING, "gb2312");
t.setOutputProperties(props);
DOMSource dom=new DOMSource(doc);
StreamResult sr=new StreamResult(file);
t.transform(dom, sr);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询