App下載
首頁javadomJava HTML/XML - 如何獲取XML節(jié)點(diǎn)名稱

Java HTML/XML - 如何獲取XML節(jié)點(diǎn)名稱

我們想知道如何獲取XML節(jié)點(diǎn)名稱。
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class Main {
  public static void main(String[] args) throws Exception {
    String xml = "<a><o><u>ok</u></o></a>";
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
        .parse(new InputSource(new StringReader(xml)));
    NodeList nl = doc.getElementsByTagName("*");
    for (int i = 0; i < nl.getLength(); i++) {
      System.out.println("name is : " + nl.item(i).getNodeName());
    }
  }
}