展开全部
38.读取XML数据库32313133353236313431303231363533e59b9ee7ad9431333238663631
/*
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
private Document document;
private Element node;
*/
File xml_file = new File(%%1);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(xml_file);
} catch (Exception e) {
e.printStackTrace();
}
String subNodeTag = %%2;
Element rootNode = document.getDocumentElement();
//%%2="Product" //%%4="id" //%%6="port"
//%%3="Name" //%%5="001"
NodeList nlist = rootNode.getElementsByTagName(subNodeTag);
int len = nlist.getLength();
for (int i = 0; i < len; i++) {
node = nlist.item(i);
String getNodeAttrValue = null;
NamedNodeMap attrList = node.getAttributes();
for (int j = 0; j < attrList.getLength(); j++) {
if (attrList.item(j).getNodeName().equals(%%4)) {
getNodeAttrValue = attrList.item(j).getNodeValue();
break;
}
}
if (getNodeAttrValue.equals(%%5)) {
nlist = node.getChildNodes();
String %%9=((Element) node).getElementsByTagName(%%3).item(0)
.getFirstChild().getNodeValue();
break;
}
}
39.写入XML数据库
/*
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
private Document document;
private Element node;
*/
File xml_file = new File(%%1);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(xml_file);
} catch (Exception e) {
e.printStackTrace();
}
String subNodeTag = %%2;
Element rootNode = document.getDocumentElement();
// %%2="Product" //%%4="pid" //%%6="author"
// %%3="Name" //%%5="price"
NodeList nlist = rootNode.getElementsByTagName(subNodeTag);
String ss = null;
int len = nlist.getLength();
for (int i = 0; i < len; i++) {
node = (Element) nlist.item(i);
//node.setAttribute(%%4, "0"+String.valueOf(i)); //ID格式化
String getNodeAttrValue = null;
NamedNodeMap attrList = node.getAttributes();
for (int j = 0; j < attrList.getLength(); j++) {
if (attrList.item(j).getNodeName().equals(%%4)) {
getNodeAttrValue = attrList.item(j).getNodeValue();
break;
}
}
if (getNodeAttrValue.equals("001")) {
nlist = node.getChildNodes();
ss = ((Element) node).getElementsByTagName(%%3).item(0)
.getFirstChild().getNodeValue();
ss = ((Element) node).getElementsByTagName(%%6).item(0)
.getFirstChild().getNodeValue();
ss = ((Element) node).getElementsByTagName(%%5).item(0)
.getFirstChild().getNodeValue();
((Element) node).getElementsByTagName(%%3).item(0)
.getFirstChild().setTextContent(%%7);
((Element) node).getElementsByTagName(%%6).item(0)
.getFirstChild().setTextContent(%%8);
((Element) node).getElementsByTagName(%%5).item(0)
.getFirstChild().setTextContent(%%9);
break;
}
}
if (ss == null) {
node = document.createElement(%%2);
node.setAttribute(%%4, String.valueOf(nlist.getLength() + 1));
node.appendChild(document.createTextNode("\n"));
Element server = document.createElement(%%3);
server.appendChild(document.createTextNode(%%7));
node.appendChild(server);
Element ipNode = document.createElement(%%6);
ipNode.appendChild(document.createTextNode(%%8));
node.appendChild(ipNode);
node.appendChild(document.createTextNode("\n"));
Element port = document.createElement(%%5);
port.appendChild(document.createTextNode(%%9));
node.appendChild(port);
node.appendChild(document.createTextNode("\n"));
rootNode.appendChild(node);
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(xml_file);
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起