如何设置实体解析器?

How can I set the entity resolver?

我正在尝试动态设置 dtd 路径。当我使用 EntityResolver class 时,它适用于 org.xml.sax.XMLReader。但我不知道如何为 SAXSource 设置 EntityResolver。我该如何更正以下代码?

import java.util.logging.Logger;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class XMLProcessor  {

       public void transform(String xmlf, String xslf) throws TransformerConfigurationException, TransformerException, org.xml.sax.SAXException, IOException{


        Transformer transformer;
        TransformerFactory factory = TransformerFactory.newInstance();

         StreamSource stylesheet = new StreamSource(xslf);

         //Source source = StreamSource(xmlf);
         SAXSource source = new SAXSource(new InputSource(xmlf));
         org.xml.sax.XMLReader reader = XMLReaderFactory.createXMLReader();

         EntityResolver ent = new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {

                System.out.println(publicId);
                System.out.println(systemId);

                if(publicId.equals("-//OASIS//DTD DITA BookMap//EN")){
                    return new InputSource("file:///D:/dtd/bookmap/dtd/bookmap.dtd");
                }

                if(systemId.equals("file:///D:/doc/maps/bookmap.dtd")){
                    return new InputSource("file:////dtd/bookmap/dtd/bookmap.dtd");
                }
                return null;
                }
            };
        // sour.setPublicId("file:///D:/dtd/bookmap/dtd/bookmap.dtd");



            reader.setEntityResolver(ent);
            reader.parse(new InputSource(xmlf));
         //StreamSource sourcedoc = new StreamSource(xmlf);
         transformer = factory.newTransformer(stylesheet);

        try {
            transformer.transform(source, new StreamResult(new FileWriter("out/result.xml")));
        } catch (IOException ex) {
            Logger.getLogger(XMLProcessor.class.getName()).log(Level.SEVERE, null, ex);
        }

    }




}

实体解析似乎适用于此行:

 reader.parse(new InputSource(xmlf));

但是下面一行失败了:

transformer.transform(source, new StreamResult(new FileWriter("out/result.xml")));

我得到这个错误:

doc\maps\bookmap.dtd (The system cannot find the file specified)

有一个构造函数

public SAXSource(XMLReader reader,
                 InputSource inputSource)

https://docs.oracle.com/javase/8/docs/api/javax/xml/transform/sax/SAXSource.html#SAXSource-org.xml.sax.XMLReader-org.xml.sax.InputSource-

所以你应该可以使用

     org.xml.sax.XMLReader reader = XMLReaderFactory.createXMLReader();

     EntityResolver ent = new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {

            System.out.println(publicId);
            System.out.println(systemId);

            if(publicId.equals("-//OASIS//DTD DITA BookMap//EN")){
                return new InputSource("file:///D:/dtd/bookmap/dtd/bookmap.dtd");
            }

            if(systemId.equals("file:///D:/doc/maps/bookmap.dtd")){
                return new InputSource("file:////dtd/bookmap/dtd/bookmap.dtd");
            }
            return null;
            }
        };



        reader.setEntityResolver(ent);

        SAXSource source = new SAXSource(reader, new InputSource(xmlf));