xml文件的修改接口

刘飞3002 发布于 2012/03/19 15:28
阅读 877
收藏 0

我现在需要一个接口来修改xml文件中的一些系统配置,使得这些被指信息可以不用由程序员事先设定,那这样的接口要怎样写呢?我在网上找了下面这个例子。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.xml.sax.*;
public class XmlModifier {


	public XmlModifier() {
		DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();//定义一个XML文件的解析器
		try {
			DocumentBuilder dombuilder = domfac.newDocumentBuilder();        //定义一个API来获取XML文件
			InputStream is = new FileInputStream("assets/XMLConfig.xml");
			//文档
			Document doc = dombuilder.parse(is);//解析来自InputStream的文件,创建为doc文件树
			//根节点
			Element root = doc.getDocumentElement();
			//根下的子节点
			NodeList books = root.getChildNodes();
			if (books != null) {
				for (int i = 0; i < books.getLength(); i++) {
					Node book = books.item(i);
					if (book.getNodeType() == Node.ELEMENT_NODE) {
						String MAP = book.getAttributes().getNamedItem("MAP").getNodeValue();
						System.out.println(MAP);
						//子节点的子节点
						for (Node node = book.getFirstChild(); node != null; node = node.getNextSibling()) {
							if (node.getNodeType() == Node.ELEMENT_NODE) {
								if (node.getNodeName() == "LOCATIONMETHOD") {
									//String name = node.getNodeValue();
									String LocationMethod = node.getFirstChild().getNodeValue();
									//System.out.println(name);
									System.out.println(LocationMethod);
									node.setTextContent("GPRS");
								}
								if (node.getNodeName() == "DOUBLECLCKZOOM") {
									String DoubleClickZoom = node.getFirstChild().getNodeValue();
									System.out.println(DoubleClickZoom);
        
									//修改字段
									node.setTextContent("false");
								}
								if(node.getNodeName()=="UPDATEPOSIONTIME"){
									String UpDatePositionTime = node.getFirstChild().getNodeValue();
									System.out.println(UpDatePositionTime);
									node.setTextContent("5");
								}
							}
						}
					}
				}
				
				//保存文件作用
				writeXML(doc, "NewFile.xml");
    
			}
		} catch (ParserConfigurationException e) {
			// TODO: handle exception
			e.printStackTrace();
		        //System.out.println("解析错误");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			//System.out.println("文件查找失败");
		} catch (SAXException e) {
			e.printStackTrace();
			//System.out.println("接口错误");
		} catch (IOException e) {
			e.printStackTrace();
			//System.out.println("输入/输出错误");
		}
	}
	/**
	 * @param args
	 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new XmlModifier();
	}
	//保存新的Document
	private static void writeXML(Document doc, String file) {
         try {
             Transformer t = TransformerFactory.newInstance().newTransformer();
             t.setOutputProperty("indent", "yes");
             t.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(file)));
         } catch (TransformerException e) {
             e.printStackTrace();
         } catch (FileNotFoundException e) {
             e.printStackTrace();  
         }
     }
	//创建新Document
	private static void newDocument(DocumentBuilder db) throws SAXException, IOException {
         Document doc = db.newDocument();
         Element root = doc.createElement("root");
         doc.appendChild(root);
         root.appendChild(doc.createElement("new"));
         writeXML(doc, "new.xml");
     }
}

但是运行时Console报错:

#

# A fatal error has been detected by the Java Runtime Environment:

#

#  Internal Error (classFileParser.cpp:3494), pid=4108, tid=4612

#  Error: ShouldNotReachHere()

#

# JRE version: 6.0_25-b06

# Java VM: Java HotSpot(TM) Client VM (20.0-b11 mixed mode windows-x86 )

# An error report file with more information is saved as:

# D:\AndroidWork\XmlConfigReader_Writer\hs_err_pid4108.log

#

# If you would like to submit a bug report, please visit:

#   http://java.sun.com/webapps/bugreport/crash.jsp

#

请大家指教。
XMLConfig.xml文件如下:
<?xml version="1.0" encoding="utf-8" ?>
  
    <MAP>
    	<LOCATIONMETHOD>ID</LOCATIONMETHOD>
    	<DOUBLECLICKZOOM>true</DOUBLECLICKZOOM>
    	<UPDATEPOSITIONTIME>1</UPDATEPOSITIONTIME>
    </MAP>

加载中
返回顶部
顶部