wmlprocessor.java~11~
来自「一个很好xml学习样例」· JAVA~11~ 代码 · 共 54 行
JAVA~11~
54 行
package xmltest;
import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class WMLProcessor {
public static void main(String[] args) {
File xmlFile = new File("test.xml");
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(xmlFile);
}
catch (IOException ex) {
ex.printStackTrace();
}
catch (SAXException ex) {
ex.printStackTrace();
}
catch (ParserConfigurationException ex) {
ex.printStackTrace();
}
Element root = doc.getDocumentElement();
//stepThrough(root);
changeWML(root,"card","改名啦");
}
private static void stepThrough(Node start){
System.out.println(start.getNodeName() + " = " + start.getNodeValue());
if (start.getNodeType() == Node.ELEMENT_NODE) {
NamedNodeMap startAttr = start.getAttributes();
for (int i = 0; i < startAttr.getLength(); i++){
Node attr = startAttr.item(i);
System.out.println(" Attibute: " + attr.getNodeName() + " = " + attr.getNodeValue());
}
}
for (Node child = start.getFirstChild() ; child != null; child = child.getNextSibling()){
stepThrough(child);
}
}
private static void changeWML(Node start, String elemName, String elemValue){
if (start.getNodeName().equals(elemName)) {
start.getFirstChild().setNodeValue(elemValue);
}
for (Node child = start.getFirstChild();child != null; child = child.getNextSibling()){
changeWML(child, elemName, elemValue);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?