如何解析实体声明并获取XML中的文件路径?

时间:2012-08-29 11:47:05

标签: java xml jaxp entityresolver

<?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"/>

参考上面的示例代码,我需要在我的html上获取日落图像,我需要获取在实体声明中定义的文件路径sunset.jpg。

我如何用Java做到这一点? 我试过了

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

但它给了我abcd,但我需要文件路径'sunset.jpg'。

我也试过了,

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));
            }
        });

但我认为我对返回类型有一些问题,因为我期待将图像文件作为字节数组读取,?什么应该是返回类型?

1 个答案:

答案 0 :(得分:1)

你走了:

Entity entity = (Entity) document.getDoctype().getEntities().item(i);
String path = entity.getSystemId();
相关问题