📄 config_xml.java
字号:
package com.hxyh.sanny.mms.telecom.config;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import java.io.ByteArrayInputStream;
/**
*/
public class Config_XML {
Document doc;
private DocumentBuilderFactory dbf = null;
private DocumentBuilder builder = null;
/**
* @param fileName
*/
public Config_XML() {
try {
dbf = DocumentBuilderFactory.newInstance();
builder = dbf.newDocumentBuilder();
}
catch (Exception e) {
System.out.println("---error to create documentbuilder");
}
}
/**
* @return HashMap
*/
/*
public static HashMap storeDomTree() {
HashMap map = new HashMap();
String NameKey = new String("");
String TextValue = new String("");
try {
Element root = doc.getDocumentElement();
NodeList subNodes = root.getChildNodes();
Element subNode = null;
NodeList subElements = null;
for (int i = 0; i < subNodes.getLength(); i++) {
NameKey = subNodes.item(i).getNodeName();
subElements = subNodes.item(i).getChildNodes();
for (int j = 0; j < subElements.getLength(); j++) {
NodeList text = subElements.item(j).getChildNodes();
if (text.getLength() == 1) {
TextValue = text.item(0).getNodeValue();
String Key = NameKey + "." +
subElements.item(j).getNodeName();
map.put(Key, TextValue);
}
}
}
}
catch (DOMException dome) {
System.out.println("����DOM�����:" + dome);
}
catch (java.lang.ClassCastException cce) {
System.out.println("�������" + cce.getMessage());
}
return map;
}*/
/**
* <p>@param: xmlName</p>
* <p>@param: subNode</p>
* <p>@param: TextNode</p>
* <p>@return: String</p>
*/
public String readAttribute(String subNode, String TextNode) {
String attributeValue = new String("");
try {
Element root = doc.getDocumentElement();
NodeList nodes = root.getElementsByTagName(subNode);
for (int i = 0; i < nodes.getLength(); i++) {
Element subNodes = (Element) nodes.item(i);
NodeList subElement = subNodes.getElementsByTagName(TextNode);
if (subElement.getLength() == 1) {
Text text = (Text) subElement.item(0).getFirstChild();
attributeValue = text.getNodeValue();
}
}
}
catch (Exception dome) {
System.out.println("����DOM�����:" + dome);
}
return attributeValue;
}
public void writexmltofile() {
try {
Transformer trans = TransformerFactory.newInstance().newTransformer();
Source source = new DOMSource(doc);
Result result = new StreamResult("d:/resppack.xml");
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
trans.transform(source, result);
}
catch (TransformerFactoryConfigurationError tfce) {
System.err.println(tfce.getException().toString());
}
catch (TransformerConfigurationException tce) {
System.out.println("�������" + tce);
}
catch (TransformerException te) {
System.out.println("�������" + te);
}
}
/**
* <p>@param: xmlName</p>
* <p>@param: subNode</p>
* <p>@param: TextNode</p>
* <p>@param: value</p>
*/
public void setAttribute(String subNode, String TextNode, String value) {
try {
Element root = doc.getDocumentElement();
NodeList nodes = root.getElementsByTagName(subNode);
for (int i = 0; i < nodes.getLength(); i++) {
Element subNodes = (Element) nodes.item(i);
NodeList subElement = subNodes.getElementsByTagName(TextNode);
if (subElement.getLength() == 1) {
Text text = (Text) subElement.item(0).getFirstChild();
// attributeValue = text.getNodeValue();
// Text text = (Text) localElement.getFirstChild();
text.setNodeValue(value);
}
}
}
catch (DOMException dome) {
System.out.println("����DOM�����:" + dome);
}
catch (java.lang.ClassCastException cce) {
System.out.println("�������" + cce.getMessage());
}
}
/**
* set
*
* @param aString String
*/
public void set(String aString) {
try {
doc = builder.parse(new ByteArrayInputStream(aString.getBytes()));
//}
// doc = builder.parse(oTmp);
}
catch (Exception dome) {
System.out.println("get xml " + dome);
}
}
/**
* getXmlStream
*
* @param anObject OutputStream
*/
public String getXmlStream() {
try {
ByteArrayOutputStream anObject = new ByteArrayOutputStream(8000);
Transformer trans = TransformerFactory.newInstance().newTransformer();
Source source = new DOMSource(doc);
Result result = new StreamResult(anObject);
//trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
trans.transform(source, result);
return anObject.toString();
}
catch (Exception tfce) {
System.err.println("---Error at ConfigXML:" + tfce.toString());
return "";
}
}
private String Temp;
private BufferedReader readBuffer;
public boolean readLine() {
try {
if ( (Temp = readBuffer.readLine()) != null) {
Temp = Temp.trim();
}
else {
return false;
}
}
catch (IOException e) {
e.printStackTrace();
return false;
}
/*if (Temp == null) {
return false;
}*/
return true;
}
/**
* setaFile
*
* @param aString String
*/
public void setaFile(String aString) {
File file2 = new File(aString);
if (!file2.exists()) {
System.err.println("---Error at getdatafrom a file");
}
try {
doc = builder.parse(file2);
}
catch (Exception e) {
System.err.println("---Error at getdatafrom a file" + e);
}
}
/**
* writexmltofile
*/
/* public static void main(String[] args) {
Config_XML config = new Config_XML("e:/list.xml");
config.setAttribute("setup3", "noWay", "Yes");
//String value = config.readAttribute("list","listname");
HashMap map = new HashMap();
try {
map = config.storeDomTree();
}
catch (NullPointerException npe) {
System.out.println("Error...." + "��");
}
String value = (String) map.get("setup2.Name");
System.out.println(value);
value = (String) map.get("setup3.noWay");
System.out.println(value);
System.out.println(map.size());
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -