Java使用DOM方式读写XML

java

一、DOM读取

import ***;

public class ReadXML {

  public static void main(String[] args) {
    try {
      //DOM
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document document = builder.parse(new File("languages.xml"));
      Element root = document.getDocumentElement();
      System.out.println("cat="+root.getAttribute("cat"));
      NodeList list = root.getElementsByTagName("lan");
      for (int i = 0; i < list.getLength(); i++) {
        Element lan = (Element) list.item(i);
        System.out.println("---------------");
        System.out.println("+c.getTextContent());
          }
        }
      }
    } catch (**){...}

}

xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<Languages cat="it">
  <lan >
  <name>C#</name>
  <ide>Visual Studio</ide>
  </lan>
</Languages>

二、DOM创建XML

  //DOM

以上是 Java使用DOM方式读写XML 的全部内容, 来源链接: utcz.com/z/391417.html

回到顶部