java修改xml代码 java设置xml文件编码格式

求一段 修改 xml文件的java代码

主要是这几个包:

目前成都创新互联已为上千多家的企业提供了网站建设、域名、虚拟空间、成都网站托管、企业网站设计、灵石网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

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();

}

}

java修改XMl文件,请高手指点

楼主这个思路是没有什么问题的,存入map中可以方便整个项目使用,特别是针对那些项目中经常要读取的xml,可以使用xpath

public static void modifyXMLFile(String name,String value) {

String oldStr = "E:/Work/Proj/gfweb/src/configuration.xml";

String newStr = "E:/Work/Proj/gfweb/src/configuration.xml";

Document document = null;

try {

SAXReader saxReader = new SAXReader(); // 用来读取xml文档

saxReader.setEncoding("GBK");

document = saxReader.read(new File(oldStr)); // 读取xml文档

List list = document.selectNodes("/system/category/item");// 用xpath查找节点book的属性

Iterator iter = list.iterator();

while (iter.hasNext()) {

Element element = (Element) iter.next();

//Attribute attribute = (Attribute) iter.next();

String itemname = element.attributeValue("name");

if(name.equals(itemname)){

element.setAttributeValue("value",value);

}

}

} catch (Exception e) {

e.printStackTrace();

}

try {

OutputFormat outFmt = OutputFormat.createPrettyPrint();

outFmt.setEncoding("GBK");

outFmt.setIndent(true);

XMLWriter writer = new XMLWriter(new FileWriter(new File(newStr)),outFmt);

writer.write(document);

writer.close();

} catch (Exception ex) {

ex.printStackTrace();

}

}

求一段 读出\修改 xml文件的java代码

javax.xml.parsers

javax.xml.transform

javax.xml.transform.dom.DOMSource

javax.xml.transform.stream.StreamResult

org.w3c.dom

测试的时候写的一个完整的操作XML的类,检测的投票系统

package tools;

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;

public class XmlHandler {

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 List getXmlData(File file)

{

if(doc==null){

doc=loadXml(file);

}

listXml=new ArrayList();

Element root=doc.getDocumentElement();//得到根节点votes

NodeList votes=root.getElementsByTagName("vote");//得到vote节点的集合

for(int i=0;ivotes.getLength();i++)

{

Vote form=new Vote();

Element vote=(Element)votes.item(i);

form.setId(vote.getAttribute("id"));

form.setSum(vote.getAttribute("sum"));

form.setTitle(vote.getAttribute("title"));

form.setTime(vote.getAttribute("time"));

NodeList options=vote.getElementsByTagName("option");

String[] sums=new String[options.getLength()];//存储vote下每个option的sum

String[] opts=new String[options.getLength()];//存储vote下每个option的text

for(int k=0;koptions.getLength();k++)

{

Element option=(Element)options.item(k);

sums[k]=option.getAttribute("sum");

opts[k]=option.getTextContent();

}

form.setSums(sums);

form.setOptions(opts);

listXml.add(form);

}

return listXml;

}

public void modifyXmlData(File file,int parentPos,int childPos)

{

if(doc==null){

doc=loadXml(file);

}

try{

Element root=doc.getDocumentElement();

NodeList votes=root.getElementsByTagName("vote");

Element vote=(Element)votes.item(parentPos);

int sum=Integer.parseInt(vote.getAttribute("sum"))+1;

//System.out.println(sum);

//vote.removeAttribute("sum");

vote.setAttribute("sum", String.valueOf(sum));

NodeList options=vote.getElementsByTagName("option");

//System.out.println(options.getLength());

Element option=(Element)options.item(childPos);

int opt_sum=Integer.parseInt(option.getAttribute("sum"))+1;

//option.removeAttribute("sum");

option.setAttribute("sum", String.valueOf(opt_sum));

}

catch(Exception e)

{

e.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();

}

}

public void deleteXmlData(File file,int parentPos,int childPos)//注:该方法不支持批量删除,批量删除可用split分割位置

{

if(doc==null)

{

doc=loadXml(file);

}

try

{

boolean flag;

if(childPos==-1)

{

flag=true;//删除父节点

}else{

flag=false;//删除子节点

}

Element root=doc.getDocumentElement();

Element vote=(Element)root.getElementsByTagName("vote").item(parentPos);

if(flag)//对父节点vote进行删除

{

root.removeChild(vote);

}else if(!flag)

{

Element option=(Element)vote.getElementsByTagName("option").item(childPos);

vote.setAttribute("sum", String.valueOf(Integer.parseInt(vote.getAttribute("sum"))-Integer.parseInt(option.getAttribute("sum"))));

vote.removeChild(option);

}

}

catch(Exception e)

{

e.printStackTrace();

}

try

{

TransformerFactory tf=TransformerFactory.newInstance();

Transformer t=tf.newTransformer();

Properties props=t.getOutputProperties();

props.setProperty(OutputKeys.ENCODING, "gb2312");

t.setOutputProperties(props);

DOMSource ds=new DOMSource(doc);

StreamResult sr=new StreamResult(file);

t.transform(ds, sr);

}

catch(Exception e)

{

e.printStackTrace();

}

}

public void appendXmlData(File file,String title,String[] options)

{

if(doc==null)

{

doc=loadXml(file);

}

try

{

Element root=doc.getDocumentElement();

Element vote=doc.createElement("vote");

root.appendChild(vote);

vote.setAttribute("time", new Date().toLocaleString());

vote.setAttribute("id", String.valueOf(root.getElementsByTagName("vote").getLength()));

vote.setAttribute("sum", "0");

vote.setAttribute("title", title);

for(int i=0;ioptions.length;i++)

{

Element option=doc.createElement("option");

vote.appendChild(option);

option.setAttribute("sum", "0");

option.setTextContent(options[i]);

}

}

catch(Exception ex){

ex.printStackTrace();

}

try{

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();

}

}

public Vote selectSingleOne(File file,int parentPos)

{

if(doc==null)

{

doc=loadXml(file);

}

Element root=doc.getDocumentElement();

Element vote=(Element)root.getElementsByTagName("vote").item(parentPos);

Vote form=new Vote();

form.setId(vote.getAttribute("id"));

form.setTime(vote.getAttribute("time"));

form.setTitle(vote.getAttribute("title"));

form.setSum(vote.getAttribute("sum"));

int len=vote.getElementsByTagName("option").getLength();

NodeList nodeList=vote.getElementsByTagName("option");

String[] sums=new String[len];

String[] opts=new String[len];

for(int i=0;ilen;i++)

{

Element option=(Element)nodeList.item(i);

sums[i]=option.getAttribute("sum");

opts[i]=option.getTextContent();

}

form.setOptions(opts);

form.setSums(sums);

return form;

}

public void modifyOneXmlData(File file,int parentPos,String title,String[] options)

{

if(doc==null){

doc=loadXml(file);

}

try

{

Element root=doc.getDocumentElement();

Element vote=(Element)root.getElementsByTagName("vote").item(parentPos);

vote.setAttribute("title", title);

vote.setAttribute("sum", "0");

int len=vote.getElementsByTagName("option").getLength();

for(int i=0;ilen;i++)

{

Element option=(Element)vote.getElementsByTagName("option").item(i);

option.setAttribute("sum", "0");

option.setTextContent(options[i]);

}

}catch(Exception ex){

ex.printStackTrace();

}

try

{

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();

}

}

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

{

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();

}

}

}

java修改xml文档

最好还是自己写,我这里有个xml处理类 你自己看着写吧, 给个邮箱


分享题目:java修改xml代码 java设置xml文件编码格式
文章出自:http://cdiso.cn/article/dosjidp.html

其他资讯