javaxmljaxpentityresolver

How to resolve the Entity declarations and get the file path in an XML?


<?xml version='1.0' encoding='UTF-8'?>
 <!DOCTYPE dmodule 
 [<!NOTATION JPEG SYSTEM 'Joint Photographic Experts Group'>
  <!ENTITY abcd SYSTEM  'sunset.jpg' NDATA JPEG>
 ]>

  ....
  <graphic id = "abcd"/>

With reference to the above sample code, i need to get the sunset image on my html for which i need to get the filepath sunset.jpg which is defined in the entity declaration .

How would i do it in Java? I tried

 document.getDoctype().getEntities().item(i).getNodeName(),  

but it gives me abcd, but i need the filepath 'sunset.jpg'.

And i also tried,

builder.setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                // TODO Auto-generated method stub

                System.out.println("Public and System IDs"+publicId+"  "+systemId);
                return new InputSource(new StringReader(referDM));
            }
        });

but i think i have some problem with the return type as i am expecting an image file to be read as a byte array,? What should be the return type?


Solution

  • Here you go:

    Entity entity = (Entity) document.getDoctype().getEntities().item(i);
    String path = entity.getSystemId();