App下載
首頁javastaxJava HTML/XML - 如何解析實體

Java HTML/XML - 如何解析實體

我們想知道如何解析實體。
import java.io.FileInputStream;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLResolver;
import javax.xml.stream.XMLStreamException;

public class Main {
  public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setXMLResolver(new XMLResolver() {
      public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace)
          throws XMLStreamException {
        System.out.println(systemID);
        return null;
      }
    });

    XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(
        "dtd.xml"));
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);
    writer.add(reader);
    writer.flush();

  }

}