⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wmlprocessor.java

📁 一个很好xml学习样例
💻 JAVA
字号:
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","hello");
    NodeList list = root.getElementsByTagName("card");
    for (int i = 0; i < list.getLength(); i++){
      System.out.println(list.item(i).getFirstChild().getNodeValue());
    }

  }

  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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -