Java 无效的.dtd文件:[致命错误]:32:43:属性声明中需要属性类型";页码“;对于“元素”;“计量文件”;

Java 无效的.dtd文件:[致命错误]:32:43:属性声明中需要属性类型";页码“;对于“元素”;“计量文件”;,java,xml,dtd,Java,Xml,Dtd,我不知道这为什么不起作用。我试图解析一些xml文件,在此之前介绍.dtd文件。不幸的是,这不起作用,因为它抛出了一个org.xml.sax.SAXParseException try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

我不知道这为什么不起作用。我试图解析一些xml文件,在此之前介绍.dtd文件。不幸的是,这不起作用,因为它抛出了一个
org.xml.sax.SAXParseException

    try {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

        dBuilder.setEntityResolver(new EntityResolver() {
            @Override
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {

                if (systemId.contains("meter.dtd") == false) {
                    return null;
                }

                String path = null;
                try {
                    File dtd = Resource.getFileFromResource("meter_corpus/sgml_dtds/meter.dtd");
                    path = dtd.getAbsolutePath();
                } catch (Exception e) {
                    e.printStackTrace();
                }   

                if(path == null) {
                    return null;
                }
                return new InputSource(new FileReader(path));
            }
        });

        xmlDocument = dBuilder.parse(xmlFile);
        xmlDocument.getDocumentElement().normalize();
    }catch (Exception e) {
        e.printStackTrace();
    }
meter.dtd
文件:

<!ELEMENT meterdocument  - - (title?,body)>
<!ATTLIST meterdocument 
                         classification CDATA    #IMPLIED
                         pagenumber     NUMBER   #IMPLIED
                         filename       CDATA    #REQUIRED 
                         newspaper      CDATA    #REQUIRED 
                         domain         CDATA    #REQUIRED
                         date           CDATA    #REQUIRED
                         catchline      CDATA    #REQUIRED >
<!ELEMENT title          - - (#PCDATA)>
<!ELEMENT body           - - (((verbatim | rewrite | new)+) | unclassified)>
<!ELEMENT verbatim       - - (#PCDATA)>
<!ATTLIST verbatim       PAsource   CDATA #IMPLIED>
<!ELEMENT rewrite        - - (#PCDATA)>
<!ATTLIST rewrite        PAsource   CDATA #IMPLIED>
<!ELEMENT new            - - (#PCDATA)>
<!ATTLIST new            PAsource   CDATA #IMPLIED>
<!ELEMENT unclassified   - - (#PCDATA)>

应可分析的文件:

<!DOCTYPE meterdocument SYSTEM "meter.dtd" [
]>

<meterdocument  filename="/meter_corpus/newspapers/annotated/courts/01.03.00/football/football382_star.sgml" newspaper="star" domain="courts" classification="wholly-derived" pagenumber="12" date="01.03.00" catchline="football">

<body>
<Verbatim PAsource="" >SIX football fans will</Verbatim>
<Rewrite PAsource="" > find out </Rewrite>
<Rewrite PAsource="" >today </Rewrite>
<Rewrite PAsource="" >whether they have won their fight to stop </Rewrite>
<Verbatim PAsource="" >Newcastle United </Verbatim>
<Rewrite PAsource="" >moving </Rewrite>
<Verbatim PAsource="" >their seats. </Verbatim>
<Verbatim PAsource="" >Mr Justice Blackburne, sitting at Newcastle High Court, </Verbatim>
<Rewrite PAsource="" >will reveal </Rewrite>
<Verbatim PAsource="" >his </Verbatim>
<Rewrite PAsource="" >decision over the </Rewrite>
<Verbatim PAsource="" >season ticket holders' </Verbatim>
<Rewrite PAsource="" >battle </Rewrite>
<Verbatim PAsource="" >at noon.
</Verbatim>
</body>
</meterdocument>

六个足球迷将参加比赛
找出
今天
他们是否赢得了阻止的斗争
纽卡斯尔联队
移动
他们的座位。
Blackburne法官在纽卡斯尔高等法院,
将揭示
他的
决定
季票持有人
战争
中午。
以及包含错误行的完整堆栈跟踪:

[Fatal Error] :32:43: The attribute type is required in the declaration of attribute "pagenumber" for element "meterdocument".
org.xml.sax.SAXParseException; lineNumber: 32; columnNumber: 43; The attribute type is required in the declaration of attribute "pagenumber" for element "meterdocument".
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:348)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:205)
    at eval.meter.METERDocument.<init>(METERDocument.java:68)
    at eval.meter.METERCorpus.runMeterCorpusTest(METERCorpus.java:194)
    at eval.meter.METERCorpus.main(METERCorpus.java:92)
java.lang.NullPointerException
    at eval.meter.METERDocument.<init>(METERDocument.java:74)
    at eval.meter.METERCorpus.runMeterCorpusTest(METERCorpus.java:194)
    at eval.meter.METERCorpus.main(METERCorpus.java:92)
[致命错误]:32:43:元素“meterdocument”的属性“pagenumber”声明中需要属性类型。
org.xml.sax.SAXParseException;行号:32;栏目号:43;元素“meterdocument”的属性“pagenumber”声明中需要该属性类型。
位于com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
在com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:348)
位于javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:205)
在eval.meter.METERDocument.(METERDocument.java:68)
at eval.meter.METERCorpus.runmetercorpuster(METERCorpus.java:194)
at eval.meter.METERCorpus.main(METERCorpus.java:92)
java.lang.NullPointerException
在eval.meter.METERDocument.(METERDocument.java:74)
at eval.meter.METERCorpus.runmetercorpuster(METERCorpus.java:194)
at eval.meter.METERCorpus.main(METERCorpus.java:92)

如何才能正确解析此文件?

请尝试
NMTOKEN
(名称标记)或其他方法,而不是
NUMBER
。这个
#隐含的
可能值得一个值。

从你身上删除破折号(“-”)dtd@Alexander我试过了,但它给了我同样的错误。然后你应该提供更多的细节,比如异常来自哪一行,最好是整个堆栈跟踪,以及一个你认为符合DTD的示例XML文件。理想情况下,您希望人们能够复制问题,但目前没有足够的信息。@dcsohl我已经添加了缺少的信息。如果你还需要别的东西,请告诉我