根据需求xml 创建一个结点以及文件。
public int onCreateEap() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(FILE_PATH));
Element eapElement = document.createElement("eap");
eapElement.setAttribute("active", "1");
eapElement.setAttribute("options", "8");
eapElement.setAttribute("strict", "0");
Element provisioningElement = (Element) document
.getElementsByTagName("provisioning").item(0);
provisioningElement.appendChild(eapElement);
Element certificateElement = document
.createElement("root-certificate");
certificateElement.setAttribute("encoding", "pem");
Element neweapElement = (Element) document.getElementsByTagName(
"eap").item(0);
Log.d("jcmtest", "readFileData********1");
neweapElement.appendChild(certificateElement);
Element naiElement = document.createElement("nai");
neweapElement.insertBefore(naiElement, certificateElement);
Element newnaiElement = (Element) document.getElementsByTagName(
"nai").item(0);
Log.d("jcmtest", "readFileData********2");
Element prefixElement = document.createElement("prefix");
newnaiElement.appendChild(prefixElement);
Element realmElement = document.createElement("realm");
newnaiElement.insertBefore(realmElement, prefixElement);
Element usernameElement = document.createElement("username");
newnaiElement.insertBefore(usernameElement, realmElement);
Element ttlsElement = document.createElement("ttls");
ttlsElement.setAttribute("active", "1");
neweapElement.insertBefore(ttlsElement, certificateElement);
Log.d("jcmtest", "readFileData********3");
Element outerNaiTypemElement = document
.createElement("outerNaiType");
ttlsElement.appendChild(outerNaiTypemElement);
Element passwordeElement = document.createElement("password");
ttlsElement.insertBefore(passwordeElement, outerNaiTypemElement);
Element typeeElement = document.createElement("type");
ttlsElement.insertBefore(typeeElement, passwordeElement);
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
Log.d("jcmtest", "readFileData********4");
File file = new File(FILE_PATH);
FileOutputStream out = new FileOutputStream(file);
StreamResult xmlResult = new StreamResult(out);
transformer.transform(domSource, xmlResult);
Log.d("jcmtest", "readFileData********5");
} catch (Exception ex) {
System.out.println(ex.getMessage());
Log.d("jcmtest", "readFileData********6");
return -1;
}
return 0;
}
根据xml 中的结点数据信息去删除某个结点信息
public int onDeleChannel(Intent intent, int flag) {
try {
int lenth = 0;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(FILE_PATH));
Element ndssElement = (Element) document.getElementsByTagName(
"ndss").item(0);
Element root = document.getDocumentElement();
NodeList nodes = root.getElementsByTagName("ndss");
for (int i = 0; i < nodes.getLength(); i++) {
Element channelElement = (Element) (nodes.item(i));
int oldlenth = channelElement.getElementsByTagName("channel")
.getLength();
lenth = oldlenth;
ArrayList<String> infoList = new ArrayList<String>();
infoList = intent.getStringArrayListExtra("position");
for (String temp : infoList) {
for (int i1 = 0; i1 < lenth; i1++) {
Element channelData = (Element) channelElement
.getElementsByTagName("channel").item(i1);
if (intent != null) {
if (temp.equals(channelData.getAttribute("id"))) {
ndssElement.removeChild(channelData);
}
}
int currentLenth = channelElement.getElementsByTagName(
"channel").getLength();
lenth = currentLenth;
}
}
}
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
// 设置编码类型
transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
File file = new File(FILE_PATH);
FileOutputStream out = new FileOutputStream(file);
StreamResult xmlResult = new StreamResult(out);
transformer.transform(domSource, xmlResult);
out.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
this.stopSelf();
Log.d("jcmtest", "delete .......finished");
}
return 0;
}
增加结点信息。
public int onaddChannel(Intent intent) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(FILE_PATH));
Element channelElement = document.createElement("channel");
channelElement.setAttribute("id", intent.getExtra("index", 00)
.toString());
channelElement.setAttribute("bandwidth",
intent.getExtra("bandwidth", "00").toString());
channelElement.setAttribute("fft", "1024");
channelElement.setAttribute("frequency",
intent.getExtra("frequency", "00").toString());
Element ndssElement = (Element) document.getElementsByTagName(
"ndss").item(0);
ndssElement.appendChild(channelElement);
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
// 设置编码类型
transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
File file = new File(FILE_PATH);
FileOutputStream out = new FileOutputStream(file);
StreamResult xmlResult = new StreamResult(out);
transformer.transform(domSource, xmlResult);
out.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
this.stopSelf();
}
return 0;
}
修改结点信息
private List<Channel> onChangeAuthentication(Intent intent) {
// TODO Auto-generated method stub
Log.d("jcmtest", "onChangeAuthentication");
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(FILE_PATH));
int eapLenth = document.getElementsByTagName("eap").getLength();
if (eapLenth == 0 || eapLenth < 0) {
if (onCreateEap() < 0) {
Log.d("jcmtest", "===create node error!!!!!!!!!!!!");
}
}
Element prefixelement = (Element) document.getElementsByTagName("prefix").item(0);
prefixelement.setTextContent(intent.getStringExtra("prefix"));
Element realmelement = (Element) document.getElementsByTagName("realm").item(0);
realmelement.setTextContent(intent.getStringExtra("realm"));
Element usernameelement = (Element) document.getElementsByTagName("username").item(0);
usernameelement.setTextContent(intent.getStringExtra("username"));
Element outerNaiTypelement = (Element) document .getElementsByTagName("outerNaiType").item(0);
outerNaiTypelement.setTextContent(intent.getStringExtra("outerNaiType"));
Element passwordelement = (Element) document.getElementsByTagName("password").item(0);
passwordelement.setTextContent(intent.getStringExtra("passwordelement"));
Element eapelement = (Element) (Element) document
.getElementsByTagName("eap").item(0);
Element ttlselement = (Element) (Element) document
.getElementsByTagName("ttls").item(0);
if ("Disable".equals(intent.getStringExtra("authentype"))){
eapelement.setAttribute("active", "0");
}else if ("TLS".equals(intent.getStringExtra("authentype"))) {
eapelement.setAttribute("active", "1");
ttlselement.setAttribute("active", "0");
} if ("TTLS".equals(intent.getStringExtra("authentype"))) {
eapelement.setAttribute("active", "1");
ttlselement.setAttribute("active", "1");
}
if (intent.getStringExtra("certificationpath") != null) {
if ("root".equals(intent.getStringExtra("certificate"))) {
Element certificateElement = (Element) document
.getElementsByTagName("root-certificate").item(0);
if (certificateElement==null){
certificateElement = document
.createElement("root-certificate");
certificateElement.setAttribute("encoding", "pem");
eapelement.insertBefore(certificateElement, eapelement.getElementsByTagName("nai").item(0));
}
certificateElement.setTextContent(readFileData(intent
.getStringExtra("certificationpath")));
}else if ("user".equals(intent.getStringExtra("certificate"))){
Element certificateElement = (Element) document
.getElementsByTagName("user-certificate").item(0);
if (certificateElement==null){
certificateElement = document
.createElement("user-certificate");
certificateElement.setAttribute("encoding", "pem");
eapelement.insertBefore(certificateElement, eapelement.getElementsByTagName("nai").item(0));
}
certificateElement.setTextContent(readFileData(intent
.getStringExtra("certificationpath")));
}else if ("userkey".equals(intent.getStringExtra("certificate"))){
Element certificateElement = (Element) document
.getElementsByTagName("userkey-certificate").item(0);
if (certificateElement==null){
certificateElement = document
.createElement("userkey-certificate");
certificateElement.setAttribute("encoding", "pem");
eapelement.insertBefore(certificateElement, eapelement.getElementsByTagName("nai").item(0));
}
certificateElement.setTextContent(readFileData(intent
.getStringExtra("certificationpath")));
}
}
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
File file = new File(FILE_PATH);
FileOutputStream out = new FileOutputStream(file);
StreamResult xmlResult = new StreamResult(out);
transformer.transform(domSource, xmlResult);
Log.d("jcmtest", "the node is exist ?===*************4****");
out.close();
} catch (Exception ex) {
// System.out.println(ex.getMessage());
ex.printStackTrace();
Log.d("jcmtest", "ex.getMessage() = "+ex.getMessage());
} finally {
Log.d("jcmtest", "finally");
this.stopSelf();
}
return null;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/tianxuhong/article/details/46785971