python - 如何使用本地DTD文件使用lxml解析XML文件?

标签 python xml lxml dtd

我正在尝试使用 python 中的 lxml 解析 DBLP 数据集。但是它给出了这个错误:

lxml.etree.XMLSyntaxError: Entity 'uuml' not defined, line 54, column 43

DBLP 确实提供了用于定义实体的 DTD 文件 here 。如何使用该文件来解析 DBLP XML 文档?

这是我当前的代码:

filename = sys.argv[1]
dtd_name = sys.argv[2]
db_name = sys.argv[3]

conn = sqlite3.connect(db_name)

dblp_record_types_for_publications = ('article', 'inproceedings', 'proceedings', 'book', 'incollection',
    'phdthesis', 'masterthesis', 'www')

# read dtd
dtd = ET.DTD(dtd_name) #pylint: disable=E1101

# get an iterable
context = ET.iterparse(filename, events=('start', 'end'), load_dtd=True, #pylint: disable=E1101
    resolve_entities=True) 

# turn it into an iterator
context = iter(context)

# get the root element
event, root = next(context)

n_records_parsed = 0
for event, elem in context:
    if event == 'end' and elem.tag in dblp_record_types_for_publications:
        pub_year = None
        for year in elem.findall('year'):
            pub_year = year.text
        if pub_year is None:
            continue

        pub_title = None
        for title in elem.findall('title'):
            pub_title = title.text
        if pub_title is None:
            continue

        pub_authors = []
        for author in elem.findall('author'):
            if author.text is not None:
                pub_authors.append(author.text)

        # print(pub_year)
        # print(pub_title)
        # print(pub_authors)
        # insert the publication, authors in sql tables
        pub_title_sql_str = pub_title.replace("'", "''")
        pub_author_sql_strs = []
        for author in pub_authors:
            pub_author_sql_strs.append(author.replace("'", "''"))

        conn.execute("INSERT OR IGNORE INTO publications VALUES ('{title}','{year}')".format(
            title=pub_title_sql_str,
            year=pub_year))
        for author in pub_author_sql_strs:
            conn.execute("INSERT OR IGNORE INTO authors VALUES ('{name}')".format(name=author))
            conn.execute("INSERT INTO authored VALUES ('{author}','{publication}')".format(author=author,
                publication=pub_title_sql_str))

        elem.clear()
        root.clear()

        n_records_parsed += 1
        print("No. of records parsed: {}".format(n_records_parsed))

conn.commit()
conn.close()

最佳答案

将 DTD 文件保存在与 XML 文件相同的目录中,并确保 DTD 文件名与 XML 文档的 doctype 声明 ( <!DOCTYPE dblp SYSTEM "dblp.dtd"> ) 中的 DTD 文件名匹配,如 mzjn 在注释,它不再给出语法错误。

关于python - 如何使用本地DTD文件使用lxml解析XML文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48777616/

相关文章:

python - 从 lxml 获取内部文本

python - Pycuda 代码无法工作 : the "block" line in the call of the function doesn't work

R:解析大型非结构化 xml 文件

python - Xpath 不匹配

xml - 在后台线程上将 XML 解析为 CoreData 以不锁定 UI

java - 在 MainActivity.java 中更改 content_main.xml 上的 TextView

python - xml 文件上的 XPath 表达式

python - 如何在不编写额外代码的情况下使用 Python 读取 "-"(破折号)作为标准输入?

python - 将 df.groupby...max() 结果输入新列。 Pandas

当 var 被最明确地定义时,Python NameError