App下載
首頁javadomJava HTML/XML - 如何在DOM文檔中獲取符號

Java HTML/XML - 如何在DOM文檔中獲取符號

我們想知道如何在DOM文檔中獲取符號。
   
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Notation;
import org.xml.sax.InputSource;

public class Main {
  public static String getXMLData(){
    return "your data";
  }
  
  public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    NamedNodeMap notations = doc.getDoctype().getNotations();
    
    for (int i = 0; i < notations.getLength(); i++) {
      Notation notation = (Notation) notations.item(i);

      String notationName = notation.getNodeName();
      System.out.println(notationName);
      String notationPublicId = notation.getPublicId();
      String notationSystemId = notation.getSystemId();
    }
  }
}