Applet:XML 找不到 DTD

发布于 2024-11-16 06:55:15 字数 700 浏览 1 评论 0原文

我正在编写一个小程序,它使用 DTD 文件来检查它接收到的 XML 的内容。

我遇到了 DTD 未使用小程序查看器放置在正确文件夹中的问题,但现在我正在服务器上测试它,我再次遇到相同的错误。

java.security.AccessControlException: 
    access denied (java.io.FilePermission/leveldtd.dtd read)

当小程序位于服务器上时,如何解决此问题?


public static void parseThis(InputSource is) throws Exception{
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLHandlerLevel myExampleHandler = new XMLHandlerLevel();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(myExampleHandler);
        /* Begin parsing */ 
        xr.parse(is);
    }

XML 解析器的创建。

I'm writing a applet that uses a DTD file to check the content of the XML it receives.

I had the problem of the DTD not placed in the right folder with the applet viewer, but now that I'm testing this on the server I get the same error again.

java.security.AccessControlException: 
    access denied (java.io.FilePermission/leveldtd.dtd read)

How can I fix this when the applet is on the server?


public static void parseThis(InputSource is) throws Exception{
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLHandlerLevel myExampleHandler = new XMLHandlerLevel();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(myExampleHandler);
        /* Begin parsing */ 
        xr.parse(is);
    }

XML parser creation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心小果奶 2024-11-23 06:55:15

小程序要从服务器获取资源,必须使用 URL。文件对象将不起作用,因为:

  1. File 对象将指向用户计算机上的某个位置。
  2. 它需要受信任的小程序才能使用File 对象。因此,输出中会出现 AccessControlException

可以使用 URL(baseURL, pathString) 构造函数轻松形成资源的 URL,其中基本 URL 是从 Applet.getDocumentBase()Applet.getCodeBase( )

..如何将 URL 提供给解析器?

以下是取自 JaNeLA 的代码片段,它使用位于其中一个 Jars 内的 XSD。 URL 存储在 schemaSource 中。

try {
    URL schemaSource = Thread.currentThread().getContextClassLoader().getResource("JNLP-6.0.xsd");
    System.out.println( "schemaSource: " + schemaSource );

    DocumentBuilderFactory factory =
        DocumentBuilderFactory.newInstance();
    factory.setFeature("http://xml.org/sax/features/validation", true);
    factory.setFeature("http://apache.org/xml/features/validation/schema", true) ;
    factory.setFeature("http://xml.org/sax/features/namespaces", true) ;
    factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
    factory.setAttribute(
        "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
        schemaSource.toString());
    factory.setNamespaceAware(true);
    factory.setValidating(true);

    InputStream schemaStream = schemaSource.openStream();
    try {
        StreamSource ss = new StreamSource( schemaStream );
        String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
        SchemaFactory schemaFactory = SchemaFactory.newInstance(language);

        Schema schema = schemaFactory.newSchema(ss);
        factory.setSchema( schema );
    }
    finally {
        schemaStream.close();
    }

    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    documentBuilder.setErrorHandler( errorHandler );

    InputStream is = page.openStream();
    try {
        document = documentBuilder.parse( is );
    }
    finally {
        is.close();
    }

    List<LaunchError> parseErrors = errorHandler.getParseErrors();
    xmlValid = parseErrors.isEmpty();
    errors.addAll(parseErrors);
} catch(Exception e) {
    System.err.println( "Error: " + e.getMessage() );
    // TODO Show to user
}

For an applet to get a resource from a server, it must use an URL. File objects will not work because:

  1. The File object will point to a place on the computer of the user.
  2. It requires a trusted applet to use File objects. Hence the AccessControlException in your output.

URLs to resources can easily be formed using the URL(baseURL, pathString) constructor where the base URL is obtained from Applet.getDocumentBase() or Applet.getCodeBase().

..how do I give the URL to the parser ?

Here is a code snippet taken from JaNeLA that uses an XSD located inside one of the Jars. The URL is stored in schemaSource.

try {
    URL schemaSource = Thread.currentThread().getContextClassLoader().getResource("JNLP-6.0.xsd");
    System.out.println( "schemaSource: " + schemaSource );

    DocumentBuilderFactory factory =
        DocumentBuilderFactory.newInstance();
    factory.setFeature("http://xml.org/sax/features/validation", true);
    factory.setFeature("http://apache.org/xml/features/validation/schema", true) ;
    factory.setFeature("http://xml.org/sax/features/namespaces", true) ;
    factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
    factory.setAttribute(
        "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
        schemaSource.toString());
    factory.setNamespaceAware(true);
    factory.setValidating(true);

    InputStream schemaStream = schemaSource.openStream();
    try {
        StreamSource ss = new StreamSource( schemaStream );
        String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
        SchemaFactory schemaFactory = SchemaFactory.newInstance(language);

        Schema schema = schemaFactory.newSchema(ss);
        factory.setSchema( schema );
    }
    finally {
        schemaStream.close();
    }

    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    documentBuilder.setErrorHandler( errorHandler );

    InputStream is = page.openStream();
    try {
        document = documentBuilder.parse( is );
    }
    finally {
        is.close();
    }

    List<LaunchError> parseErrors = errorHandler.getParseErrors();
    xmlValid = parseErrors.isEmpty();
    errors.addAll(parseErrors);
} catch(Exception e) {
    System.err.println( "Error: " + e.getMessage() );
    // TODO Show to user
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文