0

我有一个 XML 文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE dblp SYSTEM "dblp-2019-11-22.dtd">
<dblp>
<phdthesis mdate="2016-05-04" key="phd/dk/Heine2010">
<author>Carmen Heine</author>
<title>Modell zur Produktion von Online-Hilfen.</title>
<year>2010</year>
<school>Aarhus University</school>
<pages>1-315</pages>
<isbn>978-3-86596-263-8</isbn>
<ee>http://d-nb.info/996064095</ee>
</phdthesis><phdthesis mdate="2020-02-12" key="phd/Hoff2002">
.
. (continues with the same tags for a lot of other books)

从那个 XML 文件中,我试图从标签“year”中导出详细信息,以便计算每年出版了多少本书。为此,我尝试了很多实现,但似乎都没有奏效。

到目前为止我写的代码:

public class Publications {
    public static void main(String[] args) {
    try
    {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        DefaultHandler handler = new DefaultHandler()
        {
            boolean year = false;

            //parser starts parsing a specific element inside the document
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
            {
                System.out.println("Start Element :" + qName);
                if(qName.equalsIgnoreCase("Year"))
                {
                    year=true;
                }

            }
            //parser ends parsing the specific element inside the document
            public void endElement(String uri, String localName, String qName) throws SAXException
            {
                System.out.println("End Element:" + qName);
            }
            //reads the text value of the currently parsed element
            public void characters(char ch[], int start, int length) throws SAXException
            {
                if (year)
                {
                    System.out.println("Year : " + new String(ch, start, length));
                    year = false;
                }

            }
        };
        saxParser.parse("dblp-2020-04-01.xml", handler);
    }
catch (Exception e)
    {
        e.printStackTrace();
    }
}
}

我还添加了我得到的异常:

java.io.FileNotFoundException: C:\Users\Deray\DataAnalysis\dblp-2020-04-01.dtd 
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
    at java.base/sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:86)
    at java.base/sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:184)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:654)
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:150)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:860)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:824)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1216)
    at java.xml/com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:635)
    at java.xml/com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:324)
    at java.xml/javax.xml.parsers.SAXParser.parse

(SAXParser.java:276) 在 Publications.main(Publications.java:44)

您对实施还有其他建议吗?

4

0 回答 0