免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

java修改xml代碼 java設(shè)置xml文件編碼格式

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

主要是這幾個包:

目前成都創(chuàng)新互聯(lián)已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、成都網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計、靈石網(wǎng)站維護等服務(wù),公司將堅持客戶導向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

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;

}

// 添加節(jié)點

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中可以方便整個項目使用,特別是針對那些項目中經(jīng)常要讀取的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查找節(jié)點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

測試的時候?qū)懙囊粋€完整的操作XML的類,檢測的投票系統(tǒng)

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();//得到根節(jié)點votes

NodeList votes=root.getElementsByTagName("vote");//得到vote節(jié)點的集合

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;//刪除父節(jié)點

}else{

flag=false;//刪除子節(jié)點

}

Element root=doc.getDocumentElement();

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

if(flag)//對父節(jié)點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設(shè)置xml文件編碼格式
標題來源:http://m.newbst.com/article18/dosjidp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站動態(tài)網(wǎng)站App設(shè)計標簽優(yōu)化網(wǎng)站策劃云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)